-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathworld_clock.py
92 lines (74 loc) · 3.7 KB
/
world_clock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""Demonstrating the basic capabilities of flowpipe.
A graph implementation of a world clock for demonstrational purposes:
+------------------+ +---------------------+ +----------------------+
| CurrentTime | | London | | WorldClock |
|------------------| |---------------------| |----------------------|
| time o-----+--->o time<> | % times |
+------------------+ | o timezone<0> | +--->o times.London<> |
| | converted_time o-----+--->o times.Munich<> |
| +---------------------+ +--->o times.Vancouver<> |
| +---------------------+ | +----------------------+
| | Munich | |
| |---------------------| |
|--->o time<> | |
| o timezone<1> | |
| | converted_time o-----|
| +---------------------+ |
| +---------------------+ |
| | Vancouver | |
| |---------------------| |
+--->o time<> | |
o timezone<-8> | |
| converted_time o-----+
+---------------------+
"""
from datetime import datetime
from time import time
from flowpipe import Graph, INode, InputPlug, Node, OutputPlug
@Node(outputs=["time"])
def CurrentTime():
"""The @Node decorator turns the wrapped function into a Node object.
Any arguments to the function are used as input plugs to the Node.
The outputs are defined in the decorator explicitely.
"""
return {"time": time()}
class ConvertTime(INode):
"""A node can be derived from the INode interface.
The plugs are defined in the init method.
The compute method received the inputs from any connected upstream nodes.
"""
def __init__(self, time=None, timezone=0, **kwargs):
super(ConvertTime, self).__init__(**kwargs)
InputPlug("time", self)
InputPlug("timezone", self, timezone)
OutputPlug("converted_time", self)
def compute(self, time, timezone):
return {"converted_time": time + timezone * 60 * 60}
@Node()
def ShowTimes(times):
"""Nodes do not necessarily have to define output and input plugs."""
print("-- World Clock -------------------")
for location, t in times.items():
print(
"It is now: {time:%H:%M} in {location}".format(
time=datetime.fromtimestamp(t), location=location
)
)
print("----------------------------------")
# The Graph holds the nodes
graph = Graph(name="World Clock")
current_time = CurrentTime(graph=graph)
van = ConvertTime(name="Vancouver", timezone=-8, graph=graph)
ldn = ConvertTime(name="London", timezone=0, graph=graph)
muc = ConvertTime(name="Munich", timezone=1, graph=graph)
world_clock = ShowTimes(graph=graph)
# Connecting nodes can be done via the bit shift operator as well
current_time.outputs["time"].connect(van.inputs["time"])
current_time.outputs["time"].connect(ldn.inputs["time"])
current_time.outputs["time"].connect(muc.inputs["time"])
van.outputs["converted_time"] >> world_clock.inputs["times"]["Vancouver"]
ldn.outputs["converted_time"] >> world_clock.inputs["times"]["London"]
muc.outputs["converted_time"] >> world_clock.inputs["times"]["Munich"]
# Display the graph
print(graph)
graph.evaluate()