Skip to main content

Input Algorithms

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

https://travis-ci.org/delfick/input_algorithms.png?branch=master

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

Metadata

Release files for input_algorithms 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for input_algorithms 0.6.0
File Size Uploaded
input_algorithms-0.6.0.tar.gz 20.5 kB Details

Release files / input_algorithms-0.6.0.tar.gz

Download URL input_algorithms-0.6.0.tar.gz
Size 20.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d437ba67b907446f5c9a550787330334e404f6caad5d8d80544101f5f28e043f
BLAKE2b-256 checksum
How to use checksums
8a8acf55a81ba250ee4d7d715d7e3a6c349cff066d48e5677192799288109393
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.6.0 This release

1 release file

0.5.9

1 release file

0.5.8

1 release file

0.5.7

1 release file

0.5.6

1 release file

0.5.5

1 release file

0.5.4

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5

1 release file

0.4.7

1 release file

0.4.6

1 release file

0.4.5.3

1 release file

0.4.5.2

1 release file

0.4.5.1

1 release file

0.4.5

1 release file

0.4.4.9

1 release file

0.4.4.6

1 release file

0.4.4.4

1 release file

0.4.4.2

1 release file

0.4.4.1

1 release file

0.4.4

1 release file

0.4.3.1

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1.1

1 release file

0.4.1

1 release file

0.4

1 release file

0.3.5

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page