
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
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.
Install using pip
:
pip install itypes
>>> import itypes
>>> d = itypes.Dict({'a': 1, 'b': 2, 'c': 3})
>>> l = itypes.List(['a', 'b', 'c'])
>>> d = itypes.Dict({'a': 123, 'b': ['a', 'b', 'c']})
>>> d['b']
List(['a', 'b', 'c'])
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})
>>> d['z'] = 123
TypeError: 'Dict' object doesn't support item assignment
>>> del(d['c'])
TypeError: 'Dict' object doesn't support item deletion
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
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']})
>>> d = itypes.Dict({'a': 1, 'b': 2, 'c': 3})
>>> d == {'a': 1, 'b': 2, 'c': 3}
True
>>> hash(d)
277752239
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']})
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'
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.
FAQs
Simple immutable types for python.
We found that itypes demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.