diff --git a/Doc/includes/dbpickle.py b/Doc/includes/dbpickle.py --- a/Doc/includes/dbpickle.py +++ b/Doc/includes/dbpickle.py @@ -8,6 +8,7 @@ # Simple class representing a record in our database. MemoRecord = namedtuple("MemoRecord", "key, task") + class DBPickler(pickle.Pickler): def persistent_id(self, obj): @@ -47,7 +48,8 @@ def main(): - import io, pprint + import io + import pprint # Initialize and populate our database. conn = sqlite3.connect(":memory:") diff --git a/Doc/includes/minidom-example.py b/Doc/includes/minidom-example.py --- a/Doc/includes/minidom-example.py +++ b/Doc/includes/minidom-example.py @@ -18,6 +18,7 @@ dom = xml.dom.minidom.parseString(document) + def getText(nodelist): rc = [] for node in nodelist: @@ -25,6 +26,7 @@ rc.append(node.data) return ''.join(rc) + def handleSlideshow(slideshow): print("") handleSlideshowTitle(slideshow.getElementsByTagName("title")[0]) @@ -33,29 +35,36 @@ handleSlides(slides) print("") + def handleSlides(slides): for slide in slides: handleSlide(slide) + def handleSlide(slide): handleSlideTitle(slide.getElementsByTagName("title")[0]) handlePoints(slide.getElementsByTagName("point")) + def handleSlideshowTitle(title): print("%s" % getText(title.childNodes)) + def handleSlideTitle(title): print("

%s

