Brief Tour of the Standard Library — Part II

This second tour covers more advanced modules that support professional programming needs. These modules rarely occur in small scripts.

11.1. Output Formatting

  • reprlib, provides a version of repr() customized for abbreviated displays of large or deeply nested containers

    1
    2
    3
    >>> import reprlib
    >>> reprlib.repr(set('supercalifragilisticexpialidocious'))
    "{'a', 'c', 'd', 'e', 'f', 'g', ...}"
  • pprint, offers more sophisticated control over printing both built-in and user defined objects in a way that is readable by the interpreter. (auto newline and indentation)

    1
    2
    3
    >>> pprint.pprint(sys.path)
    # vs
    >>> print(sys.path)
  • textwrap, formats paragraphs of text to fit a given screen width.

    1
    2
    3
    >>> import textwrap
    >>> longstring = 'Hello'*100
    >>> print(textwrap.fill(longstring , width = 5))
  • locale, accesses a database of culture specific data formats, this will be useful when you do international coding(lol)

11.2. Templating

string.Template all you to use template strings.

1
2
3
4
5
6
7
8
9
10
11
12
>>> from string import Template
>>> template = Template('Hello $a , here is $b') #create the template
>>> template.substitute(a = 'Bob' , b = 'Alice')
'Hello Bob , here is Alice'
>>> template.substitute(a = 'Bob' , b = 'Jack')
'Hello Bob , here is Jack'
>>> template.substitute(a = 'Bob') # key missing will raise key error
Traceback (most recent call last):
KeyError: 'b'
>>> template.safe_substitute(a = 'Bob')# safe substitute will leave the pattern and raise no exception
'Hello Bob , here is $b'
# this is where the template tower than the string format
  • Template subclasses can specify a custom delimiter

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    >>> from string import Template as T
    >>> class myt(T):
    ... delimiter = '%' # change the delimiter
    ...
    >>> t = myt('%origin ---> %new')
    >>> d = {'old':'new' , 'small' : 'big' }
    >>> for k,v in d.items():
    ... print(t.safe_substitute(origin = k,new = v))
    ...
    old ---> new
    small ---> big
  • string template makes it possible to substitute custom templates for XML files, plain text reports, and HTML web reports.

11.3. Working with Binary Data Record Layouts

The struct module provides pack() and unpack() functions for working with variable length binary record formats.

11.4. Multi-threading

Threading is a technique for decoupling tasks which are not sequentially dependent.

It can run something in the background while accepting user input.

  • the preferred approach to task coordination is to concentrate all access to a resource in a single thread and then use the queue module to feed that thread with requests from other threads.

11.5. Logging

The logging module offers a full featured and flexible logging system. At its simplest, log messages are sent to a file or to sys.stderr

1
2
3
4
5
6
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')
1
2
3
WARNING:root:Warning:config file server.conf not found
ERROR:root:Error occurred
CRITICAL:root:Critical error -- shutting down

11.6. Weak References

Python does automatic memory management (reference counting for most objects and garbage collection to eliminate cycles). The memory is freed shortly after the last reference to it has been eliminated.

The weakref module provides tools for tracking objects without creating a reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
>>> 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 "<stdin>", line 1, in <module>
d['primary'] # entry was automatically removed
File "C:/python39/lib/weakref.py", line 46, in __getitem__
o = self.data[key]()
KeyError: 'primary'

11.7. Tools for Working with Lists

  • The array module provides an array() object that is like a list that stores only homogeneous data and stores it more compactly.

    1
    2
    3
    4
    5
    6
    >>> from array import array
    >>> a = array('H', [4000, 10, 700, 22222])
    >>> sum(a)
    26932
    >>> a[1:3]
    array('H', [10, 700])
  • The collections module provides a deque() object

  • The heapq module provides functions for implementing heaps based on regular lists.

11.8. Decimal Floating Point Arithmetic

The decimal module offers a Decimal datatype for decimal floating point arithmetic.

Compared to the built-in float implementation of binary floating point, the class is especially helpful for

  • financial applications and other uses which require exact decimal representation,
  • control over precision,
  • control over rounding to meet legal or regulatory requirements,
  • tracking of significant decimal places, or
  • applications where the user expects the results to match calculations done by hand.
1
2
3
4
>>> sum([Decimal('0.1')]*10) == Decimal('1.0')
True
>>> sum([0.1]*10) == 1.0
False
1
2
3
4
5
>>> from decimal import *
>>> round(Decimal('0.70') * Decimal('1.05'), 2)
Decimal('0.74')
>>> round(.70 * 1.05, 2)
0.73
1
2
3
>>> getcontext().prec = 36
>>> Decimal(1) / Decimal(7)
Decimal('0.142857142857142857142857142857142857')