Skip to main content

A simple python implementation of Specification pattern.

Project description

Sutoppu

Pypi Version Python Version CI Coverage Status Project license Code Style

Sutoppu (ストップ - Japanese from English Stop) is a simple python implementation of Specification pattern.

What is Specification Pattern?

See Wikipedia.

In computer programming, the specification pattern is a particular software design pattern, whereby business rules can be recombined by chaining the business rules together using boolean logic. The pattern is frequently used in the context of domain-driven design.

More information: Eric Evans and Martin Fowler article about Specifications

Basic usage

Installation

$ pip install sutoppu

Usage

from sutoppu import Specification


class Fruit:
    def __init__(self, color: str, sweet: bool, bitter: bool):
        self.color = color
        self.sweet = sweet
        self.bitter = bitter


class FruitIsBitter(Specification):
    description = 'The given fruit must be bitter.'

    def is_satisfied_by(self, fruit: Fruit):
        return fruit.bitter is True


class FruitIsSweet(Specification):
    description = 'The given fruit must be sweet.'
    
    def is_satisfied_by(self, fruit: Fruit):
        return fruit.sweet is True


class FruitIsYellow(Specification):
    description = 'The given fruit must be yellow.'

    def is_satisfied_by(self, fruit: Fruit):
        return self.color == 'yellow'
>>> lemon = Fruit(color='yellow', sweet=False, bitter=True)
>>> is_a_lemon = FruitIsYellow() & FruitIsBitter() & ~FruitIsSweet()
>>> is_a_lemon.is_satisfied_by(lemon)
True

Operators

Bitwise operators are overload to provide simple syntax.

And: yellow

>>> my_spec = SpecificationA() & SpecificationB()

Or:

>>> my_spec = SpecificationA() | SpecificationB()

Not:

>>> my_spec = ~SpecificationA()

Lighter syntax

If you do not find the is_satisfied_by method very convenient you can also directly call the specification as below.

>>> lemon = Fruit(color='yellow', sweet=False, bitter=True)
>>> is_a_lime = FruitIsGreen() & FruitIsBitter() & ~FruitIsSweet()
>>> is_a_lime(lemon)
False

Error reporting

It can be difficult to know which specification failed in a complex rule. Sutoppu allows to list all the failed specifications by getting the errors attribute after use. The errors attribute is reset each time the specification is used. For each failed specification, it returns a dict with the name of the specification class for key and the description provide in the class for value. In the case where the specification failed with a not condition, the description is prefixed with Not ~.

>>> apple = Fruit(color='red', sweet=True, bitter=False)
>>> is_a_lemon = FruitIsYellow() & FruitIsBitter() & ~ FruitIsSweet()
>>> is_a_lemon.is_satisfied_by(apple)
False
>>> is_a_lemon.errors
{
    'FruitIsColored': 'The given fruit must be yellow.',
    'FruitIsBitter': 'The given fruit must be bitter.',
    'FruitIsSweet': 'Not ~ The given fruit must be sweet.'
}

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

sutoppu-0.1.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

sutoppu-0.1.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file sutoppu-0.1.1.tar.gz.

File metadata

  • Download URL: sutoppu-0.1.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.10 Linux/5.4.0-89-generic

File hashes

Hashes for sutoppu-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2c918dc370a1d4701c45f8488a7fc76c3922cb0cea31fdb187aaf7e4f3b92b35
MD5 5dd152379aecc1776870228b394b87d1
BLAKE2b-256 adb440d9bc8d89678d3f2e549bb68d19f154f51e2fc13cb7b60059c51956a8ca

See more details on using hashes here.

File details

Details for the file sutoppu-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: sutoppu-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.10 Linux/5.4.0-89-generic

File hashes

Hashes for sutoppu-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 21f079f4354df6a49f3e024adb4736ab1b921ee749b7951766825f41f5c518cb
MD5 dfd1d712cf1683ea0c01f6810e51f548
BLAKE2b-256 61e45306d4bea8db4287358c6243bfd65dc673f71a019bc1a141ca421c8e8d55

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