A simple mixin for create Value Objects
Project description
Based on Ruby Gem by NoFlopSquad (https://github.com/noflopsquad/value-object)
A value object is a small object that represents a simple entity whose equality isn’t based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object.
Installation
> pip install simple-value-object
Usage
Constructor and field readers
from simple_value_object import ValueObject
class Point(ValueObject):
def __init__(self, x, y):
pass
point = Point(1, 2)
point.x
# 1
point.y
# 2
point.x = 5
# CannotBeChangeException: You cannot change values from a Value Object, create a new one
class Date(ValueObject):
def __init__(self, day, month, year):
pass
date = Date(1, 10, 2015)
date.day
# 1
date.month
# 10
date.year
# 2015
date.month = 5
# CannotBeChangeException: You cannot change values from a Value Object, create a new one
Equality based on field values
from simple_value_object import ValueObject
class Point(ValueObject):
def __init__(self, x, y):
pass
a_point = Point(5, 3)
same_point = Point(5, 3)
a_point == same_point
# True
a_different_point = Point(6, 3)
a_point == a_different_point
# False
Hash code based on field values
from simple_value_object import ValueObject
class Point(ValueObject):
def __init__(self, x, y):
pass
a_point = Point(5, 3)
same_point = Point(5, 3)
a_point.hash == same_point.hash
# True
a_different_point = Point.new(6, 3)
a_point.hash == a_different_point.hash
# False
Invariants
from simple_value_object import ValueObject, invariant
class Point(ValueObject):
def __init__(self, x, y):
pass
@invariant
def inside_first_quadrant(cls, instance):
return instance.x > 0 and instance.y > 0
@invariant
def x_less_than_y(cls, instance):
return instance.x < instance.y
Point(-5, 3)
#ViolatedInvariantException: Args values [-5, 3] violates invariant: inside_first_cuadrant
Point(6, 3)
#ViolatedInvariantException: Args values [6, 3] violates invariant: x_less_than_y
Point(1,3)
#<__main__.Point at 0x7f2bd043c780>
Mutable types not allowed
from simple_value_object import ValueObject, invariant
class AValueObject(ValueObject):
def __init__(self, an_arg):
pass
AValueObject(an_arg={'key': 'value'})
#MutableTypeNotAllowedException: 'an_arg' cannot be a mutable data type.
AValueObject({'key': 'value'})
#MutableTypeNotAllowedException: Mutable args are not allowed.
ValueObject within ValueObject
from simple_value_object import ValueObject, invariant
class Money(ValueObject):
def __init__(self, amount, currency):
pass
class Currency(ValueObject):
def __init__(self, symbol):
pass
Money(amount=100, currency=Currency(symbol="€"))
Test
> pip install -r requirements-test.txt
> PYTHONPATH=$PYTHONPATH:. mamba
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file simple-value-object-1.1.1.tar.gz.
File metadata
- Download URL: simple-value-object-1.1.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/2.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
259315e88c5eb035c5d585238f632e0b16c7a15c72f8b48e09a18ae80117fbdb
|
|
| MD5 |
89ab10a1fc9c391213f8c3c800b6a815
|
|
| BLAKE2b-256 |
1dcdb8ab2e45e01f8390fccd773ad8343c6815c4167b23a7f69325e5c3a9ecc8
|