updated concurrency examples
This commit is contained in:
parent
c1e50e4477
commit
73d98de6cd
@ -1,12 +0,0 @@
|
||||
<map version="0.9.0">
|
||||
<!-- To view this file, download free mind mapping software FreeMind from http://freemind.sourceforge.net -->
|
||||
<node CREATED="1421704544481" ID="ID_1903556943" MODIFIED="1421714803674" TEXT="Concurrency">
|
||||
<node CREATED="1421704705118" ID="ID_598225085" MODIFIED="1421714763865" POSITION="left" TEXT="threading"/>
|
||||
<node CREATED="1421704717848" ID="ID_1699689274" MODIFIED="1421704738402" POSITION="left" TEXT="multiprocessing"/>
|
||||
<node CREATED="1421704568997" ID="ID_48551069" MODIFIED="1421714690188" POSITION="left" TEXT="parallelism"/>
|
||||
<node CREATED="1421705811900" ID="ID_740158806" MODIFIED="1421714796092" POSITION="right" TEXT="event-based">
|
||||
<node CREATED="1421706014013" ID="ID_1223527993" MODIFIED="1421706021925" TEXT="callbacks"/>
|
||||
<node CREATED="1421706028536" ID="ID_151937226" MODIFIED="1421706038694" TEXT="coroutines"/>
|
||||
</node>
|
||||
</node>
|
||||
</map>
|
@ -62,6 +62,8 @@ import pickle
|
||||
import warnings
|
||||
|
||||
RE_WORD = re.compile('\w+')
|
||||
RE_UNICODE_NAME = re.compile('^[A-Z0-9 -]+$')
|
||||
RE_CODEPOINT = re.compile('U\+([0-9A-F]{4,6})')
|
||||
|
||||
INDEX_NAME = 'charfinder_index.pickle'
|
||||
MINIMUM_SAVE_LEN = 10000
|
||||
@ -81,6 +83,15 @@ def tokenize(text):
|
||||
for match in RE_WORD.finditer(text):
|
||||
yield match.group().upper()
|
||||
|
||||
def query_type(text):
|
||||
text_upper = text.upper()
|
||||
if 'U+' in text_upper:
|
||||
return 'CODEPOINT'
|
||||
elif RE_UNICODE_NAME.match(text_upper):
|
||||
return 'NAME'
|
||||
else:
|
||||
return 'CHARACTERS'
|
||||
|
||||
|
||||
class UnicodeNameIndex:
|
||||
|
||||
|
22
concurrency/flags/count_colors.py
Normal file
22
concurrency/flags/count_colors.py
Normal file
@ -0,0 +1,22 @@
|
||||
import tkinter
|
||||
|
||||
class Test:
|
||||
def __init__(self, master):
|
||||
|
||||
canvas = tkinter.Canvas(master)
|
||||
|
||||
canvas.image = tkinter.PhotoImage(file = 'img/br.gif')
|
||||
print(vars(canvas.image))
|
||||
|
||||
canvas.create_image(0,0, image=canvas.image, anchor=tkinter.NW)
|
||||
canvas.bind('<Button-2>', self.right_click)
|
||||
|
||||
canvas.grid(row=0, column=0)
|
||||
|
||||
def right_click(self, event):
|
||||
print(vars(event))
|
||||
raise SystemExit()
|
||||
|
||||
root = tkinter.Tk()
|
||||
test = Test(root)
|
||||
root.mainloop()
|
38
concurrency/spinner_proc.py
Normal file
38
concurrency/spinner_proc.py
Normal file
@ -0,0 +1,38 @@
|
||||
# spinner_proc.py
|
||||
# credit: Example by Michele Simionato in comp lang python.
|
||||
# source:
|
||||
# http://python-3-patterns-idioms-test.readthedocs.org/en/latest/CoroutinesAndConcurrency.html
|
||||
|
||||
import sys
|
||||
import time
|
||||
import multiprocessing
|
||||
|
||||
DELAY = 0.1
|
||||
DISPLAY = '|/-\\'
|
||||
|
||||
|
||||
def spinner_func(before='', after=''):
|
||||
write, flush = sys.stdout.write, sys.stdout.flush
|
||||
while True:
|
||||
for char in DISPLAY:
|
||||
msg = '{} {} {}'.format(before, char, after)
|
||||
write(msg)
|
||||
flush()
|
||||
write('\x08' * len(msg))
|
||||
time.sleep(DELAY)
|
||||
|
||||
|
||||
def long_computation():
|
||||
# emulate a long computation
|
||||
time.sleep(3)
|
||||
|
||||
if __name__ == '__main__':
|
||||
spinner = multiprocessing.Process(
|
||||
None, spinner_func, args=('Please wait ... ', ' thinking!'))
|
||||
spinner.start()
|
||||
|
||||
try:
|
||||
long_computation()
|
||||
print('\nComputation done')
|
||||
finally:
|
||||
spinner.terminate()
|
35
concurrency/spinner_thread.py
Normal file
35
concurrency/spinner_thread.py
Normal file
@ -0,0 +1,35 @@
|
||||
# spinner_thread.py
|
||||
# adapted from spinner_proc.py to use threads
|
||||
|
||||
import sys
|
||||
import time
|
||||
import threading
|
||||
|
||||
DELAY = 0.1
|
||||
DISPLAY = '|/-\\'
|
||||
|
||||
|
||||
def spinner_func(before='', after=''):
|
||||
write, flush = sys.stdout.write, sys.stdout.flush
|
||||
while True:
|
||||
for char in DISPLAY:
|
||||
msg = '{} {} {}'.format(before, char, after)
|
||||
write(msg)
|
||||
flush()
|
||||
write('\x08' * len(msg))
|
||||
time.sleep(DELAY)
|
||||
|
||||
|
||||
def long_computation():
|
||||
# emulate a long computation
|
||||
time.sleep(3)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
spinner = threading.Thread(
|
||||
None, spinner_func, args=('Please wait...', 'thinking!'))
|
||||
spinner.daemon = True
|
||||
spinner.start()
|
||||
|
||||
long_computation()
|
||||
print('\nComputation done')
|
@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
|
||||
from charfinder import UnicodeNameIndex, tokenize, sample_chars
|
||||
from charfinder import UnicodeNameIndex, tokenize, sample_chars, query_type
|
||||
from unicodedata import name
|
||||
|
||||
|
||||
@ -14,6 +14,10 @@ def full_index():
|
||||
return UnicodeNameIndex()
|
||||
|
||||
|
||||
def test_query_type():
|
||||
assert query_type('blue') == 'NAME'
|
||||
|
||||
|
||||
def test_tokenize():
|
||||
assert list(tokenize('')) == []
|
||||
assert list(tokenize('a b')) == ['A', 'B']
|
||||
|
@ -24,6 +24,8 @@ htmlize(): generic function example
|
||||
# BEGIN HTMLIZE
|
||||
|
||||
from functools import singledispatch
|
||||
from collections import abc
|
||||
import numbers
|
||||
import html
|
||||
|
||||
@singledispatch # <1>
|
||||
@ -36,13 +38,14 @@ def _(text): # <3>
|
||||
content = html.escape(text).replace('\n', '<br>\n')
|
||||
return '<p>{0}</p>'.format(content)
|
||||
|
||||
@htmlize.register(int) # <4>
|
||||
@htmlize.register(numbers.Integral) # <4>
|
||||
def _(n):
|
||||
return '<pre>{0} (0x{0:x})</pre>'.format(n)
|
||||
|
||||
@htmlize.register(list)
|
||||
def _(a_list):
|
||||
inner = '</li>\n<li>'.join(htmlize(item) for item in a_list)
|
||||
@htmlize.register(tuple) # <5>
|
||||
@htmlize.register(abc.MutableSequence)
|
||||
def _(seq):
|
||||
inner = '</li>\n<li>'.join(htmlize(item) for item in seq)
|
||||
return '<ul>\n<li>' + inner + '</li>\n</ul>'
|
||||
|
||||
# END HTMLIZE
|
||||
|
Loading…
x
Reference in New Issue
Block a user