Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

DDD Value Objects implementation

Project description

Info:DDD Value Object implementation.
Author: Paweł Zadrożny @pawelzny <pawel.zny@gmail.com>
CI Status Documentation Status PyPI Repository Status Release Status Project Status Supported python versions Supported interpreters License

Features

  • Value Objects are immutable.
  • Two objects with the same values are considered equal
  • Access to values with dot notation: value.my_attr
  • Access to values by key: value['my_attr']

Installation

pipenv install vo  # or pip install vo

Package: https://pypi.org/project/vo/

Quick Example

Value accept any key=value pairs. These pairs will be attached to object as attributes. Once created values are immutable. Attributes can’t be changed or deleted.

>>> from vo import Value
>>> book = Value(title='Learning Python',
...              authors=['Mark Lutz', 'David Ascher'],
...              publisher="O'REILLY")
>>> book
Value(authors=['Mark Lutz', 'David Ascher'], publisher="O'REILLY", title='Learning Python')

>>> str(book)
'{"authors": ["Mark Lutz", "David Ascher"], "publisher": "O\'REILLY", "title": "Learning Python"}'

Warning

Any attempt of value modification or delete will raise ImmutableInstanceError

>>> from vo import Value
>>> book = Value(title='Learning Python',
...              authors=['Mark Lutz', 'David Ascher'],
...              publisher="O'REILLY")
>>> book.title = 'Spam'
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    raise ImmutableInstanceError()
  vo.value.ImmutableInstanceError: Modification of Value frozen instance is forbidden.

Values access

Values can be accessed like object attributes or like dict keys.

>>> from vo import Value
>>> book = Value(title='Learning Python',
...              authors=['Mark Lutz', 'David Ascher'],
...              publisher="O'REILLY")
>>> book.title == book['title']
True

>>> book.authors == book['authors']
True

Objects comparison

Let’s take the same book example.

>>> from vo import Value
>>> book1 = Value(title='Learning Python',
...               authors=['Mark Lutz', 'David Ascher'],
...               publisher="O'REILLY")
>>> book2 = Value(title='Learning Python',
...               authors=['Mark Lutz', 'David Ascher'],
...               publisher="O'REILLY")
>>> book1 == book2
True

>>> book1 is book2
False

Value lookup

Check if value exists.

>>> from vo import Value
>>> book = Value(title='Learning Python',
...              authors=['Mark Lutz', 'David Ascher'],
...              publisher="O'REILLY")
>>> 'title' in book
True

>>> 'price' in book
False

>>> book.title
'Learning Python'

>>> book.price
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'Value' object has no attribute 'price'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for vo, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size vo-1.0.0.tar.gz (32.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page