Skip to main content

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 BoaWraps:
from boa import BoaWraps


# let have a simple class:
class A:
    """simple doc"""
    d = {'key': 'value'}

    def fun(self, data):
        return data

# if we `BoaWraps` an instance of a class, it will produce boa:
obj = BoaWraps(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 = BoaWraps(B())
assert b.get_a().d.key == 'value'
assert b.get_b().get_b().get_a().d.key == 'value'

Project details


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.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file PyBoa-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: PyBoa-1.0.1-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

Hashes for PyBoa-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b4376decfd08cb3b5e20966bd00c640383c4773d66da710ab5eede0f2591d508
MD5 b31f555bdf7504359bd5e18ab3ccc18a
BLAKE2b-256 caae5c19bcff02aa6ca1ce02116884291249534bfc4ed212359c114d2b1e1491

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page