-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_functor2.py
More file actions
198 lines (180 loc) · 6.7 KB
/
python_functor2.py
File metadata and controls
198 lines (180 loc) · 6.7 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from __future__ import print_function
import urllib
import urllib2
#import urllib3
import re
import serial
import time
import httplib
class Functor(object):
def __init__(self, *_args, **_kargs):
self._args_list = _args
if("own_args" in _kargs):
self._own_args = _kargs["own_args"]
else:
if len(_args) > 0:
self._own_args = True
else:
self._own_args = False
self.args_list = [ ]
self.debug = True
self.called = False
def my_print(self, *args):
if self.debug:
print(*args)
def do_expr(self):
pass
def do_expr_iter(self):
self.my_print( self, "Got called with args: ", self._args_list)
lst = []
for i in self._args_list:
if i is self:
continue
if isinstance(i, Functor) and not i is self and not i.called:
self.my_print( i, " is instance of Functor")
res = None
if(i._own_args == True):
self.my_print( "Calling ",i," with its own args")
res = i.__call__()
if res != None:
self.my_print("Appending result ", res)
lst.append(res)
else:
self.my_print( "Calling ",i," with our args")
res = i.__call__(*(self._args_list))
if res != None:
args_list = [ ]
for j in i.args_list:
args_list.append(res)
_args_list = [ n for n in self._args_list if isinstance(n, Functor) ]
_args_list.extend(args_list)
self._args_list = _args_list
self.args_list = args_list
self.my_print( self,"'s new arg list: ", self._args_list)
self.my_print( self,"'s new arg list without functors: ", self.args_list)
args_list = [ i for i in self._args_list if not isinstance(i, Functor) ]
self.args_list = args_list
#self.args_list.extend(lst)
self.my_print( "Final argument list:", self.args_list)
self.called = True
return self.do_expr()
def __call__(self, *_args):
if self.called:
return self._res
self.my_print( "__call__:", _args, len(_args))
functors = [ n for n in self._args_list if isinstance(n, Functor) ]
l = [ i for i in functors if not i._own_args ]
self.my_print( "fillowing elements do not have args: ", l)
is_all_own_args = len(l) == 0
if len(_args) > 0:
if is_all_own_args == False:
lst = [ i for i in self._args_list if isinstance(i, Functor) ]
self._args_list = lst
for j in _args:
self._args_list.append(j)
else:
self._args_list = _args
self.my_print( "__call__ calling with:", self._args_list)
self._res = self.do_expr_iter()
return self._res
class Add(Functor):
def __init__(self, *_args, **_kargs):
Functor.__init__(self,*_args, **_kargs)
def do_expr(self):
self.my_print( "add", self.args_list)
if len(self.args_list) == 0:
return 0
return sum(self.args_list)
class Mul(Functor):
def __init__(self, *_args, **_kargs):
Functor.__init__(self,*_args, **_kargs)
def do_expr(self):
self.my_print( "mul", self.args_list)
if len(self.args_list) == 0:
return 0
return reduce(lambda x,y: x*y, self.args_list)
class Div(Functor):
def __init__(self, *_args, **_kargs):
Functor.__init__(self,*_args, **_kargs)
def do_expr(self):
self.my_print( "div", self.args_list)
if len(self.args_list) == 0:
return 0
# print self._args_list
return reduce(lambda x,y: x/y, self.args_list)
class Print(Functor):
def __init__(self, *_args, **_kargs):
Functor.__init__(self,*_args, **_kargs)
def do_expr(self):
print( "Print:", self.args_list)
def func(*lst):
mul = 1
for i in lst:
mul = mul * i
return mul
class FastHTTPResponse(httplib.HTTPResponse):
def __init__(self, sock, debuglevel=0, strict=0):
httplib.HTTPResponse.__init__(self, sock, debuglevel, strict)
self.fp = sock.makefile('rb', 8192)
# Tell the httplib that we want to use our hack.
if __name__ == "__main__":
print ("HELLO")
exit(0)
#httplib.HTTPConnection.response_class = FastHTTPResponse
i=0
url = "http://stanfoxarduino-stanfoxarduino.rhcloud.com/index.php"
data = {"clear": "FErArg"}
data = urllib.urlencode(data)
ser = serial.Serial('/dev/ttyACM1', 9600, timeout=1)
#request = urllib2.Request(url + '?' + data)
#response = urllib2.urlopen(request)
#page = response.read()
while(True):
i = i + 1
print("fdfdfd ", i)
print("sending request")
http = urllib3.PoolManager(10)
#request = urllib2.Request(url)
#response = urllib2.urlopen(request)
response = http.request('GET',url)
r = re.compile('"[a-zA-Z0-9]+":"[a-zA-Z0-9]+"')
page = response.data #response.read()
print("end")
print(page)
res = r.findall(page)
print( res)
print(str(res).replace('[','{').replace(']','}'))
d = eval(str(res).replace('[','{').replace(']','}').replace('\'',''))
print(d)
#time.sleep(0.02)
if 'command' in d:
if d['command'] != "prev":
print ("writing "+ d['command'])
ser.write(d["command"])
time.sleep(.5)
#ser.write(d["command"])
print("reading")
resp = ser.read(20)
resp = resp.replace('pulse','')
resp = resp.replace(d['command'],'')
resp = resp.replace("Pong",'')
ser.flushInput()
ser.flushOutput()
time.sleep(.1)
print("end")
print(resp)
print("sending request")
response = http.request('GET', url + '?' + data)
#request = urllib2.Request(url + '?' + data)
#response = urllib2.urlopen(request)
page = response.data #response.read()
print("end")
if len(resp) > 0:
data2 = { "add": "FErArg", "command": "prev", "reply" : resp }
data2 = urllib.urlencode(data2)
print("sending request")
#request2 = urllib2.Request(url + '?' + data2)
response = http.request('GET', url + '?' + data2)
#response = urllib2.urlopen(request2)
page = response.data#response.read()
print("end")