Skip to content

jonnekaunisto/pyser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pyser

PySer page on the Python Package Index

PySer(full documentation) is a library for serializing and deserializing data in different data formats through intuitive mappings in defined inside a Python class.

Current formats supported are JSON and config files, with more coming later on.

Examples

Class mappings for serializing and deserializing in JSON

from pyser import SchemaJSON, SerField, DeserField
class FruitBasketSchema(SchemaJSON):
    def __init__(self):
        self.name           = DeserField()
        self.fruit          = DeserField()
        self.iD             = DeserField(name='ref', kind=int)
        self.intString      = DeserField(kind=int)
        self.optionalString = DeserField(kind=str, optional=True)
        self.items          = DeserField(repeated=True)

        self.name           = SerField()
        self.fruit          = SerField()
        self.iD             = SerField(name='ref', kind=int)
        self.intString      = SerField(kind=int)
        self.optionalString = SerField(optional=True)
        self.items          = SerField(repeated=True)
        self.register       = SerField(parent_keys=['checkout'], kind=int)
        self.amount         = SerField(parent_keys=['checkout'], kind=int)


fruit_basket_schema = FruitBasketSchema()


class FruitBasket():
    def __init__(self):
        self.name           = 'basket'
        self.fruit          = 'banana'
        self.iD             = '123'
        self.intString      = '12345'
        self.optionalString = None
        self.items          = ['paper', 'rock']
        self.register       = '1'
        self.amount         = '10'

Serializing to a JSON file

basket = FruitBasket()
fruit_basket_schema.to_json(basket, filename="basket.json")

File contents of basket.json after serializing:

{
    "name": "basket",
    "fruit": "banana",
    "ref": 123,
    "intString": 12345,
    "items": [
        "paper",
        "rock"
    ],
    "checkout": {
        "register": 1,
        "amount": 10
    }
}

Similarly deserialization from a json file:

basket = FruitBasket()
fruit_basket_schema.from_json(basket, raw_json=raw_json)

Installation

Installation by hand: you can download the source files from PyPi or Github:

python setup.py install

Installation with pip: make sure that you have pip installed, type this in a terminal:

pip install pyser

Documentation

Running build_docs has additional dependencies that require installation.

pip install pyser[docs]

The documentation can be generated and viewed via:

$ python setup.py build_docs

You can pass additional arguments to the documentation build, such as clean build:

$ python setup.py build_docs -E

More information is available from the Sphinx documentation.

Running Tests

Run the python command

python setup.py test

Contribute

  1. Fork the repository from Github
  2. Clone your fork
git clone https://github.com/yourname/pyser.git
  1. Add the main repository as a remote
git remote add upstream https://github.com/jonnekaunisto/pyser.git
  1. Create a pull request and follow the guidelines

Maintainers