Skip to main content

Load your classes with json and save it with json or work with python dictionary's

Project description

Clason


License: MIT Python - ^3.10 Downloads

Load your classes with json and save it with json or work with python dictionary's

For an example usage look in the folder example

Install


Install the package with pip pip install clason

Install with pip + github pip install git+https://github.com/princessmiku/Clason

Basic Setup


from clason import Clason

class Student(Clason):
    ...

Example Class

class Student(Clason):
    name: str
    age: int
    address: Address
    email: str = None

How to use?


Set a variable with type

name: str, age: int

Supported Types: str, int, list, dict, datetime, float, Clason class

Note: time is a float, current_time: float = time.time()

Values without type are not possible

Set a variable with default value

name: str = "Anna", age: int = 22, date: datetime.datetime = datetime.datetime.now()

if you set a default value, you need only to define a normale variable

Work with List

list: list, list: list[str], list: list[class]

if you set a list, you can define a subtype, as subtype you can use alle regular supported types.

Note: If you not define a subtype, it could be make problems with datetime and a clason classes because they are handled differently

Work with Dictionary

dict: dict[str, any], dict[str, str]

if you set a list, you can define a subtype, as subtype you can use alle regular supported types. if you not want to define a subtype you need to write any, else it could make problems

Work with a Clason class

myClass: ClassWithClasson

It's possible to set a type as a Clason class, if you have another class that extends Clason, for example a Address class. Clason.

Clason will then initalize these in the same way as all other clason classes. This is also possible to inherit more often

Functions


On Class initialization

It is possible to write your own init method during the initialization of a class, before and after.

Setup

from clason import Clason
class Student(Clason):
    name: str

    def clason_on_init_start(self):
        # starts before the loading process
        ... # your code
    def clason_on_init_end(self):
        # starts at the end of the loading process
        ... # your code

Class loading

If you want to load your json file or a python dictionary do this

Load - Single

Load a single json/python dictionary

from clason import Clason
class Student(Clason):
    name: str

anna = Student.clason_load("anna.json") # load from a json file

anna = Student.clason_loads("...") # load from a json string

anna = Student.clason_load_d({...}) # load from a python dictionary

Load - Multi

Load a list of data from a json/python dictionary

A multi load function need a target class

Its return a list with the loaded target class

from clason import Clason, load_many, loads_many, load_many_d

class Student(Clason):
    name: str

students = load_many('students.json', Student)  # load from a json file

students = loads_many("...", Student)  # load from a json string

students = load_many_d([{},...], Student) # load from a python dictionary

Class saving

If you want to save your class in a json file or a python dictionary do this

Indent is optional, try it out what it does, it's except an integer

Save - Single

Save a single class in a json/python dictionary

from clason import Clason
class Student(Clason):
    name: str
    
    def __init__(self, name):
        self.name = name
anna = Student("Anna")
anna.clason_dump("anna.json", indent=2) # save in a json file
anna_str = anna.clason_dumps(indent=2) # save in a json string, it return a string

anna_dict = anna.clason_dump_d() # save in a python dictionary, it returns a python dictionary

Save - Multi

Save a list of classes in a json/python dictionary

A multi save need a list with the classes

from clason import Clason, dump_many, dumps_many, dump_many_d


dump_many('students.json', [anna, ...], indent=2) # save in a json file

students_str = dumps_many([anna, ...], indent=2)  # save in a json string, it return a string

students_dict = dump_many_d([anna, ...]) # save in a python dictionary, it returns a list with python dictionarys

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

Clason-1.0.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

Clason-1.0.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file Clason-1.0.1.tar.gz.

File metadata

  • Download URL: Clason-1.0.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for Clason-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d07f2d47f558449ff10f755ed5201ad67c27fba3f11c6dc0e32df0db834730c5
MD5 081ebcf21cc7fb63ec7700d990e58523
BLAKE2b-256 9f504c40ff68557a28c507315e9570c7939e20faa2ef8e4d3f77b8ce04587a5c

See more details on using hashes here.

File details

Details for the file Clason-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: Clason-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for Clason-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 98d8d580265cdbbc2d644ce8421a4e65c8eef4551e64cb0a0ea2b4bdf7bf158e
MD5 2cc5ca4924a53ff5e0ed8b2b421f0925
BLAKE2b-256 d0928914ad1fcf8fefc068db39edc7ccdda39cc40b440c1c71c527bdc4391a1e

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