Package: pyminigasket
MiniGasket: A Tiny Flow-Based Programming LibraryMiniGasket is a tiny library for facilitating flow-based programming.InstallationInstallation is simple usingpip:$ pip install pyminigasketMiniGasket has no external dependencies.ExamplesDirecting data flows:importminigasketfromminigasketimportSourceBaseclassStringSource(minigasket.SourceBase):defemit(self,value:str):self.send(value)classToUpper(minigasket.SourceSinkBase):def__init__(self):super().__init__()defreceive(self,sender:SourceBase,message:str)->None:self.send(message.upper())classAppender(minigasket.SourceSinkBase):def__init__(self,value:str):super().__init__()self._value=valuedefreceive(self,sender:SourceBase,message)->None:self.send(message+self._value)classCollector(minigasket.SinkBase):def__init__(self):super().__init__()self.received=[]defreceive(self,sender:SourceBase,message)->None:self.received.append(message)source=StringSource()sink=Collector()source>>sinksource>>ToUpper()>>sinksource>>Appender('!')>>sinksource.emit('hello')source.emit('world')assertsink.received==['hello','HELLO','hello!','world','WORLD','world!']Creating a filter:importrandomimportminigasketfromminigasketimportSourceBaseclassRandomNumberSource(minigasket.SourceBase):defemit(self):self.send(random.randrange(100))classEvenNumberFilter(minigasket.FilterBase):defpredicate(self,sender:SourceBase,message:int)->bool:return(message%2)==0classCollector(minigasket.SinkBase):def__init__(self):super().__init__()self.received=[]defreceive(self,sender:SourceBase,message)->None:self.received.append(message)source=RandomNumberSource()even_sink=Collector()odd_sink=Collector()filt=EvenNumberFilter()source>>filt>>even_sinkfilt.rejected>>odd_sinkfor_inrange(10):source.emit()print('EVENS:',even_sink.received)print('ODDS:',odd_sink.received)Multiple sources:importminigasketclassIntSource(minigasket.SourceBase):defemit(self,value:int):self.send(value)classIncrementDecrement(minigasket.SinkBase):def__init__(self):super().__init__()self.incremented=minigasket.SourceProxy(self)self.decremented=minigasket.SourceProxy(self)defreceive(self,sender,message:int)->None:self.incremented.send(message+1)self.decremented.send(message-1)classCollector(minigasket.SinkBase):def__init__(self):super().__init__()self.received=[]defreceive(self,sender,message)->None:self.received.append(message)source=IntSource()sink_increment=Collector()sink_decrement=Collector()incdec=IncrementDecrement()source>>incdecincdec.incremented>>sink_incrementincdec.decremented>>sink_decrementsource.emit(1)source.emit(2)source.emit(3)assertsink_increment.received==[2,3,4]assertsink_decrement.received==[0,1,2]Multiple Sinks:importminigasketclassStringSource(minigasket.SourceBase):defemit(self,value:str):self.send(value)classMultiPrint(object):def__init__(self):super().__init__()self.sink_a=minigasket.SinkProxy(self.receive_a)self.sink_b=minigasket.SinkProxy(self.receive_b)defreceive_a(self,sender,message)->None:print('Got message from sink A:',message)defreceive_b(self,sender,message)->None:print('Got message from sink B:',message)source_a=StringSource()source_b=StringSource()sink=MultiPrint()source_a>>sink.sink_asource_b>>sink.sink_bsource_a.emit('Hello to sink A!')source_b.emit('Hello to sink B!')