dataclass-factory


Namedataclass-factory JSON
Version 2.16 PyPI version JSON
download
home_pagehttps://github.com/tishka17/dataclass_factory
SummaryAn utility class for creating instances of dataclasses
upload_time2022-07-20 14:21:06
maintainer
docs_urlNone
authorA. Tikhonov
requires_python>=3.6
licenseApache2
keywords
VCS
bugtrack_url
requirements dataclasses typing_extensions nose2 mypy vulture flake8 flake8-blind-except flake8-bugbear flake8-builtins flake8-cognitive-complexity flake8-commas flake8-comprehensions flake8-docstrings flake8-eradicate flake8-import-order flake8-mutable flake8-polyfill flake8-print sphinx sphinx_rtd_theme
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # dataclass_factory

[![PyPI version](https://badge.fury.io/py/dataclass-factory.svg)](https://badge.fury.io/py/dataclass-factory)
[![Build Status](https://travis-ci.org/Tishka17/dataclass_factory.svg?branch=master)](https://travis-ci.org/Tishka17/dataclass_factory)
[![downloads](https://img.shields.io/pypi/dm/dataclass_factory.svg)](https://pypistats.org/packages/dataclass_factory)
[![license](https://img.shields.io/github/license/Tishka17/dataclass_factory.svg)](https://github.com/Tishka17/dataclass_factory/blob/master/LICENSE)

**dataclass_factory** is a modern way to convert dataclasses or other objects to and from more common types like dicts

## Help

See [documentation](https://dataclass-factory.readthedocs.io/) for more details.

## TL;DR

Install
```bash
pip install dataclass_factory
```

Use
```python
from dataclasses import dataclass
import dataclass_factory


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


data = {
    "title": "Fahrenheit 451",
    "price": 100,
}

factory = dataclass_factory.Factory()
book: Book = factory.load(data, Book)  # Same as Book(title="Fahrenheit 451", price=100)
serialized = factory.dump(book) 
``` 

## Requirements

* python >= 3.6

You can use `dataclass_factory` with python 3.6 and `dataclass` library installed from pip. 

On python 3.7 it has no external dependencies outside of the Python standard library.

## Advantages

* No schemas or configuration needed for simple cases. Just create `Factory` and call `load`/`dump` methods
* Speed. It is up to 10 times faster than `marshmallow` and `dataclasses.asdict` (see [benchmarks](benchmarks))
* Automatic name style conversion (e.g. `snake_case` to `CamelCase`)
* Automatic skipping of "internal use" fields (with leading underscore)
* Enums, typed dicts, tuples and lists are supported out of the box
* Unions and Optionals are supported without need to define them in schema
* Generic dataclasses can be automatically parsed as well
* Cyclic-referenced structures (such as linked-lists or trees) also can be converted
* Validators, custom parser steps are supported.
* Multiple schemas for single type can be provided to support different ways of parsing of the same type

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/tishka17/dataclass_factory",
    "name": "dataclass-factory",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "",
    "author": "A. Tikhonov",
    "author_email": "17@itishka.org",
    "download_url": "",
    "platform": null,
    "description": "# dataclass_factory\n\n[![PyPI version](https://badge.fury.io/py/dataclass-factory.svg)](https://badge.fury.io/py/dataclass-factory)\n[![Build Status](https://travis-ci.org/Tishka17/dataclass_factory.svg?branch=master)](https://travis-ci.org/Tishka17/dataclass_factory)\n[![downloads](https://img.shields.io/pypi/dm/dataclass_factory.svg)](https://pypistats.org/packages/dataclass_factory)\n[![license](https://img.shields.io/github/license/Tishka17/dataclass_factory.svg)](https://github.com/Tishka17/dataclass_factory/blob/master/LICENSE)\n\n**dataclass_factory** is a modern way to convert dataclasses or other objects to and from more common types like dicts\n\n## Help\n\nSee [documentation](https://dataclass-factory.readthedocs.io/) for more details.\n\n## TL;DR\n\nInstall\n```bash\npip install dataclass_factory\n```\n\nUse\n```python\nfrom dataclasses import dataclass\nimport dataclass_factory\n\n\n@dataclass\nclass Book:\n    title: str\n    price: int\n    author: str = \"Unknown author\"\n\n\ndata = {\n    \"title\": \"Fahrenheit 451\",\n    \"price\": 100,\n}\n\nfactory = dataclass_factory.Factory()\nbook: Book = factory.load(data, Book)  # Same as Book(title=\"Fahrenheit 451\", price=100)\nserialized = factory.dump(book) \n``` \n\n## Requirements\n\n* python >= 3.6\n\nYou can use `dataclass_factory` with python 3.6 and `dataclass` library installed from pip. \n\nOn python 3.7 it has no external dependencies outside of the Python standard library.\n\n## Advantages\n\n* No schemas or configuration needed for simple cases. Just create `Factory` and call `load`/`dump` methods\n* Speed. It is up to 10 times faster than `marshmallow` and `dataclasses.asdict` (see [benchmarks](benchmarks))\n* Automatic name style conversion (e.g. `snake_case` to `CamelCase`)\n* Automatic skipping of \"internal use\" fields (with leading underscore)\n* Enums, typed dicts, tuples and lists are supported out of the box\n* Unions and Optionals are supported without need to define them in schema\n* Generic dataclasses can be automatically parsed as well\n* Cyclic-referenced structures (such as linked-lists or trees) also can be converted\n* Validators, custom parser steps are supported.\n* Multiple schemas for single type can be provided to support different ways of parsing of the same type\n",
    "bugtrack_url": null,
    "license": "Apache2",
    "summary": "An utility class for creating instances of dataclasses",
    "version": "2.16",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "439c9e4e4459b81fe83ed03b08b736df",
                "sha256": "9d01e73d40b8f74051df822f21d8dc8bbf2754b11670cd1c477357e8b21323ed"
            },
            "downloads": -1,
            "filename": "dataclass_factory-2.16-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "439c9e4e4459b81fe83ed03b08b736df",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 29693,
            "upload_time": "2022-07-20T14:21:06",
            "upload_time_iso_8601": "2022-07-20T14:21:06.678629Z",
            "url": "https://files.pythonhosted.org/packages/f1/66/50b9f5d8a0e9fbe5469c5b8a7f198511fff9959347fc2443531d651b21d7/dataclass_factory-2.16-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-07-20 14:21:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "tishka17",
    "github_project": "dataclass_factory",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "dataclasses",
            "specs": []
        },
        {
            "name": "typing_extensions",
            "specs": []
        },
        {
            "name": "nose2",
            "specs": []
        },
        {
            "name": "mypy",
            "specs": []
        },
        {
            "name": "vulture",
            "specs": []
        },
        {
            "name": "flake8",
            "specs": [
                [
                    "==",
                    "3.*"
                ]
            ]
        },
        {
            "name": "flake8-blind-except",
            "specs": []
        },
        {
            "name": "flake8-bugbear",
            "specs": []
        },
        {
            "name": "flake8-builtins",
            "specs": []
        },
        {
            "name": "flake8-cognitive-complexity",
            "specs": []
        },
        {
            "name": "flake8-commas",
            "specs": []
        },
        {
            "name": "flake8-comprehensions",
            "specs": []
        },
        {
            "name": "flake8-docstrings",
            "specs": []
        },
        {
            "name": "flake8-eradicate",
            "specs": []
        },
        {
            "name": "flake8-import-order",
            "specs": []
        },
        {
            "name": "flake8-mutable",
            "specs": []
        },
        {
            "name": "flake8-polyfill",
            "specs": []
        },
        {
            "name": "flake8-print",
            "specs": []
        },
        {
            "name": "sphinx",
            "specs": []
        },
        {
            "name": "sphinx_rtd_theme",
            "specs": []
        }
    ],
    "lcname": "dataclass-factory"
}
        
Elapsed time: 0.02196s