Skip to content

Commit 73d98de

Browse files
committed
updated concurrency examples
1 parent c1e50e4 commit 73d98de

File tree

7 files changed

+118
-17
lines changed

7 files changed

+118
-17
lines changed

concurrency/Concurrency.mm

Lines changed: 0 additions & 12 deletions
This file was deleted.

concurrency/charfinder.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
import warnings
6363

6464
RE_WORD = re.compile('\w+')
65+
RE_UNICODE_NAME = re.compile('^[A-Z0-9 -]+$')
66+
RE_CODEPOINT = re.compile('U\+([0-9A-F]{4,6})')
6567

6668
INDEX_NAME = 'charfinder_index.pickle'
6769
MINIMUM_SAVE_LEN = 10000
@@ -81,6 +83,15 @@ def tokenize(text):
8183
for match in RE_WORD.finditer(text):
8284
yield match.group().upper()
8385

86+
def query_type(text):
87+
text_upper = text.upper()
88+
if 'U+' in text_upper:
89+
return 'CODEPOINT'
90+
elif RE_UNICODE_NAME.match(text_upper):
91+
return 'NAME'
92+
else:
93+
return 'CHARACTERS'
94+
8495

8596
class UnicodeNameIndex:
8697

concurrency/flags/count_colors.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import tkinter
2+
3+
class Test:
4+
def __init__(self, master):
5+
6+
canvas = tkinter.Canvas(master)
7+
8+
canvas.image = tkinter.PhotoImage(file = 'img/br.gif')
9+
print(vars(canvas.image))
10+
11+
canvas.create_image(0,0, image=canvas.image, anchor=tkinter.NW)
12+
canvas.bind('<Button-2>', self.right_click)
13+
14+
canvas.grid(row=0, column=0)
15+
16+
def right_click(self, event):
17+
print(vars(event))
18+
raise SystemExit()
19+
20+
root = tkinter.Tk()
21+
test = Test(root)
22+
root.mainloop()

concurrency/spinner_proc.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# spinner_proc.py
2+
# credit: Example by Michele Simionato in comp lang python.
3+
# source:
4+
# http://python-3-patterns-idioms-test.readthedocs.org/en/latest/CoroutinesAndConcurrency.html
5+
6+
import sys
7+
import time
8+
import multiprocessing
9+
10+
DELAY = 0.1
11+
DISPLAY = '|/-\\'
12+
13+
14+
def spinner_func(before='', after=''):
15+
write, flush = sys.stdout.write, sys.stdout.flush
16+
while True:
17+
for char in DISPLAY:
18+
msg = '{} {} {}'.format(before, char, after)
19+
write(msg)
20+
flush()
21+
write('\x08' * len(msg))
22+
time.sleep(DELAY)
23+
24+
25+
def long_computation():
26+
# emulate a long computation
27+
time.sleep(3)
28+
29+
if __name__ == '__main__':
30+
spinner = multiprocessing.Process(
31+
None, spinner_func, args=('Please wait ... ', ' thinking!'))
32+
spinner.start()
33+
34+
try:
35+
long_computation()
36+
print('\nComputation done')
37+
finally:
38+
spinner.terminate()

concurrency/spinner_thread.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# spinner_thread.py
2+
# adapted from spinner_proc.py to use threads
3+
4+
import sys
5+
import time
6+
import threading
7+
8+
DELAY = 0.1
9+
DISPLAY = '|/-\\'
10+
11+
12+
def spinner_func(before='', after=''):
13+
write, flush = sys.stdout.write, sys.stdout.flush
14+
while True:
15+
for char in DISPLAY:
16+
msg = '{} {} {}'.format(before, char, after)
17+
write(msg)
18+
flush()
19+
write('\x08' * len(msg))
20+
time.sleep(DELAY)
21+
22+
23+
def long_computation():
24+
# emulate a long computation
25+
time.sleep(3)
26+
27+
28+
if __name__ == '__main__':
29+
spinner = threading.Thread(
30+
None, spinner_func, args=('Please wait...', 'thinking!'))
31+
spinner.daemon = True
32+
spinner.start()
33+
34+
long_computation()
35+
print('\nComputation done')

concurrency/test_charfinder.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from charfinder import UnicodeNameIndex, tokenize, sample_chars
3+
from charfinder import UnicodeNameIndex, tokenize, sample_chars, query_type
44
from unicodedata import name
55

66

@@ -14,6 +14,10 @@ def full_index():
1414
return UnicodeNameIndex()
1515

1616

17+
def test_query_type():
18+
assert query_type('blue') == 'NAME'
19+
20+
1721
def test_tokenize():
1822
assert list(tokenize('')) == []
1923
assert list(tokenize('a b')) == ['A', 'B']

decorators/generic.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
# BEGIN HTMLIZE
2525

2626
from functools import singledispatch
27+
from collections import abc
28+
import numbers
2729
import html
2830

2931
@singledispatch # <1>
@@ -36,13 +38,14 @@ def _(text): # <3>
3638
content = html.escape(text).replace('\n', '<br>\n')
3739
return '<p>{0}</p>'.format(content)
3840

39-
@htmlize.register(int) # <4>
41+
@htmlize.register(numbers.Integral) # <4>
4042
def _(n):
4143
return '<pre>{0} (0x{0:x})</pre>'.format(n)
4244

43-
@htmlize.register(list)
44-
def _(a_list):
45-
inner = '</li>\n<li>'.join(htmlize(item) for item in a_list)
45+
@htmlize.register(tuple) # <5>
46+
@htmlize.register(abc.MutableSequence)
47+
def _(seq):
48+
inner = '</li>\n<li>'.join(htmlize(item) for item in seq)
4649
return '<ul>\n<li>' + inner + '</li>\n</ul>'
4750

4851
# END HTMLIZE

0 commit comments

Comments
 (0)