# itypes
[![Build Status](https://travis-ci.org/PavanTatikonda/itypes.svg?branch=master)](https://travis-ci.org/PavanTatikonda/itypes)
Basic immutable container types for Python.
A simple implementation that's designed for simplicity over performance.
Use these in circumstances where it may result in more comprehensible code,
or when you want to create custom types with restricted, immutable interfaces.
For an alternative implementation designed for performance,
please see [pyrsistent](https://github.com/tobgu/pyrsistent).
### Installation
Install using `pip`:
pip install itypes
### Instantiating dictionaries and lists.
>>> import itypes
>>> d = itypes.Dict({'a': 1, 'b': 2, 'c': 3})
>>> l = itypes.List(['a', 'b', 'c'])
### On instantiation, nested types are coerced to immutables.
>>> d = itypes.Dict({'a': 123, 'b': ['a', 'b', 'c']})
>>> d['b']
List(['a', 'b', 'c'])
### Assignments and deletions return new copies.
Methods: `set(key, value)`, `delete(key)`
>>> d2 = d.set('c', 456)
>>> d2
Dict({'a': 123, 'b': ['a', 'b', 'c'], 'c': 456})
>>> d3 = d2.delete('a')
>>> d3
Dict({'b': ['a', 'b', 'c'], 'c': 456})
### Standard assignments and deletions fail.
>>> d['z'] = 123
TypeError: 'Dict' object doesn't support item assignment
>>> del(d['c'])
TypeError: 'Dict' object doesn't support item deletion
### Nested lookups.
Method: `get_in(keys, default=None)`
>>> d['b'][-1]
'c'
>>> d['b'][5]
IndexError: list index out of range
>>> d.get_in(['b', -1])
'c'
>>> d.get_in(['b', 5])
None
### Nested assignments and deletions.
Methods: `set_in(keys, value)`, `delete_in(keys)`
>>> d2 = d.set_in(['b', 1], 'xxx')
>>> d2
Dict({'a': 123, 'b': ['a', 'xxx', 'c']})
>>> d3 = d2.delete_in(['b', 0])
>>> d3
Dict({'a': 123, 'b': ['xxx', 'c']})
### Equality works against standard types.
>>> d = itypes.Dict({'a': 1, 'b': 2, 'c': 3})
>>> d == {'a': 1, 'b': 2, 'c': 3}
True
### Objects are hashable.
>>> hash(d)
277752239
### Shortcuts for switching between mutable and immutable types.
Functions: `to_mutable(instance)`, `to_immutable(value)`
>>> value = itypes.to_mutable(d)
>>> value
{'a': 123, 'b': ['a', 'b', 'c']}
>>> itypes.to_immutable(value)
Dict({'a': 123, 'b': ['a', 'b', 'c']})
### Subclassing.
Only private attribute names may be set on instances. Use `@property` for attribute access.
Define a `.clone(self, data)` method if objects have additional state.
Example:
class Configuration(itypes.Dict):
def __init__(self, title, *args, **kwargs):
self._title = title
super(Configuration, self).__init__(*args, **kwargs)
@property
def title(self):
return self._title
def clone(self, data):
return Configuration(self._title, data)
Using the custom class:
>>> config = Configuration('worker-process', {'hostname': 'example.com', 'dynos': 4})
>>> config.title
'worker-process'
>>> new = config.set('dynos', 2)
>>> new
Configuration({'dynos': 2, 'hostname': 'example.com'})
>>> new.title
'worker-process'
### Custom immutable objects.
Subclass `itypes.Object` for an object that prevents setting public attributes.
>>> class Custom(itypes.Object):
... pass
Only private attribute names may be set on instances. Use `@property` for attribute access.
>>> class Document(itypes.Object):
... def __init__(self, title, content):
... self._title = title
... self._content = title
... @property
... def title(self):
... return self._title
... @property
... def content(self):
... return self._content
Using immutable objects:
>>> doc = Document(title='Immutability', content='For simplicity')
>>> doc.title
'Immutability'
>>> doc.title = 'Changed'
TypeError: 'Document' object doesn't support property assignment.
Raw data
{
"_id": null,
"home_page": "http://github.com/PavanTatikonda/itypes",
"name": "itypes",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Tom Christie",
"author_email": "tom@tomchristie.com",
"download_url": "https://files.pythonhosted.org/packages/0e/53/764524b3907d0af00523f8794daca181c08ca7cb32ceee25a0754d5e63a5/itypes-1.2.0.tar.gz",
"platform": "",
"description": "# itypes\n\n[![Build Status](https://travis-ci.org/PavanTatikonda/itypes.svg?branch=master)](https://travis-ci.org/PavanTatikonda/itypes)\n\nBasic immutable container types for Python.\n\nA simple implementation that's designed for simplicity over performance.\n\nUse these in circumstances where it may result in more comprehensible code,\nor when you want to create custom types with restricted, immutable interfaces.\n\nFor an alternative implementation designed for performance,\nplease see [pyrsistent](https://github.com/tobgu/pyrsistent).\n\n### Installation\n\nInstall using `pip`:\n\n pip install itypes\n\n### Instantiating dictionaries and lists.\n\n >>> import itypes\n >>> d = itypes.Dict({'a': 1, 'b': 2, 'c': 3})\n >>> l = itypes.List(['a', 'b', 'c'])\n\n### On instantiation, nested types are coerced to immutables.\n\n >>> d = itypes.Dict({'a': 123, 'b': ['a', 'b', 'c']})\n >>> d['b']\n List(['a', 'b', 'c'])\n\n### Assignments and deletions return new copies.\n\nMethods: `set(key, value)`, `delete(key)`\n\n >>> d2 = d.set('c', 456)\n >>> d2\n Dict({'a': 123, 'b': ['a', 'b', 'c'], 'c': 456})\n >>> d3 = d2.delete('a')\n >>> d3\n Dict({'b': ['a', 'b', 'c'], 'c': 456})\n\n### Standard assignments and deletions fail.\n\n >>> d['z'] = 123\n TypeError: 'Dict' object doesn't support item assignment\n >>> del(d['c'])\n TypeError: 'Dict' object doesn't support item deletion\n\n### Nested lookups.\n\nMethod: `get_in(keys, default=None)`\n\n >>> d['b'][-1]\n 'c'\n >>> d['b'][5]\n IndexError: list index out of range\n >>> d.get_in(['b', -1])\n 'c'\n >>> d.get_in(['b', 5])\n None\n\n### Nested assignments and deletions.\n\nMethods: `set_in(keys, value)`, `delete_in(keys)`\n\n >>> d2 = d.set_in(['b', 1], 'xxx')\n >>> d2\n Dict({'a': 123, 'b': ['a', 'xxx', 'c']})\n >>> d3 = d2.delete_in(['b', 0])\n >>> d3\n Dict({'a': 123, 'b': ['xxx', 'c']})\n\n### Equality works against standard types.\n\n >>> d = itypes.Dict({'a': 1, 'b': 2, 'c': 3})\n >>> d == {'a': 1, 'b': 2, 'c': 3}\n True\n\n### Objects are hashable.\n\n >>> hash(d)\n 277752239\n\n### Shortcuts for switching between mutable and immutable types.\n\nFunctions: `to_mutable(instance)`, `to_immutable(value)`\n\n >>> value = itypes.to_mutable(d)\n >>> value\n {'a': 123, 'b': ['a', 'b', 'c']}\n >>> itypes.to_immutable(value)\n Dict({'a': 123, 'b': ['a', 'b', 'c']})\n\n### Subclassing.\n\nOnly private attribute names may be set on instances. Use `@property` for attribute access.\n\nDefine a `.clone(self, data)` method if objects have additional state.\n\nExample:\n\n class Configuration(itypes.Dict):\n def __init__(self, title, *args, **kwargs):\n self._title = title\n super(Configuration, self).__init__(*args, **kwargs)\n\n @property\n def title(self):\n return self._title\n\n def clone(self, data):\n return Configuration(self._title, data)\n\nUsing the custom class:\n\n >>> config = Configuration('worker-process', {'hostname': 'example.com', 'dynos': 4})\n >>> config.title\n 'worker-process'\n >>> new = config.set('dynos', 2)\n >>> new\n Configuration({'dynos': 2, 'hostname': 'example.com'})\n >>> new.title\n 'worker-process'\n\n### Custom immutable objects.\n\nSubclass `itypes.Object` for an object that prevents setting public attributes.\n\n >>> class Custom(itypes.Object):\n ... pass\n\nOnly private attribute names may be set on instances. Use `@property` for attribute access.\n\n >>> class Document(itypes.Object):\n ... def __init__(self, title, content):\n ... self._title = title\n ... self._content = title\n ... @property\n ... def title(self):\n ... return self._title\n ... @property\n ... def content(self):\n ... return self._content\n\nUsing immutable objects:\n\n >>> doc = Document(title='Immutability', content='For simplicity')\n >>> doc.title\n 'Immutability'\n >>> doc.title = 'Changed'\n TypeError: 'Document' object doesn't support property assignment.\n\n\n",
"bugtrack_url": null,
"license": "BSD",
"summary": "Simple immutable types for python.",
"version": "1.2.0",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "c5fe660c8dda870f7e64896c8f324bc4",
"sha256": "03da6872ca89d29aef62773672b2d408f490f80db48b23079a4b194c86dd04c6"
},
"downloads": -1,
"filename": "itypes-1.2.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "c5fe660c8dda870f7e64896c8f324bc4",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 4756,
"upload_time": "2020-04-19T21:50:11",
"upload_time_iso_8601": "2020-04-19T21:50:11.704135Z",
"url": "https://files.pythonhosted.org/packages/3f/bb/3bd99c7cd34d4a123b2903e16da364f6d2078b1c3a3530a8ad105c668104/itypes-1.2.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "8c8e98875d885e87074d071c447cd9c4",
"sha256": "af886f129dea4a2a1e3d36595a2d139589e4dd287f5cab0b40e799ee81570ff1"
},
"downloads": -1,
"filename": "itypes-1.2.0.tar.gz",
"has_sig": false,
"md5_digest": "8c8e98875d885e87074d071c447cd9c4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4355,
"upload_time": "2020-04-19T21:50:13",
"upload_time_iso_8601": "2020-04-19T21:50:13.144495Z",
"url": "https://files.pythonhosted.org/packages/0e/53/764524b3907d0af00523f8794daca181c08ca7cb32ceee25a0754d5e63a5/itypes-1.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2020-04-19 21:50:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "PavanTatikonda",
"github_project": "itypes",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "flake8",
"specs": []
},
{
"name": "pytest",
"specs": []
},
{
"name": "wheel",
"specs": []
}
],
"lcname": "itypes"
}