python’s standard library – part ii

19
PYTHON’S STANDARD LIBRARY – PART II

Upload: bliss

Post on 05-Jan-2016

40 views

Category:

Documents


0 download

DESCRIPTION

Python’s Standard Library – part ii. Output Formatting. Repr () Similar use to str () Used to return Python object representation that evaluates to such objects Useful for debugging and exploring objects >>> s = ‘Hello, world.’ >>> repr (s) “’Hello, world.’” >>> repr (1.0/7.0) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Python’s Standard Library – part ii

PYTHON’S STANDARD LIBRARY – PART II

Page 2: Python’s Standard Library – part ii

Repr()• Similar use to str()• Used to return Python object representation that evaluates to

such objects• Useful for debugging and exploring objects

>>> s = ‘Hello, world.’>>> repr(s)“’Hello, world.’”

>>> repr(1.0/7.0)‘0.1428557142857142857

OUTPUT FORMATTING

Page 3: Python’s Standard Library – part ii

Pprint()• Off ers more control over printing• When result is more than one line, adds breaks and indentation

to clearly reveal data structures

>>> t = [[[[‘black’, ‘cyan’], ‘white’, [‘green’, ‘red’]], [[‘magenta’, ‘yellow’], ‘blue’]]]>>> pprint.pprint(t, width=30)[[[[‘black’, ‘cyan’], ‘white’, [‘green’, ‘red’]], [[‘magenta’, ‘yellow’], ‘blue’]]]

Page 4: Python’s Standard Library – part ii

Textwrap• Formats paragraphs of text to fi t a given screen width

>>> doc = “””The wrap() method is just l ike fi ll() except that it returns a list of strings instead of one big string with newlines to separate the wrapped lines”””

>>> print(textwraplines.fi ll(doc, width=40))The wrap() method is just l ike fi ll() except that it returns a list of strings instead of one big string with newlines to separate the wrapped

Page 5: Python’s Standard Library – part ii

Template Class• Allows users to customize their applications without having to

alter the application• Uses placeholder names formed by $ with valid Python identifi ers

surrounded by braces allows it to be followed by more alphanumeric letters with no intervening spaces

>>> t = Template(‘${village}folk send $$10 to $cause.’)>>> t.substitute(village=‘Nottingham’, cause=‘the ditch fund’)‘Nottinghamfolk send $10 to the ditch fund’

TEMPLATING

Page 6: Python’s Standard Library – part ii

The substitute() method throws a KeyError when a placeholder is not supplied. You may use the safe_substitute() method to leave placeholders unchanged if data is missing.

>>> t = Template(‘Return the $item to $owner’)>>> d = dict(item=‘unladen swallor’)>>> t.substitute(d)KeyError: ‘owner’

>>> t.safe_substitute(d)‘Return the unladen swallow to $owner’

Page 7: Python’s Standard Library – part ii

Template subclasses can specify a custom delimiter. For example, a batch renaming uti l i ty for a photo browser may elect to use percent signs for placeholders such as the current date, image sequence number, or format.

