Fractal Specifications is an implementation of the specification pattern for building SOLID logic for your Python applications.
Project description
Fractal Specifications
Fractal Specifications is an implementation of the specification pattern for building SOLID logic for your Python applications.
Installation
pip install fractal-specifications
Usage
Specifications can be used to encapsulate business rules.
An example specification is EqualsSpecification("maximum_speed", 25)
.
A specification implements the is_satisfied_by(obj)
function that returns True
or False
,
depending on the state of the obj
that is passed into the function as parameter.
In our example, the obj
needs to provide the attribute maximum_speed
.
Full code example
This example includes a repository to show an application of specificationss
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import List
from fractal_specifications.generic.operators import EqualsSpecification
from fractal_specifications.generic.specification import Specification
@dataclass
class Road:
maximum_speed: int
@staticmethod
def slow_roads_specification():
return EqualsSpecification("maximum_speed", 25)
class RoadRepository(ABC):
@abstractmethod
def get_all(self, specification: Specification) -> List[Road]:
...
def slow_roads(self) -> List[Road]:
return self.get_all(Road.slow_roads_specification())
class PythonListRoadRepository(RoadRepository):
def __init__(self, roads: List[Road]):
self.roads = roads
def get_all(self, specification: Specification) -> List[Road]:
return [
road for road in self.roads
if specification.is_satisfied_by(road)
]
road_repository = PythonListRoadRepository([
Road(maximum_speed=25),
Road(maximum_speed=50),
Road(maximum_speed=80),
Road(maximum_speed=100),
])
if __name__ == '__main__':
print(road_repository.slow_roads())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for fractal-specifications-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c964065bb6a5a84d3158173a10d5dd657a71c6d4e0f25d03bd029457f74ba05c |
|
MD5 | 26d4e224aed8ccf57176cc77c3e41f87 |
|
BLAKE2b-256 | b9922e5fc5fd287bed3aa8ee881799a2d2cb4f1b9cc00b3ac21f10004a637bc1 |
Hashes for fractal_specifications-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a75486f48f14646639e9f4f37c373b9771153c7d45b22bb6343f40d54e4eb50 |
|
MD5 | 1cb40aa81e96f45f8e85cf824ca8c277 |
|
BLAKE2b-256 | 6ae64d98cdbcc219ebad33bb120686540901b538b2c8262dd480f9e3fc118834 |