11. เหลือบดูเครื่องมือเซียน (Brief Tour of the Standard Library - Part II)

 
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)

11.1 การแสดงผล (Output Formatting)

repr
มอดูล repr มีไว้สำหรับปรับฟังก์ชั่น repr() ให้เป็นแบบที่เราต้องการ
>>> 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'

11.2 เทมเพลต (Templating)

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

11.3 ข้อมูลไบนารี (Working with Binary Data Record Layouts)

struct
มอดูลนี้ใช้ฟังก์ชั่น pack() และ 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

11.4 เธรด (Multi-threading)

เธรด เป็นการกระจายงานแล้วทำขนานกันไป โปรแกรมยุคใหม่จำเป็นต้องมีอย่างยิ่ง งานที่เหมาะกับเธรด เช่น เธรดนึงรอผู้ใช้ป้อนข้อมูล อีกเธรดนึงก็ทำงานอื่นขนานกันไป พอผู้ใช้ป้อนเสร็จก็ประมวลผลเสร็จพอดี เป็นต้น
threading
ตัวอย่างเป็นการใช้มอดูลนี้บีบอัดข้อมูล โดยทำเป็นงานเบื้องหลัง
import 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
จากข้อกังวลเรื่องการแลกเปลี่ยนข้อมูลกับงานภายนอกนี่เอง จึงเกิดมอดูลนี้ขึ้นมา ทำให้เราไม่ต้องมาเขียนเรื่องจังหวะการรอของเธรดเอง

11.5 ปูม (Logging)

งานบันทึกปูม เป็นงานน่าเบื่อหน่อย แต่ถ้างานเราใหญ่ขึ้นก็ต้องทำ
logging
มอดูลนี้มีเครื่องมือให้พร้อมสำหรับการบันทึกปูม ง่ายสุดคือการส่งไปให้ระบบแสดงคือ sys.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 นอกจากนี้ยังสามารถแยกทำเป็นไฟล์ปรับตั้งสำหรับงานปูมโดยเฉพาะ โดยไม่ต้องเข้าไปยุ่งกับโค๊ดหลักเลย

11.6 กำจัดจุดอ่อน (Weak References)

ปกติไพธอนจัดการหน่วยความจำได้ดีอยู่แล้ว แต่มีบางงานที่ยกเว้น
weakref
งานตามรอยโค๊ด (tracking) เราต้องสร้างตัวแปรอ้างอิงของเราขึ้นมา หลายครั้งตัวที่เราสร้างกลับกลายเป็นปัญหาเสียเอง เวลาเราลบตัวแปรไม่หมดมันจะไปรบกวนหน่วยความจำที่เราต้องการดู มอดูล 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'

11.7 ใช้ลิสต์ให้สะดวก (Tools for Working with Lists)

เพราะลิสต์ใช้ง่าย เลยมีคนเขียนมอดูลเติมความสามารถให้ใช้ได้หลายหลายยิ่งขึ้น
array
ใช้แปลงลิสต์เป็นแอเรย์ออปเจคต์ที่เหมาะกับงานตัวเลข ตัวอย่างเป็นลิสต์ของตัวเลขที่เป็นจำนวนเต็ม (ปกติใช้หน่วยความจำ 16 ไบต์) ถูกแปลงเป็นแอเรย์ของข้อมูลไบนารีไม่คิดเครื่องหมายขนาด 2 ไบต์ (ใช้สัญลักษณ์ "H" แปลงแล้วใช้งานง่ายมาก
>>> from array import array
>>> a = array('H', [4000, 10, 700, 22222])
>>> sum(a)
26932

>>> a[1:3]
array('H', [10, 700])
collections
มอดูลนี้มีออปเจคต์ deque() ที่ใช้งานเหมือนลิสต์ ถ้าเพิ่มหรือลดสมาชิกข้างหน้าหรือต่อท้ายแล้วจะเร็วกว่าลิสต์ แต่ถ้าแทรกหรือค้นข้อมูลจะช้ากว่า เราเลยเอามาใช้ในงานคิว
>>> 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]

11.8 เลขทศนิยมลอย (Decimal Floating Point Arithmetic)

decimal
มอดูลนี้มีชนิดข้อมูลชื่อ Decimal สำหรับเลขทศนิยมลอยที่เหมาะกับงานบัญชี และงานที่ต้องการความถูกต้องของทศนิยมสูง
ตัวอย่างเป็นการคำนวณภาษีอัตราร้อยละ 5 ของค่าโทรศัพท์ 70 สตางค์ เทียบกันระหว่างใช้ 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")
 

Syndicate

Subscribe to Syndicate

Who's online

There are currently 0 users online.