The Blinker Herald includes helpers to easily emit signals using the excellent blinker library.

Overview

Blinker Herald

The Herald

https://travis-ci.org/SatelliteQE/blinker_herald.svg?branch=master Documentation Status Coverage

The Blinker Herald includes helpers to easily emit signals using the excelent blinker library.

Decorate a function or method with @blinker_herald.emit() and pre and post signals will be automatically emitted to all connected handlers.

Features

  • All the features provided by blinker
  • + an easy decorator @emit() to magically emit signals when your functions are called and before it returns a result.
  • A signals namespace proxy to discover the signals in your project
  • Customizable for your needs

Usage

Let's say you have a class and wants to emit a signal for a specific method:

from blinker_herald import emit

class SomeClass(object):

    @emit()
    def do_something(self, arg1):
        # here is were magically the 'pre' signal will be sent
        return 'something done'
        # here is were magically the 'post' signal will be sent

using @emit decorator makes blinker_herald to emit a signal for that method and now you can connect handlers to capture that signals

You can capture pre signal to manipulate the object:

SomeClass.do_something.pre.connect
def handle_pre(sender, signal_emitter, **kwargs):
    signal_emitter.foo = 'bar'
    signal_emitter.do_another_thing()

And you can also capture the post signal to log the results:

SomeClass.do_something.post.connect
def handle_post(sender, signal_emitter, result, **kwargs):
    logger.info("The method {0} returned {1}".format(sender, result))

Note

Post-signals are only called if there were no exceptions raised during the processing of their related function.

You can also use the namespace proxy blinker_herald.signals to connect handlers to signals, the signal name is the prefix pre or post followed by _ and the method name:

from blinker_herald import signals

@signals.pre_do_something.connect
def handle_pre(sender, signal_emitter, **kwargs):
    ...

If you have a lot of subclasses emitting signals with the same name and you need to capture only specific signals, you can specify that you want to listen to only one type of sender:

from blinker_herald import emit, signals, SENDER_CLASS
class BaseModel(object):
    ...
    @emit(sender=SENDER_CLASS)
    def create(self, **kwargs):
        new_instance = my_project.new(self, **kwargs)
        return new_instance

class One(BaseModel):
    pass

class Two(BaseModel):
    pass

Note

By default the sender is always the instance but you can use SENDER_CLASS to force the sender to be the class another options are SENDER_CLASS_NAME, SENDER_MODULE, SENDER_NAME and you can also pass a string, an object or a lambda receiving the sender instance e.g: @emit(sender=lambda self: self.get_sender())

Using SENDER_CLASS you can now connect to specific signal:

from blinker_herald import signals

@signals.post_create.connect_via(One)
def handle_post_only_for_one(sender, signal_emitter, result, **kwargs):
    # sender is the class One (cls)
    # signal the instance of the class One (self)
    # result is the return of the method create

The above will handle the create method signal for the class One but not for the class Two

You can also be more specific about the signal you want to connect using the __ double underscore to provide method name:

from blinker_herald import signals

@signals.module_name__ClassName__post_method_name.connect
def handle_post(sender, signal_emitter, result, **kwargs):
    ...

The above will connect to the post signal emitted by module_name.ClassName.method_name

Note

You don't have to use the pattern above if your project do not have a lot of method name collisions, using only the method name will be just fine for most cases.

Credits

This software was first created by SatelliteQE team to provide signals to Robottelo and Nailgun

Owner
SatelliteQE
Projects related to the automation of Red Hat Satellite 6
SatelliteQE
A system for assigning and grading notebooks

nbgrader Linux: Windows: Forum: Coverage: Cite: A system for assigning and grading Jupyter notebooks. Documentation can be found on Read the Docs. Hig

Project Jupyter 1.2k Dec 26, 2022
p5 is a Python package based on the core ideas of Processing.

p5 p5 is a Python library that provides high level drawing functionality to help you quickly create simulations and interactive art using Python. It c

