A library to easily use design patterns with Python
Project description
Pytterns
Pytterns is a Python library that provides an easy and intuitive way to use Design Patterns in your Python code.
Implemented Design Patterns
Strategy
The Strategy pattern allows the definition of a family of algorithms, encapsulating them and making them interchangeable at runtime.
Usage Example
from pytterns import strategy, load
@strategy("payment")
class CreditCardPayment:
def check(self, method):
return method == "credit_card"
def execute(self):
return "Processing payment via Credit Card"
@strategy("payment")
class PayPalPayment:
def check(self, method):
return method == "paypal"
def execute(self):
return "Processing payment via PayPal"
# Selecting strategy based on payment method
payment_strategy = load.strategy("payment").check("paypal").execute()
print(payment_strategy) # Output: Processing payment via PayPal
Chain of Responsibility
Chain of Responsibility is a behavioral design pattern that allows a request to be processed by a sequence of handlers. The order of the handlers will be defined in the decorator/annotation itself.
Usage Example
from pytterns import chain, load
@chain("auth_chain", order=1)
class Authenticator:
def handle(self, request):
if not request.get("authenticated", False):
print("Authentication failed!")
return # Interrompe a cadeia aqui
print("User authenticated")
@chain("auth_chain", order=2)
class Authorizer:
def handle(self, request):
if request.get("role") != "admin":
print("Authorization failed!")
return
print("User authorized")
@chain("auth_chain", order=3)
class Logger:
def handle(self, request):
print(f"Logging request: {request}")
# Carregando a cadeia
auth_chain = load.chain("auth_chain")
# Simulando requisições
print("=== Request 1 ===")
auth_chain.handle({"authenticated": False, "role": "admin"})
print("\n=== Request 2 ===")
auth_chain.handle({"authenticated": True, "role": "user"})
print("\n=== Request 3 ===")
auth_chain.handle({"authenticated": True, "role": "admin"})
Expected Output
=== Request 1 ===
Authentication failed!
=== Request 2 ===
User authenticated
Authorization failed!
=== Request 3 ===
User authenticated
User authorized
Logging request: {'authenticated': True, 'role': 'admin'}
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 pytterns-0.1.0.tar.gz.
File metadata
- Download URL: pytterns-0.1.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84fb00f77064dbc9a15ceac2cd58a85c2d41b2bbd40120a8e3cd2417f30b8138
|
|
| MD5 |
59a4ef057993b979914decab2d6b258a
|
|
| BLAKE2b-256 |
e94902c67d953ae83614535c1edb9e8c4c9065d240af2c8fbe55c591119b5a07
|
File details
Details for the file pytterns-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pytterns-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b2648d7a0871e03714ab946b9466ba60b1aa5f82c810b01881509a6a5b2196c
|
|
| MD5 |
6a9e8dd4c561203abc0c8c3a80e04c60
|
|
| BLAKE2b-256 |
af865064a0c0a4e13532a85c0e3444aed7beb0e61020b0837965935dd3fa854c
|