Public API¶
Instance of Value¶
-
class
vo.value.
Value
(**kwargs)[source]¶ Basic implementation of DDD immutable Value Object.
Features:
- Two objects with exact values are considered the same
- Objects are immutable (raise ImmutableInstanceError)
Example: >>> val = Value(name='Primary', price=10.35, currency='USD') >>> val.price 10.35 >>> val['currency'] 'USD'
Parameters: **kwargs – Any key=value pairs.
Create new instance of Value¶
Value accept any key=value pairs. These pairs will be attached to object as attributes. Once created values are immutable. Can’t be changed or deleted.
>>> from vo import Value
>>> book = Value(title='Learning Python',
... authors=['Mark Lutz', 'David Ascher'],
... publisher="O'REILLY")
>>> book
<vo.value.Value object at 0x7f38862b3860>
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'
Modification forbidden error¶
-
exception
vo.value.
ImmutableInstanceError
(message: str = None)[source]¶ Raised on any attempt to modify values in Value object.
Parameters: message (str) – Optional message. -
message
= 'Modification of Value instance is forbidden.'¶
-
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.