Skip to main content

Tiny python module to automaticallymap 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
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:

  1. Get keys from dictionary
  2. Go through subclasses and check how much of fields names intersect with dictionary keys
  3. Choose a structure which has the most keys in it.

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

flowerfield-0.5.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

flowerfield-0.5.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file flowerfield-0.5.0.tar.gz.

File metadata

  • Download URL: flowerfield-0.5.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for flowerfield-0.5.0.tar.gz
Algorithm Hash digest
SHA256 248040235791593c97069a27c27d614f74c3c3c72353bbf81a2eebfe548dc0ae
MD5 51620774e9a0678c0d73a486cbeea61b
BLAKE2b-256 f1fa0d6b179c24b2c4d6ee63efcacc8b79274981973532f1aa0bdb2abbc728b1

See more details on using hashes here.

File details

Details for the file flowerfield-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: flowerfield-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for flowerfield-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 af98f692010fe70685b51ad25a5543b17fc9e6c072643a3cb5c1cd49142d6eaa
MD5 3e803fa66350ee642cd80bad39b91cb9
BLAKE2b-256 6a6d755f2313c4c627b4c1eacd78bf2719062784550c4aa797c02395fba2183b

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