Skip to content

Commit 7e41746

Browse files
authored
Merge pull request #82 from stonebig/master
better understanding
2 parents f8e48db + 6f67cb8 commit 7e41746

File tree

5 files changed

+627
-0
lines changed

5 files changed

+627
-0
lines changed
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
# to run on command line to see result, not on IDLE
2+
import os
3+
import os.path
4+
import pickle
5+
from threading import Thread
6+
import interpreters
7+
8+
9+
SCRIPT_DIR = os.path.dirname(__file__)
10+
11+
12+
def section_header(label):
13+
print()
14+
print('####################')
15+
print(f'# {label}')
16+
print('####################')
17+
print()
18+
19+
20+
#############################
21+
22+
interp = interpreters.create()
23+
24+
interp.run(f'''if True:
25+
def get_question(answer):
26+
return '<question for {{}}>'.format(answer)
27+
''')
28+
29+
interp.run(f'''if True:
30+
import os
31+
import pickle
32+
33+
def handle_data(data):
34+
print('handling data: {{!r}}'.format(data))
35+
''')
36+
37+
38+
#############################
39+
40+
section_header('example 1')
41+
42+
"""
43+
>>> data1, data2 = 42, 'spam'
44+
>>> interp.run(f'''if True:
45+
... q = get_question({data1})
46+
... write(q)
47+
... write({data2!r})
48+
... ''')
49+
"""
50+
51+
data1, data2 = 42, 'spam'
52+
interp.run(f'''if True:
53+
q = get_question({data1})
54+
print(q)
55+
print({data2!r})
56+
''')
57+
58+
59+
#############################
60+
61+
section_header('example 2')
62+
63+
"""
64+
>>> r, w = os.pipe()
65+
>>> def handler():
66+
... interp.run(f'''if True:
67+
... handle_data( os.read({r}, 100) )
68+
... ''')
69+
...
70+
>>> Thread(target=handler).start()
71+
>>> os.write(w, b'spam')
72+
"""
73+
74+
r, w = os.pipe()
75+
76+
def handler():
77+
interp.run(f'''if True:
78+
data = os.read({r}, 100)
79+
handle_data(data)
80+
''')
81+
82+
t = Thread(target=handler)
83+
t.start()
84+
85+
os.write(w, b'spam')
86+
87+
t.join()
88+
os.close(r)
89+
os.close(w)
90+
91+
92+
#############################
93+
94+
section_header('example 3')
95+
96+
"""
97+
>>> r, w = os.pipe()
98+
>>> r2, w2 = os.pipe()
99+
>>> def handler():
100+
... interp.run(f'''if True:
101+
... data = os.read({r}, 100)
102+
... while True:
103+
... handle_data(data)
104+
... os.write({w2}, b'<ACK>')
105+
... data = os.read({r}, 100)
106+
... ''')
107+
...
108+
>>> Thread(target=handler).start()
109+
>>> os.write(w, b'spam')
110+
>>> os.read(r2, 100)
111+
"""
112+
113+
r, w = os.pipe()
114+
r2, w2 = os.pipe()
115+
116+
def handler():
117+
interp.run(f'''if True:
118+
data = os.read({r}, 100)
119+
while data != b'<DONE>':
120+
handle_data(data)
121+
os.write({w2}, data)
122+
data = os.read({r}, 100)
123+
print('handler done!')
124+
''')
125+
126+
t = Thread(target=handler)
127+
t.start()
128+
129+
os.write(w, b'spam')
130+
assert os.read(r2, 100) == b'spam'
131+
os.write(w, b'eggs')
132+
assert os.read(r2, 100) == b'eggs'
133+
os.write(w, b'<DONE>')
134+
135+
t.join()
136+
os.close(r)
137+
os.close(w)
138+
os.close(r2)
139+
os.close(w2)
140+
141+
142+
#############################
143+
144+
section_header('example 4')
145+
146+
"""
147+
>>> r, w = os.pipe()
148+
>>> def handler():
149+
... interp.run(f'''if True:
150+
... data = pickle.load( os.open({r}) )
151+
... handle_data(data)
152+
... ''')
153+
...
154+
>>> Thread(target=handler).start()
155+
>>> pickle.dump('spam', os.open(w, 'w'))
156+
"""
157+
158+
r, w = os.pipe()
159+
r2, w2 = os.pipe()
160+
161+
def handler():
162+
interp.run(f'''if True:
163+
data = os.read({r}, 100)
164+
while data != b'<DONE>':
165+
data = pickle.loads(data)
166+
handle_data(data)
167+
os.write({w2}, data if isinstance(data, bytes) else data.encode('utf-8'))
168+
data = os.read({r}, 100)
169+
#with os.fdopen({r}, 'rb') as infile:
170+
# data = pickle.load(infile)
171+
# while data != b'<DONE>':
172+
# os.write({w2}, data if isinstance(data, bytes) else data.encode('utf-8'))
173+
# handle_data(data)
174+
# data = pickle.load(infile)
175+
print('handler done!')
176+
''')
177+
178+
t = Thread(target=handler)
179+
t.start()
180+
181+
os.write(w, pickle.dumps('spam'))
182+
assert (v := os.read(r2, 100)) == b'spam', v
183+
os.write(w, pickle.dumps('eggs'))
184+
assert (v := os.read(r2, 100)) == b'eggs', v
185+
os.write(w, b'<DONE>')
186+
#with os.fdopen(w, 'wb') as outfile:
187+
# pickle.dump('spam', outfile)
188+
# assert os.read(r2, 100) == 'spam'
189+
# pickle.dump('eggs', outfile)
190+
# assert os.read(r2, 100) == 'eggs'
191+
# pickle.dump(b'<DONE>', outfile)
192+
193+
t.join()
194+
os.close(r2)
195+
os.close(w2)
196+
197+
198+
#############################
199+
200+
section_header('example 5')
201+
202+
"""
203+
>>> rch, sch = channels.create()
204+
>>> def handler():
205+
... interp.run('''if True:
206+
... while True:
207+
... handle_data( rch.recv() )
208+
... ''', channels={'rch': rch})
209+
...
210+
>>> Thread(target=handler).start()
211+
>>> sch.send(b'spam')
212+
"""
213+
214+
#rch, sch = interpreters.create_channel()
215+
#
216+
#def handler():
217+
# interp.run('''if True:
218+
# data = rch.recv()
219+
# while data:
220+
# handle_data(data)
221+
# data = rch.recv()
222+
# ''', channels={'rch': rch})
223+
#
224+
#t = Thread(target=handler)
225+
#t.start()
226+
#
227+
#sch.send(b'spam')
228+
#sch.send(b'eggs')
229+
#sch.send(b'')
230+
#
231+
#t.join()
232+
233+
234+
#############################
235+
236+
section_header('example 6')
237+
238+
"""
239+
>>> rch, sch = channels.create()
240+
>>> def handler():
241+
... interp.run('''if True:
242+
... handle_data( rch.recv() )
243+
... ''', shared={'rch': rch})
244+
...
245+
>>> Thread(target=handler).start()
246+
>>> data = np.array([1, 2, 3])
247+
>>> sch.send(data)
248+
"""
249+
250+
#rch, sch = interpreters.create_channel()
251+
#
252+
#def handler():
253+
# interp.run('''if True:
254+
# handle_data( rch.recv() )
255+
# ''', shared={'rch': rch})
256+
#
257+
#Thread(target=handler).start()
258+
#data = np.array([1, 2, 3])
259+
#sch.send(data)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import os
2+
import sys
3+
import subprocess
4+
from test.support import interpreters
5+
6+
7+
script = '''
8+
print('Hello world!')
9+
'''
10+
11+
12+
def section_header(label):
13+
print()
14+
print('####################')
15+
print(f'# {label}')
16+
print('####################')
17+
print()
18+
19+
20+
#############################
21+
22+
interp = interpreters.create()
23+
24+
25+
#############################
26+
27+
section_header('example 1')
28+
29+
"""
30+
>>> print('Hello world!')
31+
"""
32+
33+
print('Hello world!')
34+
35+
36+
#############################
37+
38+
section_header('example 2')
39+
40+
"""
41+
$ python3 -c "
42+
print('Hello world!')
43+
"
44+
"""
45+
46+
subprocess.run(f'{sys.executable} -c "{script}"', shell=True)
47+
48+
49+
#############################
50+
51+
section_header('example 3')
52+
53+
"""
54+
$ cat << EOF > hello.py
55+
print('Hello world!')
56+
EOF
57+
$ python3 hello.py
58+
"""
59+
60+
with open('hello.py', 'w') as outfile:
61+
outfile.write(script)
62+
subprocess.run(f'{sys.executable} hello.py', shell=True)
63+
os.unlink('hello.py')
64+
65+
66+
#############################
67+
68+
section_header('example 4')
69+
70+
"""
71+
>>> script = '''
72+
print('Hello world!')
73+
'''
74+
>>> exec(script)
75+
"""
76+
77+
exec(script)
78+
79+
80+
#############################
81+
82+
section_header('example 5')
83+
84+
"""
85+
>>> script = '''
86+
print('Hello world!')
87+
'''
88+
>>> interp.run(script)
89+
"""
90+
91+
interp.run(script)

0 commit comments

Comments
 (0)