Skip to content

biern/precious

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Precious

Latest PyPI version Latest Travis CI build status

Implementation of Value Object [1] for Python.

Example

from precious import Value, assign_attributes


class Color(Value):
    @assign_attributes
    def __init__(self, red, green, blue, alpha=0): pass

    @property
    def grayscale(self):
        return (self.r + self.g + self.b) / 3
>>> red = Color(255, 0, 0)
>>> red
Color(255, 0, 0, 0)
>>> red == Color(255, 0, 0)
True
>>> Color.__slots__
('red', 'green', 'blue', 'alpha')
>>> hash(red)
8736776571231852889

Installation

pip install precious

Usage

Value Object definitions should subclass Value. Every Value subclass has to define attributes, which is an iterable containing names of all attributes. This may be achieved by explicitly setting the attribute on the class:

class Point(Value):
    attributes = ('x', 'y')

    def __init__(self, x, y):
        self.x = x
        self.y = y

Attribute names can also be extracted directly from __init__ definition using one of provided helper decorators:

from precious import Value, extract_attributes

class Point(Value):
    @extract_attributes
    def __init__(self, x, y):
        self.x = x
        self.y = y

Finally, the assign_attributes decorator allows to reduce a common boilerplate of extracting and assigning all the attributes in __init__:

from precious import Value, assign_attributes

class Point(Value):
    @assign_attributes
    def __init__(self, x, y): pass

Note that in the example above attributes are not being assigned in parent's class __init__, thus no super() call is required.

Features

Value implements

  • __eq__
  • __repr__
  • __hash__
  • __slots__

Footprint

Value class footprint is as small as possible. It provides only methods listed above and a single attributes attribute.

Memory efficiency

Subclassing Value automaticaly assignes names of all attributes to __slots__ [2].

Testing

Just run tox in package directory:

$ tox

Why not simply use namedtuple?

Namedtuple definition is equally fine for simple use cases.

Point = namedtuple('Point', ('x', 'y'))

Having to repeat the classname is a minor inconvinience, but the definition is pretty readable and concise. Also, class gets iterable interface and indexing support, which sometimes is what you want. However, things with namedtuple get very ugly when a default value or a method or a property is required. Subclassing is the only way to go. Consider the following example:

class Color(namedtuple('Color_', ('r', 'g', 'b', 'alpha'))):
    __slots__ = ()

    def __new__(cls, r, g, b, alpha=0):
        return super().__new__(cls, r, g, b, alpha)

    @property
    def grayscale(self):
        return (self.r + self.g + self.b) / 3

# Equivalent to

class Color(Value):
    @assign_attributes
    def __init__(self, red, green, blue, alpha=0): pass

    @property
    def grayscale(self):
        return (self.r + self.g + self.b) / 3

To sum up, problems with extending namedtuple include:

  • Having to define empty __slots__ [2].
  • Overriding __new__ when a default values is required.
  • Repeating attributes names in several places.
  • Unintuitive inheritance by generating parent class on the fly.
[1]http://martinfowler.com/bliki/ValueObject.html
[2](1, 2) https://docs.python.org/3/reference/datamodel.html#slots

About

Value objects for python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages