Skip to main content

Flexible property bag functions

Project description

propertybag

Flexible property bag class

    import propertybag as pb

    bag = pb.Bag({'a':'b'}, c='d')
    print(bag)                      # > {"a": "b", "c": "d"}

    bag.b.c.d.e = 42
    print(bag.b.c.d.e)              # > 42
    print(bag.toJson())             # > {"a": "b", "c": "d", "b": {"c": {"d": {"e": 42}}}}

    # Returns a dict
    bag.b.c['d']

    # Returns a Bag() object
    bag.b.c.d

Table of contents

 


Install

$ pip3 install propertybag

 


Examples

    import propertybag as pb

    Log = print

    # Init with kw args
    bag = pb.Bag(a='b', c='d')
    Log(bag.toJson(True))                   # > {
                                            # >   "a": "b",
                                            # >   "c": "d"
                                            # > }

    # Init with dict
    bag = pb.Bag({'a':'b', 'c':'d'})
    Log(bag.toJson())                       # > {"a": "b", "c": "d"}

    # Init with both
    bag = pb.Bag({'a':'b'}, c='d')
    Log(bag)                                # > {"a": "b", "c": "d"}

    # Set deep value
    bag.d.e.f.g.h = 42
    Log(bag)                                # > {"a": "b", "c": "d", "d": {"e": {"f": {"g": {"h": 42}}}}}

    # Read deep value
    Log(bag.d.e.f.g.h)                      # > 42
    try:
        Log(bag.d.e.f.g.i)                  # > Throws ValueError() exception
    except ValueError as e:
        Log(e)                              # > Value not set

    # If you don't want exceptions thrown when value doesn't exist, provide a default
    bag_nt = pb.Bag({'a': 'b'}, '')
    Log(bag_nt.d.e.f.g.i)                   # > ''

    # Use get/set for compound keys
    bag.set("d.e.f.g.h", 43)
    Log(bag.get("d.e.f.g.h"))               # > 43
    Log(bag.get("d.e.f.g.i", "default"))    # > "default"

    # Iterate
    Log('--------------------------------------------')
    for k,v in bag.items():
        Log(k, v)

    Log('--------------------------------------------')
    for k in bag:
        Log(k)

    Log('--------------------------------------------')
    for v in bag.values():
        Log(v)

 


References

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

propertybag-0.1.4-py2.py3-none-any.whl (5.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file propertybag-0.1.4-py2.py3-none-any.whl.

File metadata

  • Download URL: propertybag-0.1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.5

File hashes

Hashes for propertybag-0.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 274aeffd3577b221d22d29d00ddddcc727619d2b82d50a8b65313be285a86eaa
MD5 5d1f4b6ada29e90dd45da2ef9e79c832
BLAKE2b-256 2206fd850c3cff9dd6bab1d198ad525f6ba25684a5cae5d1bb4dce1cb21a9a1c

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