Skip to main content

A lightweight model validator for modern projects.

Project description

bike

A lightweight model validator for modern projects.

Instalation

pip install bike

First Pedals

Lets define a simple model to represent a person.

import bike


@bike.model()
class Person:
    name: str
    height: float
    weight: float
    
...

p1 = Person(
    name='Patrick Love', 
    height=75, 
    weight=180
)

A Person instance can be created passing the attributes. Also can be instantiated by a dict.

...

data = {
    'name': 'Patrick Love',
    'height': 75,
    'weight': 180
}

p1 = Person(**data)

Nested Models

We can create and mananger more complex structs by nested models. It's simply by using a model as field type of other model.

# implement.py
import bike


@bike.model()
class Address:
    address_1: str
    address_2: str = ''
    city: str
    state: str
    code: str
    country: str

    
@bike.model()
class Phone:
    country_code: str
    local_code: str
    number: str

    
@bike.model()
class Person:
    name: str
    address: Address
    phones: list[Phone]
    
...
    
payload = {
    'name': 'Aline Mark',
    'address': {
        'address_1': '239 W 45th St',
        'address_2': '',
        'city': 'New York',
        'state': 'NY',
        'code': '10036',
        'country': 'EUA'
    },
    'phones': [
        {
            'country_code': '+1',
            'local_code': '010',
            'number': '134354325'
        },
        {
            'country_code': '+2',
            'local_code': '011',
            'number': '134565436'
        }
    ]
}
p1 = Person(**payload)
print(p1.phones[0].country_code)
# +1
print(p1.phones[1].local_code)
# 011
print(p1.address.state)
# NY

Parsing to Json and Dict

We can parse data to dict or json by using .dict() or json() methods respectively.

import bike


@bike.model()
class Make:
    name: str
    country: str

    
@bike.model()
class Car:
    name: str
    make: Make
    

m1 = Make(name='Nissan', country='JP')
c1 = Car(name='Leaf', make=m1)

c1_dict = c1.dict()
print(c1_dict['make']['country'])
# JP

c1_json = c1.json()
print(c1_json)
# {"name": "Leaf", "make": {"name": "Nissan", "country": "JP"}}

Validating Fields

Fields can be validated adding the validator annotation.

import bike


@bike.model()
class Character:
    name: str
    health: float = 1.0

    @bike.validator('name')
    def name_validator(cls, val):
        return val.title()
    
    @bike.validator('health')
    def health_validator(cls, val):
        if val < 0: 
            return 0.0
        elif val > 1:
            return 1.0
        return val
    
    
c1 = Character(name='ninki walker', health=2.0)
print(c1.name)
# Ninki Walker
print(c1.health)
# 1.0

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

bike-0.3.3.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bike-0.3.3-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file bike-0.3.3.tar.gz.

File metadata

  • Download URL: bike-0.3.3.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.19.0-45-generic

File hashes

Hashes for bike-0.3.3.tar.gz
Algorithm Hash digest
SHA256 c820cbefa314ca90f751cbc20ec9d1e4764c1c773b6ce36977a58b374367b689
MD5 9335061d89f87791e6e9d5bea7fa9c77
BLAKE2b-256 e9103f1d4958ea611c6c1a0fe5f5ef1a0aae09f322f19a5c62bd0b38474009e8

See more details on using hashes here.

File details

Details for the file bike-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: bike-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.19.0-45-generic

File hashes

Hashes for bike-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c1c92f04909a7c3905c9a25bb3919f011b4a87b62e4d7cdb9899e6b729603aed
MD5 ef8a6d0f544b87926d825a27d489916f
BLAKE2b-256 f8a5cd0263d9b07a65f27e40d1080637e579fda2f59ffef9c32e2962a76dc21f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page