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
Development
Setup the development environment by running:
make deps
Happy coding.
Occasionally you can run:
make lint
This is not explicitly necessary because the git hook does the same thing.
Do not disable the git hooks upon commit!
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 specifications.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fractal-specifications-1.0.2.tar.gz.
File metadata
- Download URL: fractal-specifications-1.0.2.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38a181ab8d95ca3b2fce2beec08f6cd3b7e32a1e9e981a8deff2e29d8bcb01af
|
|
| MD5 |
0fbef27c9c37f775be4f44317307eb72
|
|
| BLAKE2b-256 |
d07d3f1e18aa1ed434b8f921bc1ade130395a55b562e79d26d741729dd858f17
|
File details
Details for the file fractal_specifications-1.0.2-py3-none-any.whl.
File metadata
- Download URL: fractal_specifications-1.0.2-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52e1d7e85106ba0fcfb2151dee7b84df75901d2abd661aa5eedddd0fccdfa667
|
|
| MD5 |
01ab1060e90d704dd3fc21c5d945c64f
|
|
| BLAKE2b-256 |
37c9313714c830b0d84a2a15aeb7ffc45eaf7da0bb547bbf859aa8348ea03e05
|