" % getText(title.childNodes)) + def handlePoints(points): print("") + def handlePoint(point): print("
  • %s
  • " % getText(point.childNodes)) + def handleToc(slides): for slide in slides: title = slide.getElementsByTagName("title")[0] diff --git a/Doc/includes/mp_benchmarks.py b/Doc/includes/mp_benchmarks.py --- a/Doc/includes/mp_benchmarks.py +++ b/Doc/includes/mp_benchmarks.py @@ -5,7 +5,12 @@ # All rights reserved. # -import time, sys, multiprocessing, threading, queue, gc +import time +import sys +import multiprocessing +import threading +import queue +import gc if sys.platform == 'win32': _timer = time.clock @@ -28,6 +33,7 @@ q.put('STOP') + def test_queuespeed(Process, q, c): elapsed = 0 iterations = 1 @@ -51,8 +57,9 @@ p.join() - print(iterations, 'objects passed through the queue in', elapsed, 'seconds') - print('average number/sec:', iterations/elapsed) + print(iterations, 'objects passed through the queue in', + elapsed, 'seconds') + print('average number/sec:', iterations / elapsed) #### TEST_PIPESPEED @@ -68,6 +75,7 @@ c.send('STOP') + def test_pipespeed(): c, d = multiprocessing.Pipe() cond = multiprocessing.Condition() @@ -93,8 +101,9 @@ elapsed = _timer() - t p.join() - print(iterations, 'objects passed through connection in',elapsed,'seconds') - print('average number/sec:', iterations/elapsed) + print(iterations, 'objects passed through connection in', + elapsed, 'seconds') + print('average number/sec:', iterations / elapsed) #### TEST_SEQSPEED @@ -111,10 +120,10 @@ for i in range(iterations): a = seq[5] - elapsed = _timer()-t + elapsed = _timer() - t print(iterations, 'iterations in', elapsed, 'seconds') - print('average number/sec:', iterations/elapsed) + print('average number/sec:', iterations / elapsed) #### TEST_LOCK @@ -132,10 +141,10 @@ l.acquire() l.release() - elapsed = _timer()-t + elapsed = _timer() - t print(iterations, 'iterations in', elapsed, 'seconds') - print('average number/sec:', iterations/elapsed) + print('average number/sec:', iterations / elapsed) #### TEST_CONDITION @@ -150,6 +159,7 @@ c.release() + def test_conditionspeed(Process, c): elapsed = 0 iterations = 1 @@ -169,7 +179,7 @@ c.notify() c.wait() - elapsed = _timer()-t + elapsed = _timer() - t c.release() p.join() @@ -177,6 +187,7 @@ print(iterations * 2, 'waits in', elapsed, 'seconds') print('average number/sec:', iterations * 2 / elapsed) + #### def test(): diff --git a/Doc/includes/mp_newtype.py b/Doc/includes/mp_newtype.py --- a/Doc/includes/mp_newtype.py +++ b/Doc/includes/mp_newtype.py @@ -10,36 +10,42 @@ from multiprocessing.managers import BaseManager, BaseProxy import operator -## class Foo: def f(self): print('you called Foo.f()') + def g(self): print('you called Foo.g()') + def _h(self): print('you called Foo._h()') + # A simple generator function def baz(): for i in range(10): - yield i*i + yield i * i + # Proxy type for generator objects class GeneratorProxy(BaseProxy): _exposed_ = ('next', '__next__') + def __iter__(self): return self + def __next__(self): return self._callmethod('next') + def __next__(self): return self._callmethod('__next__') + # Function to return the operator module def get_operator_module(): return operator -## class MyManager(BaseManager): pass @@ -56,7 +62,6 @@ # register get_operator_module(); make public functions accessible via proxy MyManager.register('operator', get_operator_module) -## def test(): manager = MyManager() diff --git a/Doc/includes/mp_pool.py b/Doc/includes/mp_pool.py --- a/Doc/includes/mp_pool.py +++ b/Doc/includes/mp_pool.py @@ -10,10 +10,10 @@ import random import sys + # # Functions used by test code # - def calculate(func, args): result = func(*args) return '%s says that %s%s = %s' % ( @@ -21,30 +21,36 @@ func.__name__, args, result ) + def calculatestar(args): return calculate(*args) + def mul(a, b): - time.sleep(0.5*random.random()) + time.sleep(0.5 * random.random()) return a * b + def plus(a, b): - time.sleep(0.5*random.random()) + time.sleep(0.5 * random.random()) return a + b + def f(x): - return 1.0 / (x-5.0) + return 1.0 / (x - 5.0) + def pow3(x): - return x**3 + return x ** 3 + def noop(x): pass + # # Test code # - def test(): print('cpu_count() = %d\n' % multiprocessing.cpu_count()) @@ -107,9 +113,9 @@ (N, time.time() - t)) t = time.time() - C = list(pool.imap(pow3, range(N), chunksize=N//8)) + C = list(pool.imap(pow3, range(N), chunksize=N // 8)) print('\tlist(pool.imap(pow3, range(%d), chunksize=%d)):\n\t\t%s' \ - ' seconds' % (N, N//8, time.time() - t)) + ' seconds' % (N, N // 8, time.time() - t)) assert A == B == C, (len(A), len(B), len(C)) print() @@ -129,9 +135,9 @@ (time.time() - t)) t = time.time() - C = list(pool.imap(noop, L, chunksize=len(L)//8)) + C = list(pool.imap(noop, L, chunksize=len(L) // 8)) print('\tlist(pool.imap(noop, L, chunksize=%d)):\n\t\t%s seconds' % \ - (len(L)//8, time.time() - t)) + (len(L) // 8, time.time() - t)) assert A == B == C, (len(A), len(B), len(C)) print() diff --git a/Doc/includes/mp_synchronize.py b/Doc/includes/mp_synchronize.py --- a/Doc/includes/mp_synchronize.py +++ b/Doc/includes/mp_synchronize.py @@ -5,7 +5,9 @@ # All rights reserved. # -import time, sys, random +import time +import sys +import random from queue import Empty import multiprocessing # may get overwritten @@ -15,13 +17,15 @@ def value_func(running, mutex): random.seed() - time.sleep(random.random()*4) + time.sleep(random.random() * 4) mutex.acquire() - print('\n\t\t\t' + str(multiprocessing.current_process()) + ' has finished') + print('\n\t\t\t' + str(multiprocessing.current_process()) + \ + ' has finished') running.value -= 1 mutex.release() + def test_value(): TASKS = 10 running = multiprocessing.Value('i', TASKS) @@ -47,9 +51,10 @@ def queue_func(queue): for i in range(30): time.sleep(0.5 * random.random()) - queue.put(i*i) + queue.put(i * i) queue.put('STOP') + def test_queue(): q = multiprocessing.Queue() @@ -79,6 +84,7 @@ cond.notify() cond.release() + def test_condition(): cond = multiprocessing.Condition() @@ -116,7 +122,7 @@ mutex.release() random.seed() - time.sleep(random.random()*2) + time.sleep(random.random() * 2) mutex.acquire() running.value -= 1 @@ -125,6 +131,7 @@ sema.release() + def test_semaphore(): sema = multiprocessing.Semaphore(3) mutex = multiprocessing.RLock() @@ -150,6 +157,7 @@ time.sleep(5.5) print('\n\tchild terminating') + def test_join_timeout(): p = multiprocessing.Process(target=join_timeout_func) p.start() @@ -171,6 +179,7 @@ event.wait() print('\t%r has woken up' % multiprocessing.current_process()) + def test_event(): event = multiprocessing.Event() @@ -205,6 +214,7 @@ print('Tests passed') + def test_sharedvalues(): values = [ ('i', 10), @@ -237,9 +247,9 @@ multiprocessing = namespace - for func in [ test_value, test_queue, test_condition, - test_semaphore, test_join_timeout, test_event, - test_sharedvalues ]: + for func in [test_value, test_queue, test_condition, + test_semaphore, test_join_timeout, test_event, + test_sharedvalues]: print('\n\t######## %s\n' % func.__name__) func() diff --git a/Doc/includes/mp_webserver.py b/Doc/includes/mp_webserver.py --- a/Doc/includes/mp_webserver.py +++ b/Doc/includes/mp_webserver.py @@ -24,7 +24,7 @@ def note(format, *args): - sys.stderr.write('[%s]\t%s\n' % (current_process().name, format%args)) + sys.stderr.write('[%s]\t%s\n' % (current_process().name, format % args)) class RequestHandler(SimpleHTTPRequestHandler): @@ -32,6 +32,7 @@ def log_message(self, format, *args): note(format, *args) + def serve_forever(server): note('starting server') try: @@ -45,7 +46,7 @@ server = HTTPServer(address, RequestHandler) # create child processes to act as workers - for i in range(number_of_processes-1): + for i in range(number_of_processes - 1): Process(target=serve_forever, args=(server,)).start() # main process also acts as a worker @@ -59,7 +60,7 @@ print('Serving at http://%s:%d using %d worker processes' % \ (ADDRESS[0], ADDRESS[1], NUMBER_OF_PROCESSES)) - print('To exit press Ctrl-' + ['C', 'Break'][sys.platform=='win32']) + print('To exit press Ctrl-' + ['C', 'Break'][sys.platform == 'win32']) os.chdir(DIR) runpool(ADDRESS, NUMBER_OF_PROCESSES) diff --git a/Doc/includes/mp_workers.py b/Doc/includes/mp_workers.py --- a/Doc/includes/mp_workers.py +++ b/Doc/includes/mp_workers.py @@ -16,39 +16,37 @@ from multiprocessing import Process, Queue, current_process, freeze_support + # # Function run by worker processes # - def worker(input, output): for func, args in iter(input.get, 'STOP'): result = calculate(func, args) output.put(result) + # # Function used to calculate result # - def calculate(func, args): result = func(*args) return '%s says that %s%s = %s' % \ (current_process().name, func.__name__, args, result) + # # Functions referenced by tasks # - def mul(a, b): - time.sleep(0.5*random.random()) + time.sleep(0.5 * random.random()) return a * b + def plus(a, b): - time.sleep(0.5*random.random()) + time.sleep(0.5 * random.random()) return a + b -# -# -# def test(): NUMBER_OF_PROCESSES = 4 diff --git a/Doc/includes/sqlite3/adapter_datetime.py b/Doc/includes/sqlite3/adapter_datetime.py --- a/Doc/includes/sqlite3/adapter_datetime.py +++ b/Doc/includes/sqlite3/adapter_datetime.py @@ -1,5 +1,7 @@ import sqlite3 -import datetime, time +import datetime +import time + def adapt_datetime(ts): return time.mktime(ts.timetuple()) diff --git a/Doc/includes/sqlite3/adapter_point_1.py b/Doc/includes/sqlite3/adapter_point_1.py --- a/Doc/includes/sqlite3/adapter_point_1.py +++ b/Doc/includes/sqlite3/adapter_point_1.py @@ -1,5 +1,6 @@ import sqlite3 + class Point: def __init__(self, x, y): self.x, self.y = x, y diff --git a/Doc/includes/sqlite3/adapter_point_2.py b/Doc/includes/sqlite3/adapter_point_2.py --- a/Doc/includes/sqlite3/adapter_point_2.py +++ b/Doc/includes/sqlite3/adapter_point_2.py @@ -1,9 +1,11 @@ import sqlite3 + class Point: def __init__(self, x, y): self.x, self.y = x, y + def adapt_point(point): return "%f;%f" % (point.x, point.y) diff --git a/Doc/includes/sqlite3/collation_reverse.py b/Doc/includes/sqlite3/collation_reverse.py --- a/Doc/includes/sqlite3/collation_reverse.py +++ b/Doc/includes/sqlite3/collation_reverse.py @@ -1,5 +1,6 @@ import sqlite3 + def collate_reverse(string1, string2): if string1 == string2: return 0 diff --git a/Doc/includes/sqlite3/converter_point.py b/Doc/includes/sqlite3/converter_point.py --- a/Doc/includes/sqlite3/converter_point.py +++ b/Doc/includes/sqlite3/converter_point.py @@ -1,5 +1,6 @@ import sqlite3 + class Point: def __init__(self, x, y): self.x, self.y = x, y @@ -7,9 +8,11 @@ def __repr__(self): return "(%f;%f)" % (self.x, self.y) + def adapt_point(point): return "%f;%f" % (point.x, point.y) + def convert_point(s): x, y = list(map(float, s.split(";"))) return Point(x, y) diff --git a/Doc/includes/sqlite3/countcursors.py b/Doc/includes/sqlite3/countcursors.py --- a/Doc/includes/sqlite3/countcursors.py +++ b/Doc/includes/sqlite3/countcursors.py @@ -1,5 +1,6 @@ import sqlite3 + class CountCursorsConnection(sqlite3.Connection): def __init__(self, *args, **kwargs): sqlite3.Connection.__init__(self, *args, **kwargs) diff --git a/Doc/includes/sqlite3/ctx_manager.py b/Doc/includes/sqlite3/ctx_manager.py --- a/Doc/includes/sqlite3/ctx_manager.py +++ b/Doc/includes/sqlite3/ctx_manager.py @@ -1,7 +1,8 @@ import sqlite3 con = sqlite3.connect(":memory:") -con.execute("create table person (id integer primary key, firstname varchar unique)") +con.execute("create table person " + "(id integer primary key, firstname varchar unique)") # Successful, con.commit() is called automatically afterwards with con: diff --git a/Doc/includes/sqlite3/execute_1.py b/Doc/includes/sqlite3/execute_1.py --- a/Doc/includes/sqlite3/execute_1.py +++ b/Doc/includes/sqlite3/execute_1.py @@ -7,5 +7,6 @@ who = "Yeltsin" age = 72 -cur.execute("select name_last, age from people where name_last=? and age=?", (who, age)) +cur.execute("select name_last, age from people " + "where name_last=? and age=?", (who, age)) print(cur.fetchone()) diff --git a/Doc/includes/sqlite3/execute_2.py b/Doc/includes/sqlite3/execute_2.py --- a/Doc/includes/sqlite3/execute_2.py +++ b/Doc/includes/sqlite3/execute_2.py @@ -7,6 +7,7 @@ who = "Yeltsin" age = 72 -cur.execute("select name_last, age from people where name_last=:who and age=:age", - {"who": who, "age": age}) +cur.execute("select name_last, age from people " + "where name_last=:who and age=:age", + {"who": who, "age": age}) print(cur.fetchone()) diff --git a/Doc/includes/sqlite3/execute_3.py b/Doc/includes/sqlite3/execute_3.py --- a/Doc/includes/sqlite3/execute_3.py +++ b/Doc/includes/sqlite3/execute_3.py @@ -7,6 +7,6 @@ who = "Yeltsin" age = 72 -cur.execute("select name_last, age from people where name_last=:who and age=:age", - locals()) +cur.execute("select name_last, age from people " + "where name_last=:who and age=:age", locals()) print(cur.fetchone()) diff --git a/Doc/includes/sqlite3/executemany_1.py b/Doc/includes/sqlite3/executemany_1.py --- a/Doc/includes/sqlite3/executemany_1.py +++ b/Doc/includes/sqlite3/executemany_1.py @@ -1,5 +1,6 @@ import sqlite3 + class IterChars: def __init__(self): self.count = ord('a') @@ -11,7 +12,7 @@ if self.count > ord('z'): raise StopIteration self.count += 1 - return (chr(self.count - 1),) # this is a 1-tuple + return (chr(self.count - 1),) # this is a 1-tuple con = sqlite3.connect(":memory:") cur = con.cursor() diff --git a/Doc/includes/sqlite3/executemany_2.py b/Doc/includes/sqlite3/executemany_2.py --- a/Doc/includes/sqlite3/executemany_2.py +++ b/Doc/includes/sqlite3/executemany_2.py @@ -1,5 +1,6 @@ import sqlite3 + def char_generator(): import string for c in string.letters[:26]: diff --git a/Doc/includes/sqlite3/md5func.py b/Doc/includes/sqlite3/md5func.py --- a/Doc/includes/sqlite3/md5func.py +++ b/Doc/includes/sqlite3/md5func.py @@ -1,6 +1,7 @@ import sqlite3 import hashlib + def md5sum(t): return hashlib.md5(t).hexdigest() diff --git a/Doc/includes/sqlite3/mysumaggr.py b/Doc/includes/sqlite3/mysumaggr.py --- a/Doc/includes/sqlite3/mysumaggr.py +++ b/Doc/includes/sqlite3/mysumaggr.py @@ -1,5 +1,6 @@ import sqlite3 + class MySum: def __init__(self): self.count = 0 diff --git a/Doc/includes/sqlite3/pysqlite_datetime.py b/Doc/includes/sqlite3/pysqlite_datetime.py --- a/Doc/includes/sqlite3/pysqlite_datetime.py +++ b/Doc/includes/sqlite3/pysqlite_datetime.py @@ -1,7 +1,8 @@ import sqlite3 import datetime -con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) +con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES | + sqlite3.PARSE_COLNAMES) cur = con.cursor() cur.execute("create table test(d date, ts timestamp)") @@ -14,7 +15,8 @@ print(today, "=>", row[0], type(row[0])) print(now, "=>", row[1], type(row[1])) -cur.execute('select current_date as "d [date]", current_timestamp as "ts [timestamp]"') +cur.execute('select current_date as "d [date]", ' + 'current_timestamp as "ts [timestamp]"') row = cur.fetchone() print("current_date", row[0], type(row[0])) print("current_timestamp", row[1], type(row[1])) diff --git a/Doc/includes/sqlite3/row_factory.py b/Doc/includes/sqlite3/row_factory.py --- a/Doc/includes/sqlite3/row_factory.py +++ b/Doc/includes/sqlite3/row_factory.py @@ -1,5 +1,6 @@ import sqlite3 + def dict_factory(cursor, row): d = {} for idx, col in enumerate(cursor.description): diff --git a/Doc/includes/sqlite3/shortcut_methods.py b/Doc/includes/sqlite3/shortcut_methods.py --- a/Doc/includes/sqlite3/shortcut_methods.py +++ b/Doc/includes/sqlite3/shortcut_methods.py @@ -11,11 +11,13 @@ con.execute("create table person(firstname, lastname)") # Fill the table -con.executemany("insert into person(firstname, lastname) values (?, ?)", persons) +con.executemany("insert into person(firstname, lastname) values (?, ?)", + persons) # Print the table contents for row in con.execute("select firstname, lastname from person"): print(row) # Using a dummy WHERE clause to not let SQLite take the shortcut table deletes. -print("I just deleted", con.execute("delete from person where 1=1").rowcount, "rows") +print("I just deleted", con.execute("delete from person where 1=1").rowcount, + "rows") diff --git a/Doc/includes/sqlite3/simple_tableprinter.py b/Doc/includes/sqlite3/simple_tableprinter.py --- a/Doc/includes/sqlite3/simple_tableprinter.py +++ b/Doc/includes/sqlite3/simple_tableprinter.py @@ -12,7 +12,7 @@ # Print a header. for fieldDesc in cur.description: print(fieldDesc[0].ljust(FIELD_MAX_WIDTH), end=' ') -print() # Finish the header with a newline. +print() # Finish the header with a newline. print('-' * 78) # For each row, print the value of each field left-justified within @@ -23,4 +23,4 @@ fieldValue = str(row[fieldIndex]) print(fieldValue.ljust(FIELD_MAX_WIDTH), end=' ') - print() # Finish the row with a newline. + print() # Finish the row with a newline. diff --git a/Doc/includes/test.py b/Doc/includes/test.py --- a/Doc/includes/test.py +++ b/Doc/includes/test.py @@ -209,5 +209,6 @@ sys.path.append(src) if __name__ == "__main__": - import doctest, __main__ + import doctest + import __main__ doctest.testmod(__main__) diff --git a/Doc/includes/tzinfo-examples.py b/Doc/includes/tzinfo-examples.py --- a/Doc/includes/tzinfo-examples.py +++ b/Doc/includes/tzinfo-examples.py @@ -3,6 +3,7 @@ ZERO = timedelta(0) HOUR = timedelta(hours=1) + # A UTC class. class UTC(tzinfo): @@ -19,6 +20,7 @@ utc = UTC() + # A class building tzinfo objects for fixed-offset time zones. # Note that FixedOffset(0, "UTC") is a different way to build a # UTC tzinfo object. @@ -27,7 +29,7 @@ """Fixed offset in minutes east from UTC.""" def __init__(self, offset, name): - self.__offset = timedelta(minutes = offset) + self.__offset = timedelta(minutes=offset) self.__name = name def utcoffset(self, dt): @@ -43,14 +45,15 @@ import time as _time -STDOFFSET = timedelta(seconds = -_time.timezone) +STDOFFSET = timedelta(seconds=-_time.timezone) if _time.daylight: - DSTOFFSET = timedelta(seconds = -_time.altzone) + DSTOFFSET = timedelta(seconds=-_time.altzone) else: DSTOFFSET = STDOFFSET DSTDIFF = DSTOFFSET - STDOFFSET + class LocalTimezone(tzinfo): def utcoffset(self, dt): @@ -113,6 +116,7 @@ DSTSTART_1967_1986 = datetime(1, 4, 24, 2) DSTEND_1967_1986 = DSTEND_1987_2006 + class USTimeZone(tzinfo): def __init__(self, hours, reprname, stdname, dstname):