Skip to main content

This project provides a few enhanced types for Python:

  • A “constrained set” (ordered or not)

  • That’s all for now.

It also provides extensions for Django (1.7 - 1.10).

It has been fully tested with all versions of Python from 2.7 to 3.4; and is distributed under the BSD license.

Getting started

Intall the package from PyPI, using pip:

$ pip install extypes

Or from GitHub:

$ git clone git://github.com/rbarrois/extypes
$ cd extypes
$ python setup.py install

To check that everything went fine, fire a Python shell and import extypes:

import extypes

Introduction

extypes provides a new type, ConstrainedSet.

This is a set()-like object, but values can only be taken from a specific set of options.

A ConstrainedSet is declared in a manner very similar to collections.namedtuple:

import extypes
Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])

This will declare a new class, Foods, whose instances are ConstrainedSet that only accept options among 'eggs', 'spam' and 'bacon'.

Those objects can be used as simple set() objects:

>>> import extypes
>>> Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
>>> meat = Foods(['spam', 'bacon'])
>>> fresh = Foods(['bacon', 'eggs'])
>>> 'eggs' in meat
False
>>> 'eggs' in fresh
True
>>> meat & fresh
Foods(['bacon'])

As a set() object, they are mutable:

>>> import extypes
>>> Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
>>> meat = Foods(['spam', 'bacon'])
>>> meat.remove('spam')
>>> meat
Foods(['bacon'])

And iterable:

>>> import extypes
>>> Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
>>> meat = Foods(['bacon', 'spam'])
>>> list(meat)
['spam', 'bacon']

But only valid options are accepted:

>>> Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
>>> greens = Foods(['spinach']
Traceback (most recent call last):
...
ValueError: Invalid keys ['spinach'], please use a value in ['spam', 'bacon', 'eggs'].

Extensions: Django

extypes also provides custom fields for Django - compatible with Django 1.7 and upwards.

from django.db import models
import extypes
import extypes.django

Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])

class Fridge(models.Model):
    contents = extypes.django.SetField(choices=Foods)

This field will simply behave as a simple ConstrainedSet.

>>> fridge = Fridge(contents=['bacon'])
>>> fridge.contents.add('eggs')
>>> fridge.save()

It is displayed in forms as a multiple choice field. In the database, it is saved as a |-separated list of enabled values (in the above example, the field is stored as |eggs|bacon|).

Metadata

Release files for extypes 2.0.0

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

Source distribution (sdist)

Source distribution for extypes 2.0.0
File Size Uploaded
extypes-2.0.0.tar.gz 11.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for extypes 2.0.0
File Interpreter ABI Platform
extypes-2.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 18.3 kB

Release files / extypes-2.0.0.tar.gz

Download URL extypes-2.0.0.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
10bf455305c7e4a54bd9a5fe04a148fa2b6e33d1a7c3ce937d239fb1907f34b0
BLAKE2b-256 checksum
How to use checksums
7f7b4ecf5dd02431375d4906ae58cd9bd5af1d1e6a5239f6c44fd65aec5d99e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.6

Release files / extypes-2.0.0-py2.py3-none-any.whl

Download URL extypes-2.0.0-py2.py3-none-any.whl
Size 6.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
a58468eee18e1608bff86d28fbe900b6a1af210eea026003ab7b41b1874dcba0
BLAKE2b-256 checksum
How to use checksums
b5fc9fcaca281c7c90520656f8fdbcb1a6efa65c9a53c46385ec233a0458ae32
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.6

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.2.3

2 release files

0.2.2

1 release file

0.2.1

2 release files

0.2.0

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