Skip to main content

See the generated code of dataclasses with reassembling bytecode, formatting static templates and tons of magic.

Project description

See the generated code of dataclasses with reassembling bytecode, formatting static templates and tons of magic.

Inspector

Inspector is a class for inspecting dataclasses. You can initalize it with the dataclass.

>>> from DataclassInspector.inspector import Inspector
>>> @dataclass
... class Item:
...     name: str
...     value: int = 34
...
>>> inspected = Inspector(Item)

Then open a file and write inspected.code into it (the example output is like below).

from dataclasses import Field, _MISSING_TYPE, _DataclassParams


class Item:
    __dataclass_fields__ = {
        "name": "Field(name='name', type=str, default=_MISSING_TYPE, default_factory=_MISSING_TYPE, init=True, repr=True, hash=None, compare=True, metadata={}, _field_type=_FIELD)",
        "value": "Field(name='value', type=int, default=34, default_factory=_MISSING_TYPE, init=True, repr=True, hash=None, compare=True, metadata={}, _field_type=_FIELD)",
    }
    __dataclass_params__ = _DataclassParams(
        init=True,
        repr=True,
        eq=True,
        order=False,
        unsafe_hash=False,
        frozen=False,
    )
    name: str
    value: int = 34

    def __eq__(self, other):
        if other.__class__ is self.__class__:
            return (self.name, self.value) == (other.name, other.value)
        else:
            return NotImplemented

    __hash__ = None

    def __init__(self, name: str, value: int = 34) -> None:
        self.name = name
        self.value = value

    def __repr__(self):
        return (
            self.__class__.__qualname__
            + f"""(name={(self.name)!r}, value={(self.value)!r})"""
        )

It also provides auto-generation of other custom functions;

>>> @dataclass(order=True)
... class NewItem(Item):
...     name: int
...     value: str
...     def convert(self):
...         self.name = str(name)
...
>>> inspected = Inspector(NewItem)

The output is;

from dataclasses import Field, _MISSING_TYPE, _DataclassParams

class NewItem(Item):
    __dataclass_fields__ = {
        "name": "Field(name='name', type=int, default=_MISSING_TYPE, default_factory=_MISSING_TYPE, init=True, repr=True, hash=None, compare=True, metadata={}, _field_type=_FIELD)",
        "value": "Field(name='value', type=str, default=34, default_factory=_MISSING_TYPE, init=True, repr=True, hash=None, compare=True, metadata={}, _field_type=_FIELD)",
    }
    __dataclass_params__ = _DataclassParams(
        init=True,
        repr=True,
        eq=True,
        order=True,
        unsafe_hash=False,
        frozen=False,
    )
    name: int
    value: str

    def __eq__(self, other):
        if other.__class__ is self.__class__:
            return (self.name, self.value) == (other.name, other.value)
        else:
            return NotImplemented

    def __ge__(self, other):
        if other.__class__ is self.__class__:
            return (self.name, self.value) >= (other.name, other.value)
        else:
            return NotImplemented

    def __gt__(self, other):
        if other.__class__ is self.__class__:
            return (self.name, self.value) > (other.name, other.value)
        else:
            return NotImplemented

    __hash__ = None

    def __init__(self, name: int, value: str = 34) -> None:
        self.name = name
        self.value = value

    def __le__(self, other):
        if other.__class__ is self.__class__:
            return (self.name, self.value) <= (other.name, other.value)
        else:
            return NotImplemented

    def __lt__(self, other):
        if other.__class__ is self.__class__:
            return (self.name, self.value) < (other.name, other.value)
        else:
            return NotImplemented

    def __repr__(self):
        return (
            self.__class__.__qualname__
            + f"""(name={(self.name)!r}, value={(self.value)!r})"""
        )

    def convert(self):
        self.name = str(name)

Project details


Download files

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

Files for DataclassInspector, version 1.0.1
Filename, size File type Python version Upload date Hashes
Filename, size DataclassInspector-1.0.1.tar.gz (3.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page