DataclassInspector 1.0.1
See the generated code of dataclasses with reassembling bytecode, formatting static templates and tons of magic.
License: MIT
Requires Python: >=3.7

******************
DataclassInspector
******************

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.

.. code-block:: python

    >>> 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).

.. code-block:: python
    
    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;

.. code-block:: python

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

The output is;

.. code-block:: python

    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)