Tiny python module to automatically map dictionaries to python objects.
Project description
FlowerField
Tiny python module to automatically map dictionaries to python objects.
Installation
Pypi
Install with python's package manager:
pip install flowerfield
Git
pip intall git+https://github.com/cmd410/flowerfield.git
Or...
...or just copy flowerfield directory into your project, this module doesn't depend on anything
Usage
This library is stupid simple to use:
from flowerfield import Scheme, Field, ListField
class MyThing(Scheme, root=True):
pass
class Flower(MyThing):
color = Field((str, tuple))
class Pot(MyThing):
flowers = ListField(Flower)
watered = Field(bool)
print(MyThing.from_dict(
{
'flowers': [
{'color': '#fa21aa'},
{'color': '#daf1ad'}],
'watered': True
}
)
)
# Output:
# Pot(flowers=[Flower(color='#fa21aa'), Flower(color='#daf1ad')], watered=True)
First we create a class MyThing
which inherits from Scheme
and has a keyword parameter root
. This parameter is a boolean which, when true means that this class will be used to map its children. Root Schemes are a way to organize you schemes into some categories that you want to match dictionaries against. Root schemes never participate in the match themselves, adding fields to them is pointless.
Scheme is a root itself, so when you call
Scheme.from_dict(some_dictionary)
it will return most suitable scheme object from ALL its children that exist in you program.
Next we create Flower
and Pot
class and add some fields to them. Field class, optionally accepts type of the field it will check against when set. You can give more than one type as tuple. Schemes as fields type automatically map every dictionary that is given to that field. You can make Optional fields by passing type(None)
into Field.
Then we call MyThing.from_dict(...)
and it automatically creates a Pot with Flowers inside of it based the dict we gave it. Isn't that nice?
You can, of course, call from_dict
on non-root scheme but it will only map this concrete scheme.
How it determines what schema to use?
When deciding to what class to map a dictionary it goes through following steps:
- Get keys from dictionary
- Go through subclasses and check how much of fields names intersect with dictionary keys
- Choose a structure which has the most keys in it.
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 flowerfield-0.1.0.tar.gz
.
File metadata
- Download URL: flowerfield-0.1.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 826626208fcea31d94c2236b49e6902709d58a40ca77e59371de50e32d74bb14 |
|
MD5 | efc2ae98243ef695f4f60311d419358e |
|
BLAKE2b-256 | 9a2c4ee6611674eed5788dbf354bbcf525e185daa307d2e53d60503a418eadf3 |
File details
Details for the file flowerfield-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: flowerfield-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf9abe5624a1cb0fa5ab943ff347ee6b326c35b76e014d645763430730dbab2f |
|
MD5 | 0fabe0f57c4a3cad33fc1b7e737b105e |
|
BLAKE2b-256 | 3b28125163020789f23ae2bb7db7dbe0801936c8c410ea25ca6396398c9321d5 |