Object utils to make life easier
Project description
Basic usage
>>> from sloot.object import dictobj
>>> d = dictobj({'a': 'a', 'b': 'b'}, c='c')
>>> print(d)
dictobj({'a': 'a', 'b': 'b', 'c': 'c'})
>>> d.a
'a'
>>> d['a']
'a'
>>> d.a = 3
>>> d.a
3
>>> d['a'] = 42
>>> d.a
42
>>> print(d)
dictobj({'a': 42, 'c': 'c', 'b': 'b'})
>>> print(dict(d))
{'a': 42, 'c': 'c', 'b': 'b'}
Behavior of setattr in inherited objects
>>> class T(dictobj): ... classvar = 'classvar' ... def f(self): ... return 'f' ... @property ... def prop(self): ... return getattr(self, '_prop', 'prop') ... @prop.setter ... def prop(self, value): ... self._prop = value ...
methods and class attributes are not overwritten and go to the dict:
>>> t = T({'classvar': 1, 'f': 2, 'prop': 3}) >>> t.classvar # access the class attribute 'classvar' >>> t['classvar'] # access the dict entry 1 >>> t.classvar = 5 # we don't overwrite class attributes, this goes to dict >>> t.classvar # this is the class attribute 'classvar' >>> t['classvar'] 5 >>> t.f # access the class method <bound method T.f of T({'classvar': 1, 'f': 2, 'prop': 3})> >>> t['f'] # access the dict entry 2 >>> t.f = 4 # we don't overwrite the method, this goes to the dict >>> t.f <bound method T.f of T({'classvar': 1, 'f': 2, 'prop': 3})> >>> t['f'] 4properties getter and setter are used:
>>> t.prop # get the property 'prop' >>> t['prop'] # get the dict entry 3 >>> t.prop = 'newprop' # use property setter >>> t.prop 'newprop' >>> t['prop'] 3
otherwise the dict is updated:
>>> t.a = 42 >>> t['a'] 42 >>> t.a 42
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
sloot.object-1.5.1.tar.gz
(4.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sloot.object-1.5.1.tar.gz.
File metadata
- Download URL: sloot.object-1.5.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/2.7.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b792e11aa91b9c3f392d4a2c0ee1dbcc59b2199e74055ffac904dcf25039194
|
|
| MD5 |
3fa8025fee7cc066c081e35df53f8cdd
|
|
| BLAKE2b-256 |
8b5970fadaa5a01d47e47b57539786cf26a9fc149b20ea8f61e4a03d468cac80
|
File details
Details for the file sloot.object-1.5.1-py2.py3-none-any.whl.
File metadata
- Download URL: sloot.object-1.5.1-py2.py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/2.7.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69380a519b903b6b95bda511eb8221ed3f9c9e59f35ee343b5ddf5bd06fa0c44
|
|
| MD5 |
0a361439362ea426fc05efe80dca5056
|
|
| BLAKE2b-256 |
fe9701f90e068def0d4d787c3dd4c272f21d1f656b98f09dfb2b42897eb59033
|