Enhancements over Python's standard types
Project description
extypes
=======
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.
Links
-----
* Package on PyPI: http://pypi.python.org/pypi/extypes
* Repository and issues on GitHub: http://github.com/rbarrois/extypes
* Doc on https://extypes.readthedocs.io/ (not available yet)
Getting started
---------------
Intall the package from PyPI, using pip:
.. code-block:: sh
$ pip install extypes
Or from GitHub:
.. code-block:: sh
$ 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``:
.. code-block:: python
import extypes
Introduction
------------
.. currentmodule:: extypes
``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``:
.. code-block:: python
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:
.. code-block:: pycon
>>> 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:
.. code-block:: pycon
>>> import extypes
>>> Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
>>> meat = Foods(['spam', 'bacon'])
>>> meat.remove('spam')
>>> meat
Foods(['bacon'])
And iterable:
.. code-block:: pycon
>>> import extypes
>>> Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
>>> meat = Foods(['bacon', 'spam'])
>>> list(meat)
['spam', 'bacon']
But only valid options are accepted:
.. code-block:: pycon
>>> 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
------------------
.. currentmodule:: extypes.django
``extypes`` also provides custom fields for Django - compatible with Django 1.7 and upwards.
.. code-block:: python
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``.
.. code-block:: pycon
>>> 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|``).
.. note:: ``extypes.django.SetField`` can also receive a choice-like list:
.. code-block:: python
class Fridge(models.Model):
contents = extypes.django.SetField(choices=[('eggs', "Eggs"), ('spam', "Spam"), ('bacon', "Yummy bacon")])
In that case, the field definition is available at ``Fridge.contents.set_definition``:
.. code-block:: pycon
>>> f = Fridge(contents=Fridge.contents.set_definition(['eggs', 'spam']))
>>> f.get_contents_display()
"Eggs, Spam"
=======
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.
Links
-----
* Package on PyPI: http://pypi.python.org/pypi/extypes
* Repository and issues on GitHub: http://github.com/rbarrois/extypes
* Doc on https://extypes.readthedocs.io/ (not available yet)
Getting started
---------------
Intall the package from PyPI, using pip:
.. code-block:: sh
$ pip install extypes
Or from GitHub:
.. code-block:: sh
$ 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``:
.. code-block:: python
import extypes
Introduction
------------
.. currentmodule:: extypes
``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``:
.. code-block:: python
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:
.. code-block:: pycon
>>> 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:
.. code-block:: pycon
>>> import extypes
>>> Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
>>> meat = Foods(['spam', 'bacon'])
>>> meat.remove('spam')
>>> meat
Foods(['bacon'])
And iterable:
.. code-block:: pycon
>>> import extypes
>>> Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
>>> meat = Foods(['bacon', 'spam'])
>>> list(meat)
['spam', 'bacon']
But only valid options are accepted:
.. code-block:: pycon
>>> 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
------------------
.. currentmodule:: extypes.django
``extypes`` also provides custom fields for Django - compatible with Django 1.7 and upwards.
.. code-block:: python
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``.
.. code-block:: pycon
>>> 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|``).
.. note:: ``extypes.django.SetField`` can also receive a choice-like list:
.. code-block:: python
class Fridge(models.Model):
contents = extypes.django.SetField(choices=[('eggs', "Eggs"), ('spam', "Spam"), ('bacon', "Yummy bacon")])
In that case, the field definition is available at ``Fridge.contents.set_definition``:
.. code-block:: pycon
>>> f = Fridge(contents=Fridge.contents.set_definition(['eggs', 'spam']))
>>> f.get_contents_display()
"Eggs, Spam"
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
extypes-1.0.0.tar.gz
(5.8 kB
view details)
Built Distribution
File details
Details for the file extypes-1.0.0.tar.gz
.
File metadata
- Download URL: extypes-1.0.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd94a327aacbca0f31bf963364183b966f35f3c1ef9823be61e993bcc1271e2f |
|
MD5 | ca6e8533325f47364f2a5c793b040ca3 |
|
BLAKE2b-256 | ab3630aeb1af739faedc546f74464e296fe6d9cdaf4d08944d938317bb315bba |
File details
Details for the file extypes-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: extypes-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbd4598a67df242bd14c0cd8e11d736fc9a6d09730fd7401a9dfbe0b91d5a40f |
|
MD5 | 0c7782754352042d8c45be8c926c8fad |
|
BLAKE2b-256 | 3a95de9af3f5ac9067b043e6bd3311eab34d6338a6537bc5d964fef1b75ad265 |