vcontext package provides single object Context. Context is dictionary like datastructure with custom access to items. You can access your data by dot access. Context does not wrap data in itself, rather just access them. Context has data attribute where data is stored.
Install
vcontext is on pypi so you can simply:
pip install vcontext
__getitem__ vs __getattribute__
For Context I have decided to use __getitem__ approach, since I wanted to have consistent dot access also to lists/tuples. This would be impossible using __getattr__.
Item
Item is dot-separated path to value. This item is splitted and values have following rules * string - access to dictionary item or object attribute * integer - access to list item * first part must be string (since Context is a dictionary)
Example:
context = Context({
'status': 200,
'message': 'OK',
'result': [
{
'user': {
'username': 'phonkee',
'name': 'Peter Vrba'
}
}
]
})
assert context['result.0.user.username'] == 'phonkee'
assert context['status'] == 200
If the data is not found, KeyError is raised on part of item that was not found. Context provides get method where you can specify default value if value is not found and that will never raise exception.
Build items:
Context would be useless if it only supported get of values. Context has support also to create/delete values on underlying datas by given item. Lets build a structure from previous example:
context = Context()
context['status'] = 200
context['message'] = 'OK'
context['result.0.user.username'] = 'phonkee'
context['result.0.user.name'] = 'Peter Vrba'
assert context.data == {
'status': 200,
'message': 'OK',
'result': [
{
'user': {
'username': 'phonkee',
'name': 'Peter Vrba'
}
}
]
}
Now we try to delete item
del context['result.0']
assert len(context['result']) == 0
Isn’t that cute little helper?
.keys(item=None):
Context also supports keys method. By calling this method context traverses recursively object. It has support for dict/list, for custom object it returns just the object key.
context = Context()
context['hello.world'] = 'yay'
assert context.keys() == ['hello.world']
api:
Context provides following methods:
.copy() - deepcopies data and returns new context
.dumps(item=None) - dump to json, attributes:
item - item to be dumped to json
.items(**kwargs) - list of key value items (tuple key, value), **kwargs passed to keys method
.iteritems(**kwargs) - generator version of items, **kwargs passed to keys method
keys(item=None) - returns list of all keys, attributes:
item - item to be dumped to json
Contribute:
Contributions are welcome, there are still a lot of parts to be enhanced.
TODO:
add support for special list key append so we can append to list. e.g:
context['result.usernames.__append__'] = 'phonkee'
Metadata
Release files for vcontext 0.23
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| vcontext-0.23.tar.gz | 9.6 kB | Without provenance | Details |
Release files / vcontext-0.23.tar.gz
| Download URL | vcontext-0.23.tar.gz |
|---|---|
| Size | 9.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6823f74826327cd5593a29ded74c76a70f11feb937f62e0841d61c08842e62e9
|
|
BLAKE2b-256 checksum How to use checksums |
dd41017447d076dd401302dc50a91d28d5b3ed3d78cc269bbe0924c6a3fde848
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.5.2
|
Security for vcontext 0.23
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.