Skip to main content

An example package. Replace this with a proper project description. Generated with https://github.com/ionelmc/cookiecutter-pylibrary

Project description

Travis-CI Build Status AppVeyor Build Status Coverage Status PYPI Package PYPI Package

Container class boilerplate killer.

Features:

  • Human-readable __repr__

  • Complete set of comparison methods

  • Keyword and positional argument support. Works like a normal class - you can override just about anything in the subclass (eg: a custom __init__). In contrast, hynek/characteristic forces different call schematics and calls your __init__ with different arguments.

Installation

pip install fields

Usage

Make a class that has 2 attributes, a and b:

>>> from fields import Fields
>>> class Pair(Fields.a.b):
...     pass
...
>>> p = Pair(1, 2)
>>> p.a
1
>>> p.b
2

Make a class that has one required attribute value and two attributes (left and right) with default value None:

>>> class Node(Fields.value.left[None].right[None]):
...     pass
...
>>> p = Node(1, left=Node(2), right=Node(3, left=Node(4)))
>>> p
<Node(left=<Node(left=None, right=None, value=2)>, right=<Node(left=<Node(left=None, right=None, value=4)>, right=None, value=3)>, value=1)>

Want tuples ?

Namedtuple alternative:

>>> from fields import Tuple
>>> class Pair(Tuple.a.b):
...     pass
...
>>> p = Pair(1, 2)
>>> p.a
1
>>> p.b
2
>>> tuple(p)
(1, 2)
>>> a, b = p
>>> a
1
>>> b
2

FAQ

Why ???

It’s less to type, why have quotes around when the names need to be valid symbols anyway.

Really … why ???

Because it’s possible.

But you’re abusing a very well known syntax. You’re using attribute access instead of a list of strings. Why ?

Symbols should be symbols. Why validate strings so they are valid symbols when you can avoid that ? Just use symbols. Dooh !

The use of language constructs is not that surprising - it’s pretty obvious that in field2 of field1 of fields.Fields there can’t possibly be anything else but a class implementing a container for said two fields.

Why would anyone with a brain expect anything else ? It’s like looking at a cake resembling a dog and expecting the cake to bark and run around.

What’s good about this ?

It’s one of the shortest forms possible.

Is this stable ? Is it tested ?

Yes. Mercilessly tested on Travis and AppVeyor.

Is the API stable ?

It can’t get worse can it ;)

Why not namedtuple ?

It’s ugly, repetivive and unflexible. Compare this:

>>> from collections import namedtuple
>>> class MyContainer(namedtuple("MyContainer", ["field1", "field2"])):
...     pass
>>> MyContainer(1, 2)
MyContainer(field1=1, field2=2)

To this:

>>> class MyContainer(Tuple.field1.field2):
...     pass
>>> MyContainer(1, 2)
<MyContainer(field1=1, field2=2)>

Why not characteristic ?

Ugly, inconsistent - you don’t own the class:

Lets try this:

>>> import characteristic
>>> @characteristic.attributes(["field1", "field2"])
... class MyContainer(object):
...     def __init__(self, a, b):
...         if a > b:
...             raise ValueError("Expected %s < %s" % (a, b))
>>> MyContainer(1, 2)
Traceback (most recent call last):
    ...
ValueError: Missing value for 'field1'.

WHAT !? Ok, lets write some more code:

>>> MyContainer(field1=1, field2=2)
Traceback (most recent call last):
    ...
TypeError: __init__() ... arguments...

This is banans. You have to write your class around these quirks.

Lets try this:

>>> class MyContainer(Fields.field1.field2):
...     def __init__(self, a, b):
...         if a > b:
...             raise ValueError("Expected %s < %s" % (a, b))
...         super(MyContainer, self).__init__(a, b)

Just like a normal class, works as expected:

>>> MyContainer(1, 2)
<MyContainer(field1=1, field2=2)>

Documentation

https://python-fields.readthedocs.org/

Development

To run the all tests run:

tox

Changelog

0.1.0 (2014-06-08)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fields-0.2.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

fields-0.2.0-py2.py3-none-any.whl (7.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file fields-0.2.0.tar.gz.

File metadata

  • Download URL: fields-0.2.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for fields-0.2.0.tar.gz
Algorithm Hash digest
SHA256 bcf0e9470cb92d0a6760412c0de109d8801fd2379d890c003a623d5d311846f4
MD5 1bae8a918782dfada57686b5ff534879
BLAKE2b-256 651651a70ecf130d4f6f79a54a41e4b42b165223b787de3feb798e8da5ec7c93

See more details on using hashes here.

Provenance

File details

Details for the file fields-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for fields-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f052c0c869a1205d194631fda4a5db42efbdd50fa19b265fc2689e841faf5134
MD5 ab5fd6cdf6a9e0a22b361541cb121aac
BLAKE2b-256 9eb276fbe1abc985218be3521ae85741a218f9d2a9d36aa8f0d8c10686215513

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page