Skip to main content

Design patterns for Python implemented with decorators and classes.

Project description

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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

meta-patterns-0.2.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

meta_patterns-0.2.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file meta-patterns-0.2.1.tar.gz.

File metadata

  • Download URL: meta-patterns-0.2.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for meta-patterns-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1c55f0d53e9e248ddecd8a00922824c8fed5be05e11868e3077af70e1268faf3
MD5 48a23189bf7349d2fbfa94e80e136b15
BLAKE2b-256 92debf7bb70c43ed45e81feb89d5d2f4b946526483e5c4e0e46eb9436508b2e1

See more details on using hashes here.

File details

Details for the file meta_patterns-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: meta_patterns-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for meta_patterns-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 049faef2f7e76a80b18ee22d4257e44033dfb57780693cc8c3bcbfcac8f99969
MD5 48705160b7f323ec9817337a677a4ad5
BLAKE2b-256 adb93dd4f9ce7d0d34f36102cc458e2e22c08f117f593146eadc01cd7932e560

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page