Load your classes with json and save it with json or work with python dictionary's
Project description
Clason
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file Clason-1.0.tar.gz
.
File metadata
- Download URL: Clason-1.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 976597f92d939911175912a12c8ac04d5c9fe46f89adf09ee12843df4cf3172e |
|
MD5 | 5788c03058914ec8f203ac58be479131 |
|
BLAKE2b-256 | 6728e0feeeea7b77a1d9bb5f8b8a4e3b1811d9dbfbc646a1fc5aeef506738832 |
File details
Details for the file Clason-1.0-py3-none-any.whl
.
File metadata
- Download URL: Clason-1.0-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.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 295366b35f3ef87faf7172dffee78003b24be65deef50df8945bf53e8b56ac90 |
|
MD5 | 04ba496a192a6f2c0411111cf3b30a33 |
|
BLAKE2b-256 | 1468765069aa3abd8564e867e7e5b2bac6bf3f758700f77817d6c56e31b695ec |