change python dict and list to act more as javascript objects
Project description
PyBoa
Change Python dict and list recursively, making Data Structures where keys can be accessed by attribut. Add some usefull functions and an object wraper
from boa import boa
# recursively transforme the data into boa:
a = boa({'b': {'c': 2}})
# can be accessed by key or attribute:
assert a.b.c == a['b']['c']
# transforme inserted data into boa too:
a.x = {'y': {'z': 3}}
assert a.x.y.z == 3
# a.x = {...} and a['x'] = {...} will do the same
a['li'] = [{'k': 1}]
assert a.li[0].k == 1
# boa list and dict are instance of list, dict:
assert isinstance(boa([]), list)
assert isinstance(boa({}), dict)
# have a .map .reduce in list:
a = boa({'b': [[1, 2, 3], [1, 2]]})
assert a.b.map(lambda li: li.reduce(lambda x, y: x + y)) == [6, 3]
a['li'] = [{'k': 1}, {'k': 2}]
assert a.li.map(lambda obj: obj.k) == [1, 2]
# can overide dictionary attributes:
d = boa({})
d.keys = 2
d.keys += 1
assert list(d.values()) == [3]
# now, time for boa_wrap_obj:
from boa import boa_wrap_obj
# let have a simple class:
class A:
"""simple doc"""
d = {'key': 'value'}
def fun(self, data):
return data
# if we `boa_wrap_obj` an instance of a class, it will produce boa:
obj = boa_wrap_obj(A())
# keep the class name and all of the class information:
assert obj.__class__.__name__ == 'A'
assert obj.__class__.__doc__ == 'simple doc'
assert obj.__doc__ == 'simple doc'
# the function `fun` return boa:
assert obj.fun({'a': {'b': 4}}).a.b == 4
# and the attribute `d` too:
assert obj.d.key == 'value'
# the class A is not modified:
obj_2 = A()
# no boa here:
import pytest
with pytest.raises(AttributeError):
obj_2.fun({'b': 2}).b
with pytest.raises(AttributeError):
obj_2.d.key
# a little more complicated class
class B:
def get_a(self):
return A()
def get_b(self):
return B()
b = boa_wrap_obj(B())
assert b.get_a().d.key == 'value'
assert b.get_b().get_b().get_a().d.key == 'value'
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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
PyBoa-1.0-py3-none-any.whl
(5.1 kB
view details)
File details
Details for the file PyBoa-1.0-py3-none-any.whl
.
File metadata
- Download URL: PyBoa-1.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | faad2696ca7d39b5fdd66068bafe35b138dae52dc55fc72dbe3ec5c3938ae7df |
|
MD5 | 9c985c630ab8523f74830b4b0b389301 |
|
BLAKE2b-256 | 59af21f752e77230e25f907a554059be9a9d060bcd34f6b5ec52cba0f946d4c1 |