meta-patterns
Design patterns for Python implemented with decorators and classes.
Getting Started
Currently only one design pattern is implemented: Listener.
Listener Pattern
The Listener pattern (otherwise known as the Observer or Publish-Subscribe pattern) is a behavioral design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing (source).
Its use is demonstrated here:
from metapatterns.listener import Listenable, listenable
class Subject(Listenable):
@listenable
def myfunc(self, arg1):
"""
@listenable indicates this function can be 'listened in on'.
It allows Listeners to hook into it (see MyListener)
"""
print("myfunc called with arg", arg1)
return "Hoozah"
def myfunc2(self, arg1):
print("myfunc called with arg", arg1)
class MyListener(Subject.Listener):
"""
Identify this class as a listener of `Subject` through inheritance.
This makes it so not all listenable methods need to be implemented (they have a default empty implementation in `Subject.Listener`).
"""
def on_myfunc(self, subject, arg1):
print("listened in on call to myfunc with arg", arg1)
def on_myfunc_finished(self, subject, result, arg1):
print("listened in on result of myfunc with arg", arg1, "and result", result)
# This cannot be defined because myfunc2 is not a listenable function in Subject
#def on_myfunc2(self, arg1):
#pass
We can run this as follows:
if __name__ == "__main__":
subject = Subject()
print("# Calling myfunc without listener")
subject.myfunc(3)
listener = MyListener()
subject.add_listener(listener)
print("\n# Calling myfunc with listener")
subject.myfunc(5)
print("\n# Calling myfunc2 with listener")
subject.myfunc2(7)
subject.remove_listener(listener)
print("\n# Calling myfunc again with listener removed")
subject.myfunc(5)
which gives the output:
# Calling myfunc without listener
myfunc called with arg 3
# Calling myfunc with listener
listened in on call to myfunc with arg 5
myfunc called with arg 5
listened in on result of myfunc with arg 5 and result Hoozah
# Calling myfunc2 with listener
myfunc called with arg 7
# Calling myfunc again with listener removed
myfunc called with arg 5
Subclassing from Subject.Listener has the advantage of raising a TypeError when an on_ function has no matching counterpart in Subject. This can help detect difficult to find problems that arise when changing the name of a function in Subject. Our method includes this check to guard against some issues that come with the loose coupling of the Listener pattern.
Future Work
- Implement more design patterns.
- Suggestions? Contact me!
Metadata
Release files for meta-patterns 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| meta-patterns-0.2.1.tar.gz | 3.8 kB | Without provenance | Details for meta-patterns-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| meta_patterns-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for meta_patterns-0.2.1-py3-none-any.whl |
Total release size: 8.5 kB
Release files / meta-patterns-0.2.1.tar.gz
| Download URL | meta-patterns-0.2.1.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1c55f0d53e9e248ddecd8a00922824c8fed5be05e11868e3077af70e1268faf3
|
|
BLAKE2b-256 checksum How to use checksums |
92debf7bb70c43ed45e81feb89d5d2f4b946526483e5c4e0e46eb9436508b2e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
|
Release files / meta_patterns-0.2.1-py3-none-any.whl
| Download URL | meta_patterns-0.2.1-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
049faef2f7e76a80b18ee22d4257e44033dfb57780693cc8c3bcbfcac8f99969
|
|
BLAKE2b-256 checksum How to use checksums |
adb93dd4f9ce7d0d34f36102cc458e2e22c08f117f593146eadc01cd7932e560
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
|
Security for meta-patterns 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.