Skip to main content

An utility class for creating instances of dataclasses

Project description

dataclass_factory

PyPI version Build Status

Dataclass instance creation library

You can convert dataclass to dict using asdict method, but cannot convert back. This module provides ParserFactory method for such task.

It is very useful in combination with json

What's supported

  • dataclass from dict
  • Enum from its value
  • List, Set, FrozenSet, Dict
  • Tuple with specified types or ellipsis
  • Optional with specified type
  • Union parsed in order of given types
  • Any returned as is
  • int/float/decimal also parsed from string
  • other classes based on their __init__ method
  • custom parser if specified

Usage

Install:

pip install dataclass_factory 

Code:

@dataclass
class Book:
    title: str
    author: str = "Unknown author"


data = {
    "title": "Fahrenheit 451"
}

parserFactory = dataclass_factory.ParserFactory()
obj = parserFactory.get_parser(Book)(data)  # Same as Book(title="Fahrenheit 451")

You need to create parserFactory only once at the startup of your app. It caches created parsers and it will be significantly quicker than creating parser each time.

Extended usage

Parser factory provides some useful options:

  • trim_trailing_underscore (enabled by default) - allows to trim trailing unders score in dataclass field names when looking them in corresponding dictionary.
    For example field id_ can be stored is id
  • debug_path - allows to see path to an elemetn, that cannot be parsed in raised Exception.
    This causes some permfomance decrease
  • type_factories - dictionary with type as a key and functions that can be used to create intances of corresponding types as value.
    See below.

Custom parsers and dict factory

You can provide your parsers for types that are not supported. For example, you can parse datetime from iso format.

Also there is dict_factory, which can help you to serialize data in your dataclasses. You can provide custom serializers as well

from dataclass_factory import ParserFactory, dict_factory
from datetime import datetime
import dateutil.parser
from dataclasses import dataclass, asdict


parserFactory = ParserFactory()


@dataclass
class Todo:
    id_: int
    title: str
    deadline: datetime


data = {
    "id": 1,
    "deadline": "2025-12-31T00:00:00",
    "title": "Release 1.0"
}

todo = Todo(
    id_=1,
    title="Release 1.0",
    deadline=datetime(2025, 12, 31, 0, 0, 0)
)
assert todo == parserFactory.get_parser(Todo)(
    data,
    Todo,
    trim_trailing_underscore=True,
    type_factories={datetime: dateutil.parser.parse}
)

assert data == asdict(
    todo,
    dict_factory=dict_factory(
        trim_trailing_underscore=True,
        type_serializers={datetime: datetime.isoformat}
    )
)

Compatibility

In versions below 1.0 there was a simple parse method.

It is still provided for compatibility, but is not recommended because it recreates ParserFactory each time it is called It can be removed in some future version

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

dataclass_factory-1.0.2.tar.gz (6.1 kB view details)

Uploaded Source

File details

Details for the file dataclass_factory-1.0.2.tar.gz.

File metadata

  • Download URL: dataclass_factory-1.0.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.7

File hashes

Hashes for dataclass_factory-1.0.2.tar.gz
Algorithm Hash digest
SHA256 b7b2e14cc19364fd45a6680ebf6566f8ac9a06b3c17778aafab76424b58b480b
MD5 6fea950faea6d6f27d8059eed6118dd5
BLAKE2b-256 a4c606fe95657a4ff0870f5e611c5303f006802dd7542115d83837e29a69cca5

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