p5py 645 Jan 04, 2023
Source code for Learn Programming: Python

This repository contains the source code of the game engine behind Learn Programming: Python. The two key files are game.py (the main source of the ga

Niema Moshiri 25 Apr 24, 2022
Convert long numbers into a human-readable format in Python

Convert long numbers into a human-readable format in Python

Alex Zaitsev 73 Dec 28, 2022
Advanced Developing of Python Apps Final Exercise

Advanced-Developing-of-Python-Apps-Final-Exercise This is an exercise that I did for a python advanced learning course. The exercise is divided into t

Alejandro Méndez Fernández 1 Dec 04, 2021
A sage package for working with circular genomes represented by signed or unsigned permutations

Circular genome tools (cgt) A sage package for working with circular genomes represented by signed or unsigned permutations. It includes tools for con

Joshua Stevenson 1 Mar 10, 2022
Python solutions to Codeforces problems

CodeForces This repository is dedicated to my Python solutions for CodeForces problems. Feel free to copy, contribute and/or comment. If you find any

Shukur Sabzaliev 15 Dec 20, 2022
Read and write life sciences file formats

Python-bioformats is a Python wrapper for Bio-Formats, a standalone Java library for reading and writing life sciences image file formats. Bio-Formats

CellProfiler 106 Dec 19, 2022
Quantity Takeoff with Python. Collecting groups of elements by filters

The free tool QuantityTakeoff allows you to group elements from Revit and IFC models (in BIMJSON-CSV format) with just a few filters and find the required volume values for the grouped elements.

OpenDataBIM 9 Jan 06, 2023
Viewer for NFO files

NFO Viewer NFO Viewer is a simple viewer for NFO files, which are "ASCII" art in the CP437 codepage. The advantages of using NFO Viewer instead of a t

Osmo Salomaa 114 Dec 29, 2022
CMPE 204 Modelling Project

CISC/CMPE 204 Modelling Project Welcome to the major project for CISC/CMPE 204 (Fall 2021)! Change this README.md file to summarize your project (few

totallyrin 2 May 16, 2022
Vaccine for STOP/DJVU ransomware, prevents encryption

STOP/DJVU Ransomware Vaccine Prevents STOP/DJVU Ransomware from encrypting your files. This tool does not prevent the infection itself. STOP ransomwar

Karsten Hahn 16 May 31, 2022
Python decorator for `TODO`s

Python decorator for `TODO`s. Don't let your TODOs rot in your python projects anymore !

Klemen Sever 74 Sep 13, 2022
Consolemenu on python with pynput

ConsoleMenu Consolemenu on python 3 with pynput Powered by pynput and colorama Description Модуль позволяющий сделать меню выбора с помощью стрелок дл

KrouZ_CZ 2 Nov 15, 2021
Python Monopoly Simulator

Monopoly simulator Original creator: Games Computer Play YouTube: https://www.youtube.com/channel/UCTrp88f-QJ1SqKX8o5IDhWQ Config file (optional) conf

Games Computers Play 37 Jan 03, 2023
A clipboard where a user can add and retrieve multiple items to and from (resp) from the clipboard cache.

A clipboard where a user can add and retrieve multiple items to and from (resp) from the clipboard cache.

Gaurav Bhattacharjee 2 Feb 07, 2022
1 May 12, 2022
To effectively detect the faulty wafers

wafer_fault_detection Aim of the project: In electronics, a wafer (also called a slice or substrate) is a thin slice of semiconductor, such as crystal

Arun Singh Babal 1 Nov 06, 2021
dynamically create __slots__ objects with less code

slots_factory Factory functions and decorators for creating slot objects Slots are a python construct that allows users to create an object that doesn

Michael Green 2 Sep 07, 2021
Holographic Declarative Memory for Python ACT-R

HDM This is the repository for the Holographic Declarative Memory (HDM) module for Python ACT-R. This repository contains: documentation: a paper, con

Carleton Cognitive Modeling Lab 1 Jan 17, 2022