Package: fastwire
fastwireFastwire is a simple package to facilitate communication between objects. It
provides similar functionality to several other packages, such as:blinkerwireswiredobservablepyDispatcherpymitterpy-notifyzope-eventFastwire is intended to be elegant to use, fast to implement, and more
flexible, while maintianing high performance.Basic usageCreate a signal container:importfastwireasfwsc=fw.SignalContainer()Then create a signal...signal=sc.signal('your_name')We can connect to that signal like this:classA():def__init__(self):signal.connect(self.connected)defconnected(self,a):print('Class A instance received a: '+str(a))a=B()signal.connect(a.connected)signal.emit(a=5.7)# Class A instance received a 5.7And we can connect other signals if we want to:signal_b=sc.signal('new_sig')signal_b.connect(a.connected)signal_b.emit(a=3)# Class A instance received a 3Connecting also works with functions. We can connect a function to the same
signal.deftest_fun(a):print('test_fun got a '+str(a))signal.connect(test_fun)signal.emit(a=5.7)# Class A instance received a 5.7# test_fun got a 5.7Only keyword arguments are accepted to ensure the required type of data is
passed.The emit method doesn't return anything. But the signal.fetch method does. It
requires there to be a single function or method that 'supplies' the return
value. The signal.fetch_all method returns a list of return values from
all receivers.Signal propertiessignal.nNumber of recievers.signal.receivers_presentTrue if receivers are present.nameThe name of the signal.DecoratorsIt can be convenient to use decorators to automatically connect. Do do this,
the class needs to inherit fw.Wired.signal_c=sc.signal('C')classB(fw.Wired):@fw.receives(signal_c)defconnected(self,a):print('Class B instance got '+str(a))b=B()signal_c.emit(a=7)# Class B instance got 7Functions need to use a different decorator.@fw.fn_receives(signal_c)deftest_fun_2(a):print('test_fun_2 got '+str(a))signal_c.emit(a=88)# Class B instance got 88# test_fun_2 got 88Use the @fastwire.supply decorator for methods that supply data,
and the @fastwire.fn_supply decorator for functions that supply data.ConditionsConditions can be added. They need to have a method called 'check', which is
passed combined keyword arguments from the caller and receiver. It needs to
return a boolean. If it's true, the given receiver gets the signal, if not,
it doesn't. They also need a class attribute called 'name'.Add a signal condition like this:classMy_Condition():name='default'defcheck(self,a,**kwargs):''' The main check call - must return a boolean '''ifa<10:returnTrueelse:returnFalsesignal.add_condition(My_Condition())Now, receivers only get the signal when a is less than 10:signal.emit(a=5.7)# Class A instance received a 5.7# test_fun got a 5.7signal.emit(a=15)# Nothing happensTo remove:signal.remove_condition(My_Condition.name)signal.emit(a=15)# Class A instance received a 15# test_fun got a 15Condition classes are completely open - they can be as simple as the above
example or as complex as a state machine.MutingNo receivers get a muted signal. You can mute and unmute a signal easily...signal.mute()signal.emit(a=3)# Nothing mappenssignal.unmute()signal.emit(a=3)# Class A instance received a 3# test_fun got a 3DocumentationDocumentation is hosted at ReadTheDocs.org.https://fastwire.readthedocs.io/en/latest/