Skip to main content

ntgen - named tuple generator

CircleCI PyPI - Package Version PyPI - Python Version Coverage Status PyPI - License

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] [-f] [-t]
             [--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=True) Convert the NamedTuple field
                        names to snake_case
  -c, --camel-case      (bool, default=True) Convert the NamedTuple class
                        names to CamelCase
  -f, --constructors    (bool, default=False) Insert generic methods that will
                        allow for parsing of the analyzed data structures
  -t, --as-dict         (bool, default=False) 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.3.0'

License

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

Author

Maciej Rapacz

Release files for ntgen 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ntgen 0.3.1
File Size Uploaded
ntgen-0.3.1.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ntgen 0.3.1
File Interpreter ABI Platform
ntgen-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.3 kB

Release files / ntgen-0.3.1.tar.gz

Download URL ntgen-0.3.1.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b1789575a1aab79c37192559d65d18835b0ea45fe180d69c485e7106eeb992c1
BLAKE2b-256 checksum
How to use checksums
53a1cb50362f1de5e7f09ac2fa6632a1bdb8e868188aa1b00eec756dec787723
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.1 Darwin/19.6.0

Release files / ntgen-0.3.1-py3-none-any.whl

Download URL ntgen-0.3.1-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
23dffdea80cf2c473d80e987cde76e60a1eaf90c89492174f87789278ddaccbf
BLAKE2b-256 checksum
How to use checksums
378592ba06d8754807f11c45df0b4603edea795a70b0c3120452b8a0790017c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.1 Darwin/19.6.0

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page