Skip to main content

No project description provided

Project description

Prodot

A new way to deal with dictionaries and lists in python. Treat data like classes, or by reading/writing data with json paths

>>> from prodot import ProObject
>>> my_obj = ProObject({'foo':{'bar':['eggs']}})

>>> my_obj.foo.bar.n1.get_value()
'eggs'

>>> my_obj['$.foo.bar.[0]'].get_value()
'eggs

Getting started

install

pip install prodot

Usage

Import the pro object from the prodot library. You can create a new empty dictionary, or start with a filled one

# No parameters instances an empty dictionary
>>> my_new_obj = ProObject() 

# The pro object can be initialized with a dictionary
>>> my_dict_obj = ProObject({"foo":["bar","eggs"]})

# The pro object can also initialize with a list
>>> my_list_obj = ProObject([ [1,2,3], ["a","b","c"], [{"foo":"bar"}, {"bar":"eggs"}] ])

Dot notation usage

By using the pro-object you can use the dictionary as a class

>>> my_json = {
...     "userData": {
...         "name": "John",
...         "age": "38",
...         "shoppingCart":[
...             {"cellphone": 999.99},
...             {"notebook": 2999.99},
...             {"wireless keyboard": 299.99}
...         ]
...     }
... }

>>> my_new_obj = ProObject(my_json)

>>> shoppingCart = my_new_obj.userData.shoppingCart

The ProObject will return another instance of the ProObject with the main_object attribute as being the selected path.

>>> type(shoppingCart)
<class 'prodot.pro_object.ProObject'>

To get the raw value of the object, you can use the .get_value() function.

>>> shoppingCart.get_value()
[{"cellphone": 999.99}, {"notebook": 2999.99}, {"wireless keyboard": 299.99}]

# or by using the get_value directly at the path
>>> my_new_obj.userData.shoppingCart.get_value()
[{"cellphone": 999.99}, {"notebook": 2999.99}, {"wireless keyboard": 299.99}]

>>> type(shoppingCart.get_value())
<class 'dict'>

You can also add new information to the instancied object

# n3 means list index 3 (will be added as 4th item)
>>> my_new_obj.userData.shoppingCart.n3 = {"monitor": 699.99}
>>> my_new_obj.userData.shoppingCart.get_value()
[{"cellphone": 999.99}, {"notebook": 2999.99}, {"wireless keyboard": 299.99}, {"monitor": 399.99}]

Python doesn't accept list indexes to be used as a class attribute. For solve this problem, list indexes start with the n letter (like n0, n1, ...)

Json path usage

If you prefere or need to use json paths, it is possible to write and retrieve information using json path strings.

>>> my_new_obj['$.userData.shoppingCart[4]'] = {'FunStation 6 Series T':'699,99'}

>>> my_new_obj['$.userData.shoppingCart'].get_value()
[{"cellphone": 999.99}, {"notebook": 2999.99}, {"wireless keyboard": 299.99}, {"monitor": 399.99}, {'FunStation 6 Series T':'699,99'}]

Note that by using a json path string, list indexes must not have n as their first value.

You can view all possible json_paths from object by using the get_all_paths attribute.

>>> my_new_obj.get_all_paths()
<generator object PathObject.all_paths_from_main_object at 0x7f6a012b5c80>

>>> list(my_new_obj.get_all_paths())
['.userData', '.userData.name', '.userData.age', '.userData.shoppingCart', '.userData.shoppingCart[0]', '.userData.shoppingCart[0].cellphone', '.userData.shoppingCart[1]', '.userData.shoppingCart[1].notebook', '.userData.shoppingCart[2]', '.userData.shoppingCart[2].wireless keyboard', '.userData.shoppingCart[3]', '.userData.shoppingCart[3].monitor', '.userData.shoppingCart[4]', '.userData.shoppingCart[4].FunStation 6 Series T']

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prodot-0.2.0.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

prodot-0.2.0-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file prodot-0.2.0.tar.gz.

File metadata

  • Download URL: prodot-0.2.0.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.15.79.1-microsoft-standard-WSL2

File hashes

Hashes for prodot-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8049d1b3c599af28b27fd8a5a3a5b081d248c4db709b1e56164cf65ccc788788
MD5 de557fe7d34b78cd15b8f57aeaa46389
BLAKE2b-256 2acfc4691b616d8eb2b5d6e6b4fd8509d7e2d5167059c68ca9ebe2262de688da

See more details on using hashes here.

File details

Details for the file prodot-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: prodot-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.15.79.1-microsoft-standard-WSL2

File hashes

Hashes for prodot-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9d168bf7f6b2622cdf5526b63a0bb5dcd8f0553e662cde5d2a83c9aaf68ff52c
MD5 f711d85869066d19a940146f98b8cae4
BLAKE2b-256 9a28af3c833ee4364047fa6025d9e6ceb0efc0fa727b8b7adb4c438a27be8d77

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