Skip to main content
######
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

Metadata

Release files for encase 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for encase 1.1
File Size Uploaded
encase-1.1.tar.gz 4.1 kB Details

Release files / encase-1.1.tar.gz

Download URL encase-1.1.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
35d338f63d102e4ac0c9caa458004054d6772856dcea0706033d25fbb940d791
BLAKE2b-256 checksum
How to use checksums
eadea91fca6d42ce1d4c95723eba90379763c6b3c6c8330f247740f0c5bbd917
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.1 This release

1 release file

1.0.2

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page