Skip to main content

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

Metadata

Release files for Clason 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Clason 1.0.1
File Size Uploaded
Clason-1.0.1.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Clason 1.0.1
File Interpreter ABI Platform
Clason-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.3 kB

Release files / Clason-1.0.1.tar.gz

Download URL Clason-1.0.1.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d07f2d47f558449ff10f755ed5201ad67c27fba3f11c6dc0e32df0db834730c5
BLAKE2b-256 checksum
How to use checksums
9f504c40ff68557a28c507315e9570c7939e20faa2ef8e4d3f77b8ce04587a5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release files / Clason-1.0.1-py3-none-any.whl

Download URL Clason-1.0.1-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
98d8d580265cdbbc2d644ce8421a4e65c8eef4551e64cb0a0ea2b4bdf7bf158e
BLAKE2b-256 checksum
How to use checksums
d0928914ad1fcf8fefc068db39edc7ccdda39cc40b440c1c71c527bdc4391a1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page