Skip to main content

DDD entity

Project description

dddentity

PyPI PyPI - Python Version Code style: black license

DDD entity.

Usage

from dddentity import Entity, ref

UserId = str


class User(Entity[UserId]):
    def __init__(self, id: UserId, name: str) -> None:
        self.id = id
        self.name = name

    def _ref_(self) -> UserId:
        return self.id


assert User("0001", "Gilgamesh") == User("0001", "Bilgamesh")

assert ref(User("0002", "Enkidu")) == "0002"

Caveat

Currently eq=False must be set when using with dataclasses to prevent to generate __eq__() method by dataclass.

@dataclass(eq=False)
class User(Entity[UserId]):
    ...

API

Module dddentity

abstract class Entity[T]

Entity class. Classes implementing this class must implement abstract method _ref_().

This class is hashable, satisfying the following conditions:

  • entity1 == entity2 iff entity1._ref_() == entity2._ref_()
  • hash(entity) == hash(entity._ref_())

where entity, entity1, entity2 are entities.

type parameter T

Type of the identifier of the entity, which must be hashable.

abstract method _ref_() -> T

Shall return the identifier of the entity.

function ref(entity: Entity[T]) -> T

Return entity._ref_().

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

dddentity-0.1.0.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

dddentity-0.1.0-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file dddentity-0.1.0.tar.gz.

File metadata

  • Download URL: dddentity-0.1.0.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/2.7.12 Darwin/20.2.0

File hashes

Hashes for dddentity-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1ac702a841b7b2c2747ad1059853c478bded70c6384921d9a43cead7856446b8
MD5 55c4d375afa98c93215c4423a99a6bd3
BLAKE2b-256 7d5d8fe317dc03da8173645167f2819cf67adcddd5ba07c602a7a3df6fcee80a

See more details on using hashes here.

File details

Details for the file dddentity-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dddentity-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/2.7.12 Darwin/20.2.0

File hashes

Hashes for dddentity-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4320953c4757428fbb4b9e7c4b030eadf67b2d252790755610bfd10a13e9e867
MD5 9258e88d976a58b3a8fdb59f984414ec
BLAKE2b-256 8104516782a5ff46fac5e3eaf4490b32c8f91204d168d2d2d5b29d759fa5f308

See more details on using hashes here.

Supported by

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