GithubHelp home page GithubHelp logo

adriandiazgar / notifyr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from victorcmoura/notifyr

0.0 0.0 0.0 27 KB

Python object notification tool package

Home Page: https://pypi.org/project/notifyr/

License: GNU General Public License v3.0

Python 95.18% Shell 4.82%

notifyr's Introduction

notifyr - Create observers in runtime

PyPI

Description

Notifyr is a package that enables simple class observed-observer schema at runtime. Instead of building the whole observation and notification toolchain, just use python decorators and notifyr will take care of the rest.

Inspired by the Observer Design Pattern, notifyr adds the necessary methods to your classes without inheritance.

Decorators

Function decorators:

  • @target
    • Indicates that the decorated function is targeted and will trigger the observers update() everytime it runs.

Class decorators:

  • @observed
    • Adds .observers list attribute.
    • Adds .attach(obj) method that appends obj to the observers list.
    • Adds .notify() method that notifies the observers everytime the targeted functions are called
  • @observer('function_name')
    • Adds update() method that executes class's function_name() passing, as arguments, self and everything that the targeted function received (including the self argument).

Usage

Original Code:

class Dog(object):
    def __init__(self, name):
        self.name = name
    
    def bark(self):
        print('Woof')
    
    def sleep(self):
        print(self.name, 'is now asleep: ZZzzzzZzzZ...')

class Person(object):
    def __init__(self, name):
        self.name = name
    
    def educate_dog(self, dog):
        print(self.name + ':','Sleep,', dog.name)
        dog.sleep()

Suppose we want a person to educate a dog every time the animal barks:

from notifyr.agents import observed, observer
from notifyr.functions import target

@observed
class Dog(object):
    def __init__(self, name):
        self.name = name
    
    @target
    def bark(self):
        print('Woof')
    
    def sleep(self):
        print(self.name, 'is now asleep: ZZzzzzZzzZ...')
    
@observer('educate_dog')
class Person(object):
    def __init__(self, name):
        self.name = name
    
    def educate_dog(self, dog):
        print(self.name + ':','Sleep,', dog.name)
        dog.sleep()

And now, it is possible to archieve this by magically calling bark() after attaching a person to a dog:

d = Dog('Tobby')
p = Person('Victor')

d.attach(p) # Victor is now observing Tobby

d.bark()
# Woof
# Victor: Sleep, Tobby
# Tobby is now asleep: ZZzzzzZzzZ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.