基于轻量级流的编程框架。
flowpipe的Python项目详细描述
基于流的管道
python中基于流编程的轻量级框架。
安装
pip install flowpipe
示例:使用FlowPipe实现世界时钟
这非常简单,最后它只是python中的一行代码,但是一个简单的示例可以更容易地演示flowpipe背后的思想。
让我们导入必要的类:
fromdatetimeimportdatetimeimportloggingfromtimeimporttimefromflowpipeimportloggerfromflowpipe.nodeimportINode,Nodefromflowpipe.plugimportInputPlug,OutputPlugfromflowpipe.graphimportGraph
然后,所需的功能必须实现到节点中。我们需要两个节点,一个获取当前时间,一个将其转换为时区。
@Node(outputs=['time'])defCurrentTime():return{'time':time()}
上面的节点是通过快捷方式decorator创建的。
对于更复杂的节点,可以直接实现inode接口。
classConvertTime(INode):def__init__(self,time=None,timezone=0,city=None,**kwargs):super(ConvertTime,self).__init__(**kwargs)InputPlug('time',self)InputPlug('timezone',self,timezone)InputPlug('city',self,city)OutputPlug('converted_time',self)defcompute(self,time,timezone,city):return{'converted_time':[time+timezone*60*60,city]}
世界时钟将接收时间和地点并显示出来。
@Node()defWorldClock(time1,time2,time3):print('-- World Clock -------------------')print('It is now {0} in {1}'.format(datetime.fromtimestamp(time1[0]).strftime("%H:%M"),time1[1]))print('It is now {0} in {1}'.format(datetime.fromtimestamp(time2[0]).strftime("%H:%M"),time2[1]))print('It is now {0} in {1}'.format(datetime.fromtimestamp(time3[0]).strftime("%H:%M"),time3[1]))print('----------------------------------')
现在我们可以创建表示世界时钟的图形:
graph=Graph(name="WorldClockGraph")
现在我们创建所有必需的节点:
current_time=CurrentTime(graph=graph)van=ConvertTime(city='Vancouver',timezone=-8,graph=graph)ldn=ConvertTime(city='London',timezone=0,graph=graph)muc=ConvertTime(city='Munich',timezone=1,graph=graph)world_clock=WorldClock(graph=graph)
通过在节点上指定“graph”属性,将自动添加到图中。
节点现在可以连接在一起了。Bitshift运算符用作连接插头的速记。
current_time.outputs['time']>>van.inputs['time']current_time.outputs['time']>>ldn.inputs['time']current_time.outputs['time']>>muc.inputs['time']van.outputs['converted_time']>>world_clock.inputs['time1']ldn.outputs['converted_time']>>world_clock.inputs['time2']muc.outputs['converted_time']>>world_clock.inputs['time3']
图形可以可视化。
print(graph)
0 | +----------------+ +-------------------+ +---------------+
1 | | CurrentTime | | ConvertTime | | WorldClock |
2 | |----------------| |-------------------| |---------------|
3 | | time o-----+ o city<"Vancouver> | +--->o time1<> |
4 | +----------------+ +--->o time<> | |--->o time2<> |
5 | | o timezone<-8> | |--->o time3<> |
6 | | | converted_time o-----+ +---------------+
7 | | +-------------------+ |
8 | | +-------------------+ |
9 | | | ConvertTime | |
10 | | |-------------------| |
11 | | o city<"Munich"> | |
12 | |--->o time<> | |
13 | | o timezone<1> | |
14 | | | converted_time o-----|
15 | | +-------------------+ |
16 | | +-------------------+ |
17 | | | ConvertTime | |
18 | | |-------------------| |
19 | | o city<"London"> | |
20 | +--->o time<> | |
21 | o timezone<0> | |
22 | | converted_time o-----+
23 | +-------------------+
现在可以计算图形。
graph.evaluate()
-- World Clock -------------------
It is now 14:55 in Vancouver
It is now 22:55 in London
It is now 23:55 in Munich
----------------------------------
要获得更详细的输出,请将flowpipe logger设置为debug。
importloggingfromflowpipeimportloggerlogger.setLevel(logging.DEBUG)graph.evaluate()
flowpipe DEBUG: Evaluating c:\projects\flowpipe\flowpipe\graph.pyc -> Graph.compute(**{})
flowpipe DEBUG: Evaluating C:\PROJECTS\flowpipe\tests\test_a.py -> CurrentTime.compute(**{})
flowpipe DEBUG: Evaluation result for C:\PROJECTS\flowpipe\tests\test_a.py -> CurrentTime: {
"time": 1528040105.439
}
flowpipe DEBUG: Evaluating C:\PROJECTS\flowpipe\tests\test_a.py -> ConvertTime.compute(**{
"city": "Munich",
"time": 1528040105.439,
"timezone": 1
})
flowpipe DEBUG: Evaluation result for C:\PROJECTS\flowpipe\tests\test_a.py -> ConvertTime: {
"converted_time": [
1528043705.439,
"Munich"
]
}
flowpipe DEBUG: Evaluating C:\PROJECTS\flowpipe\tests\test_a.py -> ConvertTime.compute(**{
"city": "Vancouver",
"time": 1528040105.439,
"timezone": -8
})
flowpipe DEBUG: Evaluation result for C:\PROJECTS\flowpipe\tests\test_a.py -> ConvertTime: {
"converted_time": [
1528011305.439,
"Vancouver"
]
}
flowpipe DEBUG: Evaluating C:\PROJECTS\flowpipe\tests\test_a.py -> ConvertTime.compute(**{
"city": "London",
"time": 1528040105.439,
"timezone": 0
})
flowpipe DEBUG: Evaluation result for C:\PROJECTS\flowpipe\tests\test_a.py -> ConvertTime: {
"converted_time": [
1528040105.439,
"London"
]
}
flowpipe DEBUG: Evaluating C:\PROJECTS\flowpipe\tests\test_a.py -> WorldClock.compute(**{
"time1": [
1528011305.439,
"Vancouver"
],
"time2": [
1528040105.439,
"London"
],
"time3": [
1528043705.439,
"Munich"
]
})
-- World Clock -------------------
It is now 08:35 in Vancouver
It is now 16:35 in London
It is now 17:35 in Munich
----------------------------------
flowpipe DEBUG: Evaluation result for C:\PROJECTS\flowpipe\tests\test_a.py -> WorldClock: {}
flowpipe DEBUG: Evaluation result for c:\projects\flowpipe\flowpipe\graph.pyc -> Graph: {}
计划功能
- 视觉编辑器
- 芹菜集成
- API简化