Skip to main content

A simple pythonic implementation of the Specification Pattern for Python 3.7 or higher.

Project description

Build Status Code Coverage https://badge.fury.io/py/simple-specification.svg

A simple pythonic implementation of the Specification Pattern for Python 3.7 or higher.

Installation

pip install simple-specification

Examples

Creating specification classes to check integer numbers

from simple_specification import Specification


# hardcoded specification
class Odd(Specification):
    def is_satisfied_by(self, candidate: int) -> bool:
        return candidate % 2 != 0


# parametrized specification
class DivisibleBy(Specification):
    def __init__(self, number: int):
        self.number = number

    def is_satisfied_by(self, candidate: int) -> bool:
        return candidate % self.number == 0

Instantiating the specifications

>>> odd = Odd()
>>> even = ~odd
>>> div3 = DivisibleBy(3)
>>> div5 = DivisibleBy(5)

Using specifications - method is_satisfied_by(candidate)

>>> odd.is_satisfied_by(1)
True
>>> odd.is_satisfied_by(2)
False
>>> even.is_satisfied_by(3)
False
>>> even.is_satisfied_by(4)
True
>>> div3.is_satisfied_by(9)
True
>>> div5.is_satisfied_by(12)
False

Composing specifications - operators & and |

>>> # AND
>>> (even & div3).is_satisfied_by(6)
True
>>> (even & div3).is_satisfied_by(9)
False
>>> (even & div3 & div5).is_satisfied_by(30)
True
>>> (even & div3 & div5).is_satisfied_by(36)
False
>>>
>>> # OR
>>> (even | div3).is_satisfied_by(10)
True
>>> (even | div3).is_satisfied_by(15)
True
>>> (even | div3).is_satisfied_by(5)
False

Negating specifications - operator ~

>>> (~odd).is_satisfied_by(2)
True
>>> (~div5).is_satisfied_by(25)
False
>>> (~div5).is_satisfied_by(11)
True
>>> (~(odd | div5)).is_satisfied_by(6)
True
>>> (~(odd | div5)).is_satisfied_by(7)
False

Composing a list of specifications with & - class method Specification.all(specs)

>>> all_spec = Specification.all([~even, div3, div5])
>>> all_spec.is_satisfied_by(15)
True
>>> all_spec.is_satisfied_by(30)
False
>>> all_spec.is_satisfied_by(25)
False
>>> all_spec.is_satisfied_by(6)
False

Composing a list of specifications with | - class method Specification.any(specs)

>>> div15 = DivisibleBy(3) & DivisibleBy(5)
>>> any_spec = Specification.any([div15, even])
>>> any_spec.is_satisfied_by(2)
True
>>> any_spec.is_satisfied_by(15)
True
>>> any_spec.is_satisfied_by(30)
True
>>> any_spec.is_satisfied_by(3)
False
>>> any_spec.is_satisfied_by(5)
False

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

simple-specification-0.3.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

simple_specification-0.3-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file simple-specification-0.3.tar.gz.

File metadata

  • Download URL: simple-specification-0.3.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for simple-specification-0.3.tar.gz
Algorithm Hash digest
SHA256 1d75783c12479ac1d40072b9976fdd73c26eb4918ded81da2ec03991d0846dc6
MD5 c2fbdd54e9799ffeaf44358534edbd98
BLAKE2b-256 40a7aa5aa306e56fbf4260f4e66bd01b59a1a2023f2d7b1cc9115d4667a950dd

See more details on using hashes here.

File details

Details for the file simple_specification-0.3-py3-none-any.whl.

File metadata

  • Download URL: simple_specification-0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for simple_specification-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e3e9e098bfcd78bcac75c2fcec906afd0f01b60d2b256edb641885a8125900fd
MD5 d0de361c12fb53b6f215b1749720855c
BLAKE2b-256 e19a8908b5cc0ecf17466526eb3322fb2c42cceee92c798ffbadc0714c9cfd7a

See more details on using hashes here.

Supported by

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