Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Thin DSL for creating input_algorithms

Project Description

Input Algorithms

A DSL to assist with writing specifications describing valid data and testing that inputted data meets those defined specifications.

Why the name?

I got the inspiration from the movie Transcendence when a character says something to the effect of “We could do this if we had better input algorithms”.

Installation

Use pip!:

pip install input_algorithms

Or if you’re developing it:

pip install -e .
pip install -e ".[tests]"

USAGE

Here is an example to help you use the library.

from input_algorithms.validators import Validator
from input_algorithms.dictobj import dictobj
from input_algorithms import spec_base as sb
from input_algorithms.meta import Meta
import re

meta = Meta({},[])

# 1. Create a class defining your fields.
class PersonDictObj(dictobj):
    fields = ["name", "age"]

# 2. Create custom validate methods as required.
class ValidName(Validator):
    def validate(self, meta, val):
        matcher = re.compile("^[A-Za-z\ ]+$")
        if not matcher.match(val):
            raise Exception("{0} doesn't look like a name.".format(val))
        return val

class ValidAge(Validator):
    def validate(self, meta, val):
        if val > 120:
            raise Exception("I don't believe you are that old")
        return val

# 3. Tie together the pieces.
person_spec = sb.create_spec(
    PersonDictObj,
    name = sb.required(sb.valid_string_spec(ValidName())),
    age = sb.and_spec(sb.integer_spec(), ValidAge()),
)

# 4. Have some data
data = {"name": "Ralph", "age": 23}

# 5. Normalise the data into your object
normalised = person_spec.normalise(meta, data)

# 6. Use the object!
print("Name is {0}".format(normalised.name))
print("Age is {0}".format(normalised.age))

Tests

To run the tests in this project, just use the helpful script:

./test.sh

Or run tox:

tox
Release History

Release History

This version
History Node

0.6.0

History Node

0.5.9

History Node

0.5.8

History Node

0.5.7

History Node

0.5.6

History Node

0.5.5

History Node

0.5.4

History Node

0.5.3

History Node

0.5.2

History Node

0.5.1

History Node

0.5

History Node

0.4.7

History Node

0.4.6

History Node

0.4.5.5

History Node

0.4.5.4

History Node

0.4.5.3

History Node

0.4.5.2

History Node

0.4.5.1

History Node

0.4.5

History Node

0.4.4.9

History Node

0.4.4.8

History Node

0.4.4.7

History Node

0.4.4.6

History Node

0.4.4.5

History Node

0.4.4.4

History Node

0.4.4.3

History Node

0.4.4.2

History Node

0.4.4.1

History Node

0.4.4

History Node

0.4.3.1

History Node

0.4.3

History Node

0.4.2

History Node

0.4.1.1

History Node

0.4.1

History Node

0.4

History Node

0.3.5

History Node

0.3.4

History Node

0.3.3

History Node

0.3.2

History Node

0.3.1

History Node

0.3

History Node

0.2

History Node

0.1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
input_algorithms-0.6.0.tar.gz (20.5 kB) Copy SHA256 Checksum SHA256 Source Jun 21, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting