This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

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.
[1](1, 2) https://docs.python.org/3/reference/datamodel.html#slots
Release History

Release History

0.1.1

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
precious-0.1.1.tar.gz (4.1 kB) Copy SHA256 Checksum SHA256 Source Nov 1, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting