Skip to main content

Pythonic Object Notation - with native objects and path support

Project description

Pyon

Introduction

Pyon (Pythonic JSON) is a Python library which allows you to easily convert native objects into JSON objects.

It also supports filesystem-like path-structure, which allows you to easily construct you JSON objects just the way you like it.

Additionally, it uses recursion in order to also convert every connected object into a usable form.

Prerequisites

  • Python >= 3.2

Installation

The installation via pip is as easy as typing

pip install pyon-lib

If you want to install the newest version manually, you can also do this:

git clone https://github.com/lagmoellertim/pyon.git

cd pyon

python3 setup.py install

Build

git clone https://github.com/lagmoellertim/pyon.git

cd pyon

python3 setup.py sdist bdist_wheel

Usage

Import Pyon

from pyon import PyonObject

Create a basic Object

class Test(PyonObject):
    def __init__(self):
        self.var1 = "Variable 1"
        self.var2 = 5.5

Convert an object into JSON

test = Test()
json = test.dump()

And this is the output:

{'var1': 'Variable 1', 'var2': 5.5}

Write the JSON object to a file

test = Test()
json = test.generate_json(file_object=open("test.json","w+"))

Hide certain variables

Let's say your Test-Class only needs the variable var2 for it's own calculation, but you don't want it to end up in your final JSON object. You can avoid this by adding the prefix "_"

class Test(PyonObject):
    def __init__(self):
        self.var1 = "Variable 1"
        self._var2 = 5.5

test = Test()
json = test.dump()

And this is the output:

{'var1': 'Variable 1'}

Use multiple objects / classes

As an example, I use the concept of a store with products

class Store(PyonObject):
    def __init__(self, store_name):
        self.store_name = store_name
        self.products = [
            Product(0, "Smartphone"),
            Product(1, "Laptop")
        ]

class Product(PyonObject):
    def __init__(self, article_id, name):
        self.article_id = article_id
        self.name = name

store = Store("Generic Store")
json = store.dump()

And this is the output:

{
    'store_name': 'Generic Store',
    'products': 
        [
            {'article_id': 0, 'name': 'Smartphone'},
            {'article_id': 1, 'name': 'Laptop'}
        ]
}

Specifying object paths

This time, the JSON structure should not be based on the class structure, but rather on the path string that we supply.

Path Strings are very similar to your filesystem paths. You can navigate inside the JSON object using these Path Strings.

Here are some example:

path1 = "/store/"

path2 = "/test/../store/./" # Identical to path1 since ../ means one layer up and ./ can be ignored

path3 = "/store/products/*" #The star symbol tells pyon to create a list instead of a dict in this location

Let's modify our Store / Products Class in order for it to use custom paths

class Store(PyonObject):
    def __init__(self, store_name):
        super().__init__("/stores/*")
        self.store_name = store_name
        self.products = [
            Product(0, "Smartphone"),
            Product(1, "Laptop")
        ]

class Product(PyonObject):
    def __init__(self, article_id, name):
        super().__init__("products/*")
        self.article_id = article_id
        self.name = name

store = Store("Generic Store")
json = store.dump()

And this is the output:

{
    'stores':
    [
        {'store_name': 'Generic Store', 'products': 
            [
                {'article_id': 0, 'name': 'Smartphone'},
                {'article_id': 1, 'name': 'Laptop'}
            ]
        }
    ]
}

As you can see in the example above, relative paths are also possible as long as the object has a parent.

Use object variables for the Path String

To use object variables inside of you Path String, simply specify them in the String using brackets like this: {var1}

The value of {var1} is self.var1 , so you just leave the 'self.' away.

class Test(PyonObject):
    def __init__(self):
        super().__init__("/test/{var1}-{_var2}/")
        self.var1 = "Variable 1"
        self._var2 = 5.5

test = Test()
json = test.dump()

And this is the output:

{
    'test':
    {
        'Variable 1-5.5': {'var1': 'Variable 1'}
    }
}

Different dump methods

Pyon supports different methods for dumping the PyonObject. You can use the normal python dictionary ('json'), 'xml' or 'yaml'. Here are some usage examples:

json = test.dump(output_format="json") # JSON is default, so you can also leave it empty

xml = test.dump(output_format="xml")

yaml = test.dump(output_format="yaml")

Allow Overwrite

Finally, there is Overwrite Protection. Since Paths allow you to freely choose the location where the object should end up, it is possible for them to overlap. To allow / stop overwriting, you can do this:

json = test.dump(allow_overwrite=True)

The default value for allow_overwrite is False

Documentation

If you get stuck at some point while trying to use the API, take a look the code. It is fully commented and well-labeled, which should help you understand what's going on.

Contributing

If you are missing a feature or have new idea, go for it! That is what open-source is for!

Author

Tim-Luca Lagmöller (@lagmoellertim)

Donate

You can also contribute by buying me a coffee.

License

MIT License

Copyright © 2019-present, Tim-Luca Lagmöller

Have fun :tada:

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

pyon-lib-0.1.3.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyon_lib-0.1.3-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file pyon-lib-0.1.3.tar.gz.

File metadata

  • Download URL: pyon-lib-0.1.3.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for pyon-lib-0.1.3.tar.gz
Algorithm Hash digest
SHA256 233167f0f00ed7fd25d3b24a89d260be6212a668b98df422e249c99f76866200
MD5 31c8e75fd6dc882819e802bc493a06b7
BLAKE2b-256 db1c326a7b5a1b72398d83f41a5ac55665cf6dc68a647e9f8f3f925fa35ae867

See more details on using hashes here.

File details

Details for the file pyon_lib-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pyon_lib-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for pyon_lib-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 000e852eb9b1dbdf7c0478fbc9ed78a9e8a1ec83731c422db2054fe54a32550d
MD5 489a1a24d51a3371c41434a67842cc19
BLAKE2b-256 27cd639116079dc1ce27c8db7181296587bbb1ce884f2324269c37daae0f6dd7

See more details on using hashes here.

Supported by

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