dataclass-abc


Namedataclass-abc JSON
Version 0.0.8 PyPI version JSON
download
home_pagehttps://github.com/MichaelSchneeberger/dataclass-abc
SummaryLibrary that lets you define abstract properties for dataclasses.
upload_time2023-04-21 06:53:54
maintainer
docs_urlNone
authorMichael Schneeberger
requires_python>=3.10
license
keywords dataclass-abc abstract abc property
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Dataclass ABC

Library that lets you define abstract properties for dataclasses. 

## Installation

```pip install dataclass-abc```

## Usage

The `dataclassabc` class decorator resolves the abstract properties 
overwritten by a field.

``` python
from abc import ABC, abstractmethod

from dataclassabc import dataclassabc

class A(ABC):
    @property
    @abstractmethod
    def name(self) -> str:
        ...

@dataclassabc(frozen=True)
class B(A):
    name: str        # overwrites the abstract property 'name' in 'A'
```

## Define mutable variables

Define a mutable variable `name` in the abstract class `A` by using the
`name.setter` decorator. 

``` python
from abc import ABC, abstractmethod

from dataclassabc import dataclassabc

class A(ABC):
    @property
    @abstractmethod
    def name(self) -> str:
        ...

    @name.setter
    @abstractmethod
    def name(self, val: str):
        ...

    def set_name(self, val: str):
        self.name = val

@dataclassabc
class B(A):
    name: str

b = B(name='A')
b.set_name('B')
```

## Example

The [example](https://github.com/MichaelSchneeberger/dataclass-abc/tree/master/example)
implements the code snippets taken from [RealPython](https://realpython.com/python-data-classes/)
 with abstract properties.

## Design pattern

This library suggests the following design pattern:

- **mixins** - a *mixin* is an abstract class that implements data as abstract
properties and methods based on the abstract properties.
- **classes** - an abstract class inherits from one or more mixins
(see `City` or `CapitalCity` in the example). This class is used for pattern matching,
e.g. using `isinstance` method.
- **impl** - an *implementation class* implements the abstract properties. 
(see `CityImpl` or `CapitalCityImpl` in the example). This class is decorated with
`dataclassabc` and `resolve_abc_prop` and should always be called through an 
*initialize function*.
- **init** - an *initialize function* (or *constructor function*) initializes an 
*implementation class*.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/MichaelSchneeberger/dataclass-abc",
    "name": "dataclass-abc",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "dataclass-abc abstract abc property",
    "author": "Michael Schneeberger",
    "author_email": "michael.schneeb@outlook.com",
    "download_url": "https://files.pythonhosted.org/packages/f1/7e/7682d3baa50e0fadc009d6093d07b457e6ccdbc2952efc6e2238cfa267b0/dataclass-abc-0.0.8.tar.gz",
    "platform": null,
    "description": "# Dataclass ABC\n\nLibrary that lets you define abstract properties for dataclasses. \n\n## Installation\n\n```pip install dataclass-abc```\n\n## Usage\n\nThe `dataclassabc` class decorator resolves the abstract properties \noverwritten by a field.\n\n``` python\nfrom abc import ABC, abstractmethod\n\nfrom dataclassabc import dataclassabc\n\nclass A(ABC):\n    @property\n    @abstractmethod\n    def name(self) -> str:\n        ...\n\n@dataclassabc(frozen=True)\nclass B(A):\n    name: str        # overwrites the abstract property 'name' in 'A'\n```\n\n## Define mutable variables\n\nDefine a mutable variable `name` in the abstract class `A` by using the\n`name.setter` decorator. \n\n``` python\nfrom abc import ABC, abstractmethod\n\nfrom dataclassabc import dataclassabc\n\nclass A(ABC):\n    @property\n    @abstractmethod\n    def name(self) -> str:\n        ...\n\n    @name.setter\n    @abstractmethod\n    def name(self, val: str):\n        ...\n\n    def set_name(self, val: str):\n        self.name = val\n\n@dataclassabc\nclass B(A):\n    name: str\n\nb = B(name='A')\nb.set_name('B')\n```\n\n## Example\n\nThe [example](https://github.com/MichaelSchneeberger/dataclass-abc/tree/master/example)\nimplements the code snippets taken from [RealPython](https://realpython.com/python-data-classes/)\n with abstract properties.\n\n## Design pattern\n\nThis library suggests the following design pattern:\n\n- **mixins** - a *mixin* is an abstract class that implements data as abstract\nproperties and methods based on the abstract properties.\n- **classes** - an abstract class inherits from one or more mixins\n(see `City` or `CapitalCity` in the example). This class is used for pattern matching,\ne.g. using `isinstance` method.\n- **impl** - an *implementation class* implements the abstract properties. \n(see `CityImpl` or `CapitalCityImpl` in the example). This class is decorated with\n`dataclassabc` and `resolve_abc_prop` and should always be called through an \n*initialize function*.\n- **init** - an *initialize function* (or *constructor function*) initializes an \n*implementation class*.\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Library that lets you define abstract properties for dataclasses.",
    "version": "0.0.8",
    "split_keywords": [
        "dataclass-abc",
        "abstract",
        "abc",
        "property"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a6d13e7e687c78e9ccf524fbba2d5f745b8ac86f766d3193b3f40557dcbc08e8",
                "md5": "2b829fcaa0af38fc3a85bda226d89c17",
                "sha256": "817ebd5b83e9853129061faca432d31a3756580ab9d3df18405cda63769462ec"
            },
            "downloads": -1,
            "filename": "dataclass_abc-0.0.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2b829fcaa0af38fc3a85bda226d89c17",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 7604,
            "upload_time": "2023-04-21T06:53:53",
            "upload_time_iso_8601": "2023-04-21T06:53:53.452236Z",
            "url": "https://files.pythonhosted.org/packages/a6/d1/3e7e687c78e9ccf524fbba2d5f745b8ac86f766d3193b3f40557dcbc08e8/dataclass_abc-0.0.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f17e7682d3baa50e0fadc009d6093d07b457e6ccdbc2952efc6e2238cfa267b0",
                "md5": "97e37911c9d30cc2c8a4a6f331bfa946",
                "sha256": "f93f93c5e30d982af39539bbc6b83fda0a8158c9fd30e2b2e016992825073f29"
            },
            "downloads": -1,
            "filename": "dataclass-abc-0.0.8.tar.gz",
            "has_sig": false,
            "md5_digest": "97e37911c9d30cc2c8a4a6f331bfa946",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 7272,
            "upload_time": "2023-04-21T06:53:54",
            "upload_time_iso_8601": "2023-04-21T06:53:54.998160Z",
            "url": "https://files.pythonhosted.org/packages/f1/7e/7682d3baa50e0fadc009d6093d07b457e6ccdbc2952efc6e2238cfa267b0/dataclass-abc-0.0.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-21 06:53:54",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "MichaelSchneeberger",
    "github_project": "dataclass-abc",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "dataclass-abc"
}
        
Elapsed time: 0.31172s