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
from enum import Enum
class FlowerType(str, Enum):
DANDELION = 'Dandelion'
SUNFLOWER = 'Sunflower'
ROSE = 'Rose'
class MyThing(Scheme, root=True):
pass
class Flower(MyThing):
color = Field(str, tuple)
type = Field(str, validator=FlowerType)
class Pot(MyThing):
flowers = ListField(Flower)
watered = Field(bool)
print(MyThing.from_dict(
{
'flowers': [
{'color': '#fa21aa',
'type': 'Dandelion'},
{'color': '#daf1ad',
'type': 'Sunflower'},
{'color': '#ffaadd',
'type': 'Rose'}
],
'watered': False
}
)
)
# Output:
# Pot(flowers=[Flower(type=<FlowerType.DANDELION: 'Dandelion'>, color='#fa21aa'), Flower(type=<FlowerType.SUNFLOWER: 'Sunflower'>, color='#daf1ad'), Flower(type=<FlowerType.ROSE: 'Rose'>, color='#ffaadd')], watered=False)
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.
Flower class has a validator in type field. Validators are callables that accept value of the field and return validated value or raise an exception.
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.
Metadata
Release files for flowerfield 0.8.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flowerfield-0.8.0.tar.gz | 5.3 kB | Without provenance | Details for flowerfield-0.8.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flowerfield-0.8.0-py3-none-any.whl | Python 3 | none | any | Details for flowerfield-0.8.0-py3-none-any.whl |
Total release size: 11.4 kB
Release files / flowerfield-0.8.0.tar.gz
| Download URL | flowerfield-0.8.0.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7ca0d5af279c09901514b4744f19cd3c9cdafaca071f8e77c7c9c58ad63495fa
|
|
BLAKE2b-256 checksum How to use checksums |
4da8b14fa05a85582d675399192178c1a636a083648f7a84ea99015680a7b91d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1
|
Release files / flowerfield-0.8.0-py3-none-any.whl
| Download URL | flowerfield-0.8.0-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2f6336c3b9cb79c8935794a30479a6ce0463570a8b1cf6610e7af0067cd5d06a
|
|
BLAKE2b-256 checksum How to use checksums |
baefe968a51379129beb75873d216b91b44d493530bbf5e8f2ed49457d1a2b27
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1
|
Security for flowerfield 0.8.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.