Skip to main content

Structural matching lib for Python

Project description

klotan

Description

Klotan is a Python library used to match a structure (tuples, lists, dicts) with a template. A template is made of a set of criteria.

Installation

You just have to clone this repository and install it using the command

pip install .

Or you can also install it from PyPi

pip install klotan

You can test the installation by running the following command :

python -c "import klotan; print(klotan.match.match([klotan.criteria.equals(22)], [33]).is_valid())"

which should print False

Usage

Klotan can be used to validate some form results for example

person_template = {
    # The gender can be either M, F or N/A
    "gender": criteria.is_in("M", "F", "N/A"),
    # Expects a name starting with a capital letter, followed by 1 to 15 letters
    "name": criteria.regex("[A-Z][a-z]{1,15}"),
    # Jeanne Calment still holds the world record
    "age": criteria.between(0, 122),
    # Matches all emails
    "email": criteria.is_email(),
    # Optional field, matches if value is an URL
    match.optional("personal_website"): criteria.is_url(),
    # Matches valid IBANs
    "iban": criteria.is_valid_iban(),
    # Matches valid credit card numbers
    "credit_card": criteria.is_valid_credit_card_number(),
    # We only accept people without debts !
    "amount_of_money": criteria.is_positive(),
    # This criteria will accept anything
    "bonus": criteria.accept(),
}

# Those are of course, fake informations
person = {
    "gender": "M",
    "name": "Bob",
    "age": 18,
    "email": "bob.dupont@gmail.com",
    "personal_website": "https://bob.dupont.fr/",
    "iban": "FR68 1009 6000 7018 7224 1164 C75",
    "credit_card": "4929609419559701",
    "bonus": lambda: 0,
}

print(match.match(person_template, person).to_string())

The code above prints the following output :

{
    "gender": Criteria<is_in>("M", "F", "N/A") << ("M") => True,
    "name": Criteria<regex>("[A-Z][a-z]{1,15}") << ("Bob") => True,
    "age": Criteria<between>(0, 122) << (18) => True,
    "email": Criteria<is_email> << ("bob.dupont@gmail.com") => True,
    "personal_website": Criteria<is_url> << ("https://bob.dupont.fr/") => True,
    "iban": Criteria<is_valid_iban> << ("FR68 1009 6000 7018 7224 1164 C75") => True,
    "credit_card": Criteria<is_valid_credit_card_number> << ("4929609419559701") => True,
    "amount_of_money": Criteria<expected>(Criteria<is_positive>, False) << (None) => False,
    "bonus": Criteria<accept> << (<function <lambda> at 0x000001C4767330D0>) => True
}

Licensing

Klotan uses the MIT license, you can find more details about it here

Contributing

You can find more details about contributions here

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

klotan-1.11.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file klotan-1.11.0-py3-none-any.whl.

File metadata

  • Download URL: klotan-1.11.0-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.8.0

File hashes

Hashes for klotan-1.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 039c8fc3616da007cba6be659a241163222dddf5d1dfa78b719edb77475ad894
MD5 a6443523bc4373e4b3eb4f5bd7b91a05
BLAKE2b-256 c2a83f30bd1d55c5a1a4a0571783123271a497714b149556da40af194b70d524

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