11.1 การแสดงผล (Output Formatting)
11.2 เทมเพลต (Templating)
11.3 ข้อมูลไบนารี (Working with Binary Data Record Layouts)
11.4 เธรด (Multi-threading)
11.5 ปูม (Logging)
11.6 กำจัดจุดอ่อน (Weak References)
11.7 ใช้ลิสต์ให้สะดวก (Tools for Working with Lists)
11.8 เลขทศนิยมลอย (Decimal Floating Point Arithmetic)
reprrepr() ให้เป็นแบบที่เราต้องการ
>>> import repr
>>> repr.repr(set('supercalifragilisticexpialidocious'))
"set(['a', 'c', 'd', 'e', 'f', 'g', ...])"pprint>>> import pprint >>> t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta', ... 'yellow'], 'blue']]] ... >>> pprint.pprint(t, width=30) [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta', 'yellow'], 'blue']]]
textwrap>>> import textwrap >>> doc = """The wrap() method is just like fill() except that it returns ... a list of strings instead of one big string with newlines to separate ... the wrapped lines.""" ... >>> print textwrap.fill(doc, width=40) The wrap() method is just like fill() except that it returns a list of strings instead of one big string with newlines to separate the wrapped lines.
locale>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'English_United States.1252')
'English_United States.1252'
>>> conv = locale.localeconv() # get a mapping of conventions
>>> x = 1234567.8
>>> locale.format("%d", x, grouping=True)
'1,234,567'
>>> locale.format("%s%.*f", (conv['currency_symbol'],
... conv['frac_digits'], x), grouping=True)
'$1,234,567.80'string"$" ใส่ข้างหน้าตัวแปร แล้วแทนค่าตอนรัน (แต่ถ้าต้องการใช้ $ ก็แค่ใส่เป็น "$$")
>>> from string import Template
>>> t = Template('${village}folk send $$10 to $cause.')
>>> t.substitute(village='Nottingham', cause='the ditch fund')
'Nottinghamfolk send $10 to the ditch fund.'
ถ้าใส่ค่าตัวแปรในดิกชันนารีผิด เมธอด substitute จะยกข้อผิดพลาด KeyError ขึ้นแสดง ในงานบางประเภทผู้ใช้อาจใส่ค่าไม่ครบ ก็มีเมธอด safe_substitute เพื่อป้องกันการแสดงข้อความผิดพลาดได้
>>> t = Template('Return the $item to $owner.')
>>> d = dict(item='unladen swallow')
>>> t.substitute(d)
Traceback (most recent call last):
. . .
KeyError: 'owner'
>>> t.safe_substitute(d)
'Return the unladen swallow to $owner.'
เรายังสามารถสร้างคลาสลูกให้กับ Template ในการแปลงสัญลักษณ์ระบุตัวแปรได้ ตามตัวอย่างเปลี่ยนจากสัญลักษณ์ $ เป็น %
>>> import time, os.path
>>> photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']
>>> class BatchRename(Template):
... delimiter = '%'
>>> fmt = raw_input('Enter rename style (%d-date %n-seqnum %f-format): ')
Enter rename style (%d-date %n-seqnum %f-format): Ashley_%n%f
>>> t = BatchRename(fmt)
>>> date = time.strftime('%d%b%y')
>>> for i, filename in enumerate(photofiles):
... base, ext = os.path.splitext(filename)
... newname = t.substitute(d=date, n=i, f=ext)
... print '%s --> %s' % (filename, newname)
img_1074.jpg --> Ashley_0.jpg
img_1076.jpg --> Ashley_1.jpg
img_1077.jpg --> Ashley_2.jpgเทมเพลตแบบนี้เหมาะกับงานรายงาน หรืองาน XML และ HTML
structpack() และ unpack() ในการทำงานกับข้อมูลไบนารีที่มีความยาวไม่คงที่ ตัวอย่างจะเป็นการวนรอบทำงานกับข้อมูลส่วนหัวของไฟล์ ZIP (ใช้สัญลักษณ์ "H" และ "L" แทนข้อมูลไบนารีแบบไม่นับเครื่องหมายขนาด 2 และ 4 ไบต์ ตามลำดับ)
import struct
data = open('myfile.zip', 'rb').read()
start = 0
for i in range(3): # show the first 3 file headers
start += 14
fields = struct.unpack('LLLHH', data[start:start+16])
crc32, comp_size, uncomp_size, filenamesize, extra_size = fields
start += 16
filename = data[start:start+filenamesize]
start += filenamesize
extra = data[start:start+extra_size]
print filename, hex(crc32), comp_size, uncomp_size
start += extra_size + comp_size # skip to the next header
เธรด เป็นการกระจายงานแล้วทำขนานกันไป โปรแกรมยุคใหม่จำเป็นต้องมีอย่างยิ่ง งานที่เหมาะกับเธรด เช่น เธรดนึงรอผู้ใช้ป้อนข้อมูล อีกเธรดนึงก็ทำงานอื่นขนานกันไป พอผู้ใช้ป้อนเสร็จก็ประมวลผลเสร็จพอดี เป็นต้น
threadingimport threading, zipfile
class AsyncZip(threading.Thread):
def __init__(self, infile, outfile):
threading.Thread.__init__(self)
self.infile = infile
self.outfile = outfile
def run(self):
f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
f.write(self.infile)
f.close()
print 'Finished background zip of: ', self.infile
background = AsyncZip('mydata.txt', 'myarchive.zip')
background.start()
print 'The main program continues to run in foreground.'
background.join() # Wait for the background task to finish
print 'Main program waited until background was done.'
จุดที่ยากของงานเธรด คือถ้าต้องมีการแลกเปลี่ยนข้อมูลกับโปรแกรมอื่น ๆ มอดูล threading นี้ เตรียมคลาสหรือฟังก์ชันในการนี้ไว้แล้ว เช่น locks, events, condition variables, and semaphores
Queueงานบันทึกปูม เป็นงานน่าเบื่อหน่อย แต่ถ้างานเราใหญ่ขึ้นก็ต้องทำ
loggingsys.stderr
import logging
logging.debug('Debugging information')
logging.info('Informational message')
logging.warning('Warning:config file %s not found', 'server.conf')
logging.error('Error occurred')
logging.critical('Critical error -- shutting down')ให้ผลแบบนี้
WARNING:root:Warning:config file server.conf not found ERROR:root:Error occurred CRITICAL:root:Critical error -- shutting down
ค่าปริยายของการส่งผลงานปูมคือ การแจ้งข้อผิดพลาดของระบบ (standard error) แต่เราสามารถดักให้ส่งไปยังจุดอื่นได้ เช่น อีเมล ดาต้าแกรม ซอคเก็ต หรือแม้กระทั่งแม่ข่ายเว็บ และยังอาจเลือกจ่ายไปยังจุดต่าง ๆ แบ่งตามระดับความสำคัญของข้อมูลคือ DEBUG, INFO, WARNING, ERROR, และ CRITICAL
นอกจากนี้ยังสามารถแยกทำเป็นไฟล์ปรับตั้งสำหรับงานปูมโดยเฉพาะ โดยไม่ต้องเข้าไปยุ่งกับโค๊ดหลักเลย
ปกติไพธอนจัดการหน่วยความจำได้ดีอยู่แล้ว แต่มีบางงานที่ยกเว้น
weakref>>> import weakref, gc
>>> class A:
... def __init__(self, value):
... self.value = value
... def __repr__(self):
... return str(self.value)
...
>>> a = A(10) # create a reference
>>> d = weakref.WeakValueDictionary()
>>> d['primary'] = a # does not create a reference
>>> d['primary'] # fetch the object if it is still alive
10
>>> del a # remove the one reference
>>> gc.collect() # run garbage collection right away
0
>>> d['primary'] # entry was automatically removed
Traceback (most recent call last):
File "<pyshell#108>", line 1, in -toplevel-
d['primary'] # entry was automatically removed
File "C:/PY24/lib/weakref.py", line 46, in __getitem__
o = self.data[key]()
KeyError: 'primary'เพราะลิสต์ใช้ง่าย เลยมีคนเขียนมอดูลเติมความสามารถให้ใช้ได้หลายหลายยิ่งขึ้น
array"H" แปลงแล้วใช้งานง่ายมาก
>>> from array import array
>>> a = array('H', [4000, 10, 700, 22222])
>>> sum(a)
26932
>>> a[1:3]
array('H', [10, 700])collectionsdeque() ที่ใช้งานเหมือนลิสต์ ถ้าเพิ่มหรือลดสมาชิกข้างหน้าหรือต่อท้ายแล้วจะเร็วกว่าลิสต์ แต่ถ้าแทรกหรือค้นข้อมูลจะช้ากว่า เราเลยเอามาใช้ในงานคิว
>>> from collections import deque
>>> d = deque(["task1", "task2", "task3"])
>>> d.append("task4")
>>> print "Handling", d.popleft()
Handling task1กับงานค้นแบบ Breadth-first search
unsearched = deque([starting_node])
def breadth_first_search(unsearched):
node = unsearched.popleft()
for m in gen_moves(node):
if is_goal(m):
return m
unsearched.append(m)bisect>>> import bisect >>> scores = [(100, 'perl'), (200, 'tcl'), (400, 'lua'), (500, 'python')] >>> bisect.insort(scores, (300, 'ruby')) >>> scores [(100, 'perl'), (200, 'tcl'), (300, 'ruby'), (400, 'lua'), (500, 'python')]
heapq>>> from heapq import heapify, heappop, heappush >>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0] >>> heapify(data) # rearrange the list into heap order >>> heappush(data, -5) # add a new entry >>> [heappop(data) for i in range(3)] # fetch the three smallest entries [-5, 0, 1]
decimalDecimal สำหรับเลขทศนิยมลอยที่เหมาะกับงานบัญชี และงานที่ต้องการความถูกต้องของทศนิยมสูงDecimal กับใช้ทศนิยมลอยของระบบ
>>> from decimal import *
>>> Decimal('0.70') * Decimal('1.05')
Decimal("0.7350")
>>> .70 * 1.05
0.73499999999999999
ฟังก์ชัน Decimal จะคำนวนเหมือนเราคำนวนด้วยมือ จากตัวอย่างคือการคูณเลขทศนิยม 2 ตำแหน่งเข้าด้วยกัน ดังนั้นผลลัพธ์จะมีทศนิยม 4 ตำแหน่ง ซึ่งมีความถูกต้องกว่าการใช้ทศนิยมลอยของระบบ
ดูตัวอย่างข้อผิดพลาดจากการหาเศษผลหาร
>>> Decimal('1.00') % Decimal('.10')
Decimal("0.00")
>>> 1.00 % 0.10
0.09999999999999995และความผิดพลาดจากการจัดการตัวเลขในลิสต์
>>> sum([Decimal('0.1')]*10) == Decimal('1.0')
True
>>> sum([0.1]*10) == 1.0
Falseนอกจากนี้ ยังสามารถกำหนดความละเอียดของทศนิยมได้เท่าที่เราต้องการ
>>> getcontext().prec = 36
>>> Decimal(1) / Decimal(7)
Decimal("0.142857142857142857142857142857142857")