Skip to main content
Latest PyPI version Latest Travis CI build status

Value objects 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 classes should subclass base Value class. Every Value subclass has to define attributes, which is an iterable containing names of all attributes. This may happen 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

By extracting attribute names 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

By using a shortcut assign_attributes to replace 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__

Memory efficiency

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

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__ [1].

  • Overriding __new__ when a default values is required.

  • Repeating attributes names in several places.

  • Unintuitive inheritance by generating parent class on the fly.

Metadata

Release files for precious 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for precious 0.1.1
File Size Uploaded
precious-0.1.1.tar.gz 4.1 kB Details

Release files / precious-0.1.1.tar.gz

Download URL precious-0.1.1.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e2bb253292c15bdb45c0e8e1c8db83c94f1c6e51aa5862a469f55fd5b8b60330
BLAKE2b-256 checksum
How to use checksums
9529a18d12e52cd6aa735b7e974d7d62ab67f70e4df3ae9f9eb09fbad5b72fd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page