Git Product home page Git Product logo

py-serializable's Introduction

Python object serializer

Python serializer class (can be extended by other classes).

Thanks to the Serializable class, each object can be serialized. I created it because I like saving my objects in key->value storage systems. In this way, you can create a class that extends this one in order to obtain a serializable object.

##Define a serializable object

To define a new serializable object you only need to specify its params and extend the Serializable object. Params can be a list of strings or a list of tuples (param_name, type).

from serializable import Serializable

class Photo(Serializable):
	params = [
		('service_id', str), 
		('id', int), 
		('tags', list), 
		('title', str)
	]

	def ...

##Instantiate and serialize an object Create a new object is simple. After you created its instance, you can save it into the database as text using the json_me() method. You can recreate the same object from text using its constructor.

This is an example:

from serializable import Serializable
from ... import Photo

photo = Photo(
	service_id='flickr', 
	id=12131, 
	tags=['#happy', '#smile'], 
	title="Happiness"
)

exported_json = photo.json_me()
#exported_json = '{"tags": ["#happy", "#smile"], "id": 12131, "service_id": "flickr", "title": "Happiness"}'

##Deserialize - instantiate an object from the exported text

import json
from ... import Photo

exported_json = '{"tags": ["#happy", "#smile"], "id": 12131, "service_id": "flickr", "title": "Happiness"}'
params = json.loads(exported_json)

photo = Photo(**params)

##Attributes You can easily access the object attributes.

from ... import Photo

photo = Photo(
	service_id='flickr', 
	id=12131, 
	tags=['#happy', '#smile'], 
	title="Happiness"
)

print(photo.service_id)

##License

Released under the MIT license.

py-serializable's People

Contributors

ilkamo avatar

Stargazers

Giancarlo Mennillo avatar Michael Herman avatar Chew Chit Siang avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.