A Better Dictionary Class
Project description
######
Encase
######
.. image:: https://travis-ci.org/RyanMillerC/encase.svg?branch=master
:target: https://travis-ci.org/RyanMillerC/encase
An extension of the Python built-in Dictionary class for working with large
datasets.
Features
========
The Encase class is used to create dictionary objects with:
* Attribute access via dot (.) notation
* Nested instances
* Recursive conversion for existing dictionaries, including nested lists of
dictionaries
This allows key/value pairs to be accessed like instance attributes, which
makes code dealing with APIs that return large amounts of JSON easier to read
and write. Instead of writing,
``relevant_data = api_result['data']['subset']['subsubset']``
you can accomplish the same with,
``relevant_data = api_result.data.subset.subsubset``.
Installation
============
Install this package using pip:
``pip install encase``
Documentation
=============
Encase is an extension of the Python dictionary class and therefore inherits
all attributes and methods of regular dictionary objects. This also means that
an Encase instance can be used as a drop in replacement for a dict object.
Usage
-----
Basic Usage
::
>>> from encase import Encase
>>> d = Encase()
>>> d.hello_world = "Hello World!"
>>> print(d.hello_world)
Hello World!
Instances can also be nested
::
>>> d.child = Encase()
>>> d.child.message = "Encase Instances can be nested"
>>> print(d.child.message)
Encase Instances can be nested
Methods
-------
get(key)
""""""""
Return value of attribute at 'key'. This is the method form of using,
``encase.child``. This can be used when you won't know a key name prior and
need to use a variable for 'key'.
``param str key``
Name of attribute whose value will be returned
set(key, value)
"""""""""""""""
Set value of attribute at 'key'. This is the method form of using,
``encase.child = 'Value'``. This can be used when you won't know key name prior
and need to use a variable for 'key'.
``param str key``
Name of attribute to set
``param str value``
Value to set for attribute
Examples
========
Create a JSON File from Encase instance:
::
>>> import json
>>> j = Encase()
>>> j.data = Encase()
>>> j.data.info = "JSON can be converted into nested Encases"
>>> j.data.features = []
>>> j.data.features.append('Recursively transform dictionaries')
>>> j.data.features.append('Supports recursion through lists as well')
>>> j.data.features.append(Encase())
>>> j.data.features[2].example = "Example of a Encase in a list"
>>> print(j)
{
'data': {
'info': 'JSON can be converted into nested Encases',
'features': [
'Resursively transform dictionaries into Encases',
'Supports recursion through lists as well',
{
'example': 'Example of a Encase in a list'
}
]
}
}
>>> with open('example.json', 'w') as stream:
... json.dump(j, stream)
Read a JSON File into Encase instance:
::
>>> with open('example.json', 'r') as stream:
... j_data = json.load(stream)
>>> j_stack = Encase(j_data)
>>> print(j_stack.data.info)
JSON can be converted into nested Encases
>>> for item in j_stack.data.features:
... print(item)
...
Resursively transform dictionaries into Encases
Supports recursion through lists as well
{'example': 'Example of a Encase in a list'}
>>> print(j_stack.data.features[2].example)
Example of a Encase in a list
Author
======
Ryan Miller - ryan@devopsmachine.com
Encase
######
.. image:: https://travis-ci.org/RyanMillerC/encase.svg?branch=master
:target: https://travis-ci.org/RyanMillerC/encase
An extension of the Python built-in Dictionary class for working with large
datasets.
Features
========
The Encase class is used to create dictionary objects with:
* Attribute access via dot (.) notation
* Nested instances
* Recursive conversion for existing dictionaries, including nested lists of
dictionaries
This allows key/value pairs to be accessed like instance attributes, which
makes code dealing with APIs that return large amounts of JSON easier to read
and write. Instead of writing,
``relevant_data = api_result['data']['subset']['subsubset']``
you can accomplish the same with,
``relevant_data = api_result.data.subset.subsubset``.
Installation
============
Install this package using pip:
``pip install encase``
Documentation
=============
Encase is an extension of the Python dictionary class and therefore inherits
all attributes and methods of regular dictionary objects. This also means that
an Encase instance can be used as a drop in replacement for a dict object.
Usage
-----
Basic Usage
::
>>> from encase import Encase
>>> d = Encase()
>>> d.hello_world = "Hello World!"
>>> print(d.hello_world)
Hello World!
Instances can also be nested
::
>>> d.child = Encase()
>>> d.child.message = "Encase Instances can be nested"
>>> print(d.child.message)
Encase Instances can be nested
Methods
-------
get(key)
""""""""
Return value of attribute at 'key'. This is the method form of using,
``encase.child``. This can be used when you won't know a key name prior and
need to use a variable for 'key'.
``param str key``
Name of attribute whose value will be returned
set(key, value)
"""""""""""""""
Set value of attribute at 'key'. This is the method form of using,
``encase.child = 'Value'``. This can be used when you won't know key name prior
and need to use a variable for 'key'.
``param str key``
Name of attribute to set
``param str value``
Value to set for attribute
Examples
========
Create a JSON File from Encase instance:
::
>>> import json
>>> j = Encase()
>>> j.data = Encase()
>>> j.data.info = "JSON can be converted into nested Encases"
>>> j.data.features = []
>>> j.data.features.append('Recursively transform dictionaries')
>>> j.data.features.append('Supports recursion through lists as well')
>>> j.data.features.append(Encase())
>>> j.data.features[2].example = "Example of a Encase in a list"
>>> print(j)
{
'data': {
'info': 'JSON can be converted into nested Encases',
'features': [
'Resursively transform dictionaries into Encases',
'Supports recursion through lists as well',
{
'example': 'Example of a Encase in a list'
}
]
}
}
>>> with open('example.json', 'w') as stream:
... json.dump(j, stream)
Read a JSON File into Encase instance:
::
>>> with open('example.json', 'r') as stream:
... j_data = json.load(stream)
>>> j_stack = Encase(j_data)
>>> print(j_stack.data.info)
JSON can be converted into nested Encases
>>> for item in j_stack.data.features:
... print(item)
...
Resursively transform dictionaries into Encases
Supports recursion through lists as well
{'example': 'Example of a Encase in a list'}
>>> print(j_stack.data.features[2].example)
Example of a Encase in a list
Author
======
Ryan Miller - ryan@devopsmachine.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
encase-1.1.tar.gz
(4.1 kB
view details)
File details
Details for the file encase-1.1.tar.gz
.
File metadata
- Download URL: encase-1.1.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35d338f63d102e4ac0c9caa458004054d6772856dcea0706033d25fbb940d791 |
|
MD5 | bf833c22afa40b28201928c94b6e841a |
|
BLAKE2b-256 | eadea91fca6d42ce1d4c95723eba90379763c6b3c6c8330f247740f0c5bbd917 |