A Python library for implementing design patterns
Project description
python-design-patterns
python-design-patterns is a Python library that provides implementations of various design patterns. Currently, it includes an implementation of the Pipeline pattern, which allows for the processing of data through a series of steps.
Table of Contents
Installation
To use this library, you need Python 3.9 or later installed on your machine. You can install the library using pip.
pip install python-design-patterns
Usage
Pipeline Pattern
The Pipeline pattern allows you to pass data through multiple processing steps. Each step can take inputs, perform operations, and pass results to the next step.
Examples
Here is a simple example of how to use the pipeline:
from pdp.pipeline import Pipeline, Step
# Define your processing functions
def add(x, y):
return x + y
def compute(step1, x, z):
return step1 + x - z
# Create a pipeline and add steps
steps = [
Step(name="step1", func=add),
Step(name="step2", func=compute),
]
pipeline = Pipeline(steps)
# Run the pipeline
result = pipeline.run(x=1, y=2, z=3)
print(result) # Output: {'x': 1, 'y': 2, 'z': 3, 'step1': 3, 'step2': 1}
Here are some additional examples to demonstrate the capabilities of the Pipeline pattern:
def multiply(x, y):
return x * y
def subtract(step1, z):
return step1 - z
# Create a new pipeline
pipeline = Pipeline()
pipeline.add_step(Step(name="step1", func=multiply))
pipeline.add_step(Step(name="step2", func=subtract))
# Run the pipeline
result = pipeline.run(x=2, y=3, z=1)
print(result) # Output: {'x': 2, 'y': 3, 'z': 1, 'step1': 6, 'step2': 5}
Memento Pattern
The Memento pattern allows you to save and restore the state of an object without exposing its internal structure. It is useful for implementing features like undo/redo in applications.
Examples
Here's a simple example of how to use the Memento pattern: To use this pattern, your class should inherit from the BaseOriginator class provided by the library.
from pdp.memento import BaseOriginator, Caretaker
# Define your own class that you want to be able to save/restore
class Mobility(BaseOriginator):
def __init__(self):
self.x = 1
self.y = 2
self.speed = 26
# override get and restore state
def get_state(self):
return {
'x': self.x,
'y': self.y,
'speed': self.speed
}
def set_state(self, state):
self.x = state['x']
self.y = state['y']
self.speed = state['speed']
def __str__(self):
return f"x={self.x}, y={self.y}, speed={self.speed}"
car = Mobility()
caretaker = Caretaker(car)
# save state
print(car) # x=1, y=2, speed=26
caretaker.save()
# change state
car.x = 5
car.y = 10
car.speed = 50
print(car) # x=5, y=10, speed=50
# save new state
caretaker.save()
# change state
car.x = 10
car.y = 20
car.speed = 100
print(car) # x=10, y=20, speed=100
# restore to previous state saved
caretaker.undo()
print(car) # x=5, y=10, speed=50
# restore to saved index
caretaker.restore(0)
print(car) # x=1, y=2, speed=26
# save history to file
caretaker.save_to_file("car_history.json")
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 python_design_patterns-0.2.0.tar.gz.
File metadata
- Download URL: python_design_patterns-0.2.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.9.20 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6332a1afe67cec913a2da8f3e46a1ef6e08e6e28e7463d2c54531ec8f77e6c4c
|
|
| MD5 |
5bf7dede9160da285fbb7094557cd545
|
|
| BLAKE2b-256 |
550be274c5b5f4e3efc67a275194fb5f6f801be1e2827d97d70d2026f30a3d4c
|
File details
Details for the file python_design_patterns-0.2.0-py3-none-any.whl.
File metadata
- Download URL: python_design_patterns-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.9.20 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59e06ac9c46f8e1aaa579b41038e954e28a9608f7333c3b36c399ae1ebbf231e
|
|
| MD5 |
bb037d5e0f38133e4417bf957ce150ac
|
|
| BLAKE2b-256 |
a667608592d821ca056f6c90d76bab1fe897eeb6d8efd5748b6ac11da27fcaa9
|