Skip to main content

Python3 Custom Dataclass Helpers

Project description

pyderive

PyPI version Python versions License: MIT Made with Love

Python3 Custom Dataclass Helpers

Installation

pip install pyderive3

Features

Additional Features availalbe via Extensions.

Expanded Reimplementation of Dataclasses
Default Standards Implemented

'slots' kwarg now supported in older versions of python such as 3.8

from pyderive import *

@dataclass
class Foo:
    bar: int

@dataclass(slots=True, order=True)
class Bar(Foo):
    foo: str

bar = Bar(foo='foo', bar=100)
print(asdict(bar))
Recursive Field Compilation
from pyderive import *
from typing import Optional

class Foo:
    bar: int

@dataclass(recurse=True)
class Bar(Foo):
    foo: Optional[str] = None

bar = Bar(foo='foo', bar=100)
print(bar)
Allow for Custom Field Definitions
from pyderive import *
from typing import Any

@dataclass
class NewField(BaseField):
    custom_attr: bool = False

def field(*_, **kwargs) -> Any:
    return NewField(**kwargs)

@dataclass(field=NewField)
class Foo:
    a: int
    b: str = field(default='b', repr=False, custom_attr=True)

foo = Foo(1, 'boo')
print(foo)

for f in fields(foo):
    print(f.name, f.custom_attr)
Backwards Compatibility
import pyderive
import dataclasses

@dataclasses.dataclass
class Foo:
    a: int
    b: int = dataclasses.field(repr=False)

@pyderive.dataclass
class Bar(Foo):
    c: int = pyderive.field(frozen=True)

f = Bar(1, 2, 3)
print(f)
Monkey Patching
from pyderive import compat
compat.monkey_patch()

from dataclasses import dataclass, field

@dataclass
class Foo:
    a: int
    b: int = field(frozen=True)

f = Foo(1, 2)
print(f)
Low Level Dataclass Compilation Tools
from pyderive import *

class Foo:
    foo: int

class Bar(Foo):
    bar: int = 100

# parse class-structure into raw unordered heigharhcy
struct = parse_fields(Bar, recurse=True)
print('struct', struct)

# order and structure fields into organized list following dataclass standard
fields = flatten_fields(struct)
print('final_fields', fields)

# build and assign `init` and `repr` functions to `Bar` class
assign_func(Bar, create_init(fields))
assign_func(Bar, create_repr(fields))

# assign slots for field definitions onto `Bar` class
Bar = add_slots(Bar, fields)
print(Bar.__slots__)

# initialize foo w/ compiled `init` func and print w/ added `repr`
bar = Bar(foo=1, bar=2)
print(bar)

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

pyderive3-0.0.7.tar.gz (41.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyderive3-0.0.7-py3-none-any.whl (52.8 kB view details)

Uploaded Python 3

File details

Details for the file pyderive3-0.0.7.tar.gz.

File metadata

  • Download URL: pyderive3-0.0.7.tar.gz
  • Upload date:
  • Size: 41.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.8.20

File hashes

Hashes for pyderive3-0.0.7.tar.gz
Algorithm Hash digest
SHA256 0c97a6a327459e4dad18e51e6420f7b5a943724cd3a03e67579acd0aae2fd9a3
MD5 e45250ba6ebdcb429169655a9aed6be7
BLAKE2b-256 3b9bf43107c2c92c4a283f4ca5e6bb989a7aaa1407db4cb6aa12034a99d01040

See more details on using hashes here.

File details

Details for the file pyderive3-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: pyderive3-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 52.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.8.20

File hashes

Hashes for pyderive3-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 726a57facc4d6d9a33bb26db19cd4d4c11739fd2e48dda123d135d17f4f1a17a
MD5 44d00d4d2d4c11101f053beffc0ad0cd
BLAKE2b-256 316bb3b2de481802940d3a6231b6307e1d2478ca6ceb399c45359996135e8dda

See more details on using hashes here.

Supported by

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