Code Monkey home page Code Monkey logo

tinytraitlet's Introduction

tinytraitlet

tinytraitlet is a simple library that enables attribute validation and type checking for Traitful classes, inspired by traitlets library.

Usage

from tinytraitlet import Traitful, String, validate, TraitError


class User(Traitful):
    name = String()
   
    @validate('name')
    def validate_name(self, value):
        if value == 'undefined':
            raise TraitError("Name cannot be 'undefined'")


class SubscriptionUser(User):
    subscription = String()
   
    @validate('subscription')
    def validate_subscription(self, value):
        if value not in ["monthly", "yearly"]:
            raise TraitError("subscription must be either 'monthly' or 'yearly'")

These classes behave as one would expect. @validate('name') decorator registers the validate_name method as the validator for the name trait. SubscriptionUser inherits both the name trait and the validate_name validator.

>>> user = User(name="Niki")
>>> user.name
'Niki'
>>> user = SubscriptionUser(name="undefined")
tinytraitlet.TraitError: Name cannot be 'undefined'
>>> user = SubscriptionUser(subscription="yearly")
>>> user.subscription
'yearly'
>>> user.subscription = "daily"
tinytraitlet.TraitError: subscription must be either 'monthly' or 'yearly'

Defining custom types: We should define default_value and model_type attributes. Example:

from tinytraitlet import Model

class Integer(Model):
    default_value = 0
    model_type = int

Why solve the same problem?

tinytraitlet does not use metaclasses unlike traitlets. This reduces the chance of metaclass conflict. tinytraitlet is a simple alternative, currently under 100 lines of code, and offers only the absolutely essential functionality.

Limitations

  • Currently a single trait type is defined: String.
  • Default values are not validated.
  • Cross validation is not supported.
  • A trait cannot have multiple validators

tinytraitlet's People

Contributors

kori73 avatar

Stargazers

Ekinsu Özmen avatar  avatar Eren Şahin avatar burakyilmaz321 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.