Skip to main content

ntgen: generate your NamedTuple definition

Project description

ntgen

Generate NamedTuple definitions with typehints based on your data automatically. If you've ever felt like preparing NamedTuple skeletons for any json data you're dealing with is tedious and could be automated, well, this is the tool that automates the process.

Usage

Let's say you want to prepare a NamedTuple definition for the following json object:

$ cat apartment.json
{
    "id": "1234-1234",
    "type": "living",
    "isAvailable": true,
    "countryCode": "DE",
    "address": {
        "borough": "Dulsberg",
        "city": "Hamburg",
        "houseNumber": "2",
        "latitude": 53.587485,
        "longitude": 10.063215,
        "postalCode": "22049",
        "streetName": "Nordschleswiger Strasse",
        "area": "Hamburg"
    },
    "_attachments": "attachments/",
    "_ts": 15828103462
}%

All you need to do is run the following command:

$ ntgen apartment.json
class Address(NamedTuple):
    borough: str
    city: str
    house_number: str
    latitude: float
    longitude: float
    postal_code: str
    street_name: str
    area: str



class Apartment(NamedTuple):
    id: str
    type: str
    is_available: bool
    country_code: str
    address: Address
    attachments: str
    ts: int

The output will be directed to stdout by default - you may also redirect it to a file to bootstrap a Python module with the class definitions.

Runtime configuration

To find out about all of the runtime configuration options, run:

$ ntgen --help
usage: ntgen [--out OUT] [--name NAME] [-s] [-c] [--insert_constructors] [--insert_as_dict] [--max_level MAX_LEVEL] [-h] input

positional arguments:
  input                 (str, default=None) Json file containing an object with the data to analyzed

optional arguments:
  --out OUT             (Union[str, NoneType], default=None) Destination file to write the Python code to
  --name NAME           (str, default=NTGenTuple) Name of the main NamedTuple
  -s, --snake-case      (bool, default=False) Convert the NamedTuple field names to snake_case
  -c, --camel-case      (bool, default=True) Convert the NamedTuple class names to CamelCase
  --insert_constructors
                        (bool, default=True) Insert generic methods that will allow for parsing of the analyzed data structures
  --insert_as_dict      (bool, default=True) Insert generic methods allowing for dumping the nested NamedTuple hierarchy to a dict
  --max_level MAX_LEVEL
                        (Union[int, NoneType], default=None) Specify the max nesting level of the NamedTuple
  -h, --help            show this help message and exit

Other invocation options

You can also use the library from the Python context:

>>> from ntgen import generate_from_dict
>>> data = {'name': 'John Wick', 'profession': 'assassin', 'age': 34}
>>> print(generate_from_dict(data=data, name="Character"))
class Character(NamedTuple):
    name: str
    profession: str
    age: int

Installation

You'll need to be running Python >= 3.7.

pip install ntgen

Verify that the latest package version was installed correctly:

>>> import ntgen
>>> ntgen.__version__
'0.1.0'

License

This project is licensed under the MIT License - see the LICENSE file for details

Author

Maciej Rapacz

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ntgen-0.1.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

ntgen-0.1.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file ntgen-0.1.0.tar.gz.

File metadata

  • Download URL: ntgen-0.1.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for ntgen-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b7ab03d1d813e12a8f6530e8c021bebd825391d070de641eea12f137a4a6f83a
MD5 ec3aad603727da0cfbd51f8930346e89
BLAKE2b-256 a0c62dbaae4213b807c29d2b248b824b13a11f3181b5ba9ab2e5338a94396d21

See more details on using hashes here.

File details

Details for the file ntgen-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ntgen-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for ntgen-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 90b3675690737f9e6066ce7c901e02b5a71bfb6469ac0ecad2ea256294a0024f
MD5 0d21f8193748fcf95513e4fcd817e4ba
BLAKE2b-256 5708b16ddb3dbfebdbe50bbf28b5d773471105d4a5aa583ebd6ea3f849948f8d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page