>>> photofi les = [‘ img_1074.jpg’, ‘ img_1076.jpg’, ‘ img_1077.jpg’]>>> class BatchRename(Template):… delimiter = ‘%’>>> fmt = input(‘Enter rename style (%d-date, %n-seqnum, %f-fornamt):

Ashley_%n%f>>> t = BatchRename(fmt)>>> date = time.strft ime(‘%d%b%y’)>>> for I , fi lename in enumerate(photofi les):… base, ext = os.path.splitext(fi lename)… newname = t.substitute(d=date, n=1, f=ext)… print(‘{0} --> {1}’.format(fi lename, newname))img_1074.jpg --> Ashley_0.jpgimg_1076.jpg --> Ashley_1.jpgimg_1077.jpg --> Ashley_2.jpg

Page 8: Python’s Standard Library – part ii

struct() module provides pack() and unpack() functions for working with variable length binary record formats. In the fol lowing example, “H” and “I” represent two and four byte unsigned numbers respectively. The “<“ indicates they are standard size and in l itt le-endian byte order.

With open(‘myfi le.zip’, ‘rb’) as f:data = f.read()

start = 0for i in range(3): # show fi rst 3 fi le headers

start += 14fi elds = struct.unpack(‘<II IHH’, data[start:start+16])crc32, comp_size, uncomp_size, fi lenamesize, extra_size = fi elds

start += 16fi lename = data[start:start+fi lenamesize]start += fi lenamesizeextra = data[start:start+extra_size]print(fi lename, hex(crc32), comp_size, uncomp_size)

start += extra_size + comp_size # skip to the next header

BINARY DATA RECORD LAYOUTS

Page 9: Python’s Standard Library – part ii

By using threading, you can decouple tasks which are not sequential ly dependent, improving the responsiveness of appl ications that accept user input while other tasks run in the background.

class AsyncZip(threading.Thread):def __ init__(self, infi le, outfi le):

threading.Thread.__init__(self)self. infi le = infi leself.outfi le = outfi le

def run(self) :f = zipfi le.ZipFi le(self.outfi le, ‘w’, zipfi le.ZIP_DEFLATED)f.write(self. infi le)

f.close()print(‘Finished background zip of: ’ , self. infi le)

background = AsyncZip(‘mydata.txt’ , ‘myarchive.zip’)background.start()print(‘The main program continues to run in the foreground.’)

background.join() # wait for the background task to fi nishprint(‘Main program wil l wait unti l the background was done.’)

MULTI-THREADING

Page 10: Python’s Standard Library – part ii

The logging module off ers a full featured and fl exible logging system. Log messages are sent to a fi le or to sys.stdrr.

logging.debug(‘Debugging information’)logging.info(‘Informational message’)logging.warning(‘Warning:confi ge fi le %s not found’, ‘server.conf’)logging.error(‘Error occurred’)logging.critical(‘Critical error – shutting down’)

Produces the following output:

WARNING:roor:Warning:confi g fi le server.conf not foundERROR:root:Error occurredCRITICAL:root:Critical error – shutting down

By default, informational and debugging messages are suppressed and the output is sent to standard error.

LOGGING

Page 11: Python’s Standard Library – part ii

Python does automat ic memory management , and the memory i s f reed a fte r the las t re fe rence has been e l im inated. Track ing ob jec ts in Python c reates a re fe rence that makes them permanent . The weakre f module prov ides too l s for t rack ing ob jec ts w i thout c reat ing a re fe rence .

>>> c lass A:… def __ in i t __ (se l f ,va lue) :… se l f.va lue=value… def __ repr__ (se l f ) :… return s t r (se l f.va lue)

>>> a = A(10) # c reate a re fe rence>>> d = weakre f.WeakValueDic t ionary( )>>> d[ ‘pr imary ’ ] = a # does not c reate re fe rence>>> d[ ‘pr imary ’ ] # fe tch the ob jec t i f s t i l l a l i ve10>>> de l a # remove the re fe rence>>> gc .co l lec t ( ) # run garbage co l lec t0>>> d[ ‘pr imary ’ ] # entry was automat ica l l y removedTraceback (most recent ca l l l as t ) :Fi l e “<std in>” , l i ne 1 , in <module>

d[ ‘pr imary ’ ]Fi l e”C: /python34/ l ib /weakre f.py” , l i ne 46 , in __get i tem__

o.se l f.data[key] ( )KeyError: ‘pr imary ’

WEAK REFERENCES

Page 12: Python’s Standard Library – part ii

The array module provides an array() object that is like a list that stores only homogeneous data and stores it compactly. The following example shows an array of number stored as two byte unsigned binary numbers (typecode ‘H’) rather than 16 bytes per entry for regular lists.

>>> from array import array>>> a = array(‘H’, [4000, 10, 700, 22222])>>> sum(a)26932>>> a[1:3]array(‘H’, [10, 700])

TOOLS FOR WORKING WITH LISTS

Page 13: Python’s Standard Library – part ii

The collections module provides a deque() object that is l ike a l ist with faster appends and pops from the left side but slower lookups in the middle. They are useful for implementing queues and breadth fi rst tree searches

>>> from collections import deque>>> d = deque([“task1”, “task2”, “task3”])>>> d.append(“task4”)>>> print(“Handling”, d.popleft())Handling task1

unsearched = deque([starting_node])def breadth_fi rst_search(unsearched):

node = unsearched.popleft()for m in gen_moves(node):

if is_goal(m)return m

unsearched.append(m)

Page 14: Python’s Standard Library – part ii

The library also off ers other tools such as the bisect module with functions for manipulating sorted lists.

>>> 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’)]

Page 15: Python’s Standard Library – part ii

The heapq module provides functions for implementing heaps based on regular lists. The lowest valued entry is always kept at position zero. This is useful for applications that repeatedly access the smallest element but do not want to run a full list sort.

>>> from heapq import heap>>> data = [1,3,5,7,9,2,4,6,8,0]>>> heapify(data) # rearrange in heap>>> heappush(data, -5) # add new entry>>> [heappop(data) for i in range(3)] # fetch 3 smallest[-5, 0, 1]

Page 16: Python’s Standard Library – part ii

The decimal module off ers a Decimal datatype for decimal fl oating point arithmetic. Compared to the built-in fl oat implementation of binary fl oating point, the class is helpful for:

• Financial applications and others which require exact decimal representation

• Control over precision• Control over rounding for legal or regulatory means• Tracking of signifi cant decimal places• Applications where the user expect to match calculations done by

hand

>>> from decimal import *>>> round(Decimal(‘0.75’) * Decimal(‘1.05’), 2)Decimal(‘0.74’)>>> round(.70 * 1.05, 2)0.73

DECIMAL FLOATING POINT ARITHMETIC

Page 17: Python’s Standard Library – part ii

Decimal result keeps a trai l ing zero, automatically inferring four place signifi cance from multipl icands with two signifi cant places. Decimal reproduces mathematics as done by hand and avoids issues that can arise when binary fl oating point cannot exactly represent decimal quantities.

Exact representation enables the Decimal class to perform modulo calculations and equality tests that are unsuitable for binary fl oating point:

>>> Decimal(‘1.00’) % Decimal(‘.10’)Decimal(‘0.00’)>>> 1.00 % 0.100.09999999999999995

>>> sum([Decimal(‘0.1’)*10) == Decimal(‘1.0’)True>>> sum([0.1]*10 == 1.0)False

Page 18: Python’s Standard Library – part ii

The Decimal module provides arithmetic with as much precision as needed:

>>> getcontext().prec = 36>>> Decimal(1) / Decimal(7)Decimal(‘0.142857142857142857142857142857142857’)

Page 19: Python’s Standard Library – part ii

https://docs.python.org/3/tutorial/stdlib2.html

RESOURCES