Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python’s Standard Library Part 2 Francis Ryan. Output formatting, 1 repr module – When using large or deeply nested containers, allows better printing.

Similar presentations


Presentation on theme: "Python’s Standard Library Part 2 Francis Ryan. Output formatting, 1 repr module – When using large or deeply nested containers, allows better printing."— Presentation transcript:

1 Python’s Standard Library Part 2 Francis Ryan

2 Output formatting, 1 repr module – When using large or deeply nested containers, allows better printing using repr function >>> import repr >>> repr.repr(list('this is a list with 31 elements')) "['t', 'h', 'i', 's', ' ', 'i',...]"

3 Output formatting, 2 pprint module – Provides “prettier” printing for both built-in and user objects – Gives the user more control over how objects are printed – Especially useful in debugging >>> import pprint >>> mylist = ['this elem is a string', ['this', 'is', 'a', 'list'], ['this', ['is', ['nested']]]] >>> pprint.pprint(mylist, indent=4, width=15) [ 'this elem is a string', [ 'this', 'is', 'a', 'list'], [ 'this', [ 'is', [ 'nested']]]]

4 Output formatting, 3 textwrap module – Wraps text to fit a given screen width – fill() returns a string with appropriate new line characters added – wrap() returns a list with each line instead of adding new line characters >>> import textwrap >>> print textwrap.fill("Add new line chars.", width=10) Add new line chars. >>> print textwrap.wrap("Separate into list.", width=10) ['Separate', 'into list.']

5 Output formatting, 4 locale module – Provides access to a database of culture specific data formats – Use typically begins by calling the setlocale method – Common functions are format (for numeric values) and format_string (for strings) >>> import locale >>> locale.setlocale(locale.LC_All, ‘English_United States.1252’) >>> x = 1234567.8 >>> locale.format("%d", x, grouping=True) '1,234,567'

6 Multi-threading, 1 Threading is a process by which you can decouple tasks that are not sequentially dependent That is, you can run two processes simultaneously – For example, you may run IO processes while doing background calculations Can be problematic when separate processes need to share resources

7 Multi-threading, 2 Example import threading import datetime class BackgroundClass(threading.Thread): def __init__(self, num_calcs): threading.Thread.__init__(self) self.num_calcs = num_calcs def run(self): for i in range(self.num_calcs): for j in range(self.num_calcs): i + j background = BackgroundClass(10000) background.start() print datetime.datetime.now().time(), print "Calculations are running in the background" background.join() # Wait for the background task to finish print datetime.datetime.now().time(), print "Calculations are finished and the main program is running normally" 01:59:39.406000 Calculations are running in the background 01:59:49.681000 Calculations are finished and the main program is running normally output

8 Logging, 1 logging module – provides a full featured logging system – If output is not directed anywhere, it goes to sys.stderr by default >>> import logging >>> logging.basicConfig(level=logging.INFO) >>> logging.debug("Below output level") >>> logging.info("Info message") INFO:root:Info message >>> logging.warning("Warning message") WARNING:root:Warning message >>> try:... raise Exception, "manually raising exception"... except Exception:... logging.exception("Exception message")... ERROR:root:Exception message Traceback (most recent call last): File " ", line 2, in Exception: manually raising exception

9 Logging, 2 Objects available from logging module >>> import logging >>> import sys >>> myformatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s") >>> myhandler = logging.StreamHandler(sys.stdout) >>> myhandler.setLevel(logging.DEBUG) >>> myhandler.setFormatter(myformatter) >>> mylogger = logging.Logger("logger name", logging.WARNING) >>> mylogger.addHandler(myhandler) >>> mylogger.info("Info message") >>> mylogger.warning("Warning message") logger name - WARNING - Warning message

10 Weak References, 1 Python does automatic memory management (garbage collection) Sometimes, you may want to reference an object only when it is still in memory (i.e., you do not want to create another permanent reference to the object) Python provides weak references to solve this – Allows you to track an object without creating a reference to it

11 Weak References, 2 Example >>> import weakref, gc >>> class MyClass(list):... pass... >>> mylist = MyClass([0, 1, 2]) >>> weak_dict = weakref.WeakValueDictionary() >>> weak_dict['weak key'] = mylist # creates a weak reference to mylist # so when mylist is removed from memory, # it will be removed from the dictionary >>> print weak_dict['weak key'] [0, 1, 2] >>> del mylist >>> gc.collect() # tell the garbage collector to clean up immediately >>> weak_dict['weak key'] # should raise a KeyException because it should have # been removed from the dictionary Traceback (most recent call last): File " ", line 245, in run_nodebug File " ", line 12, in File "C:\Python27\Lib\weakref.py", line 56, in __getitem__ o = self.data[key]() KeyError: 'weak key'

12 Tools for Working with Lists, 1 While lists are useful for many applications, sometimes alternative implementations are desired There are several objects available as alternatives to working directly with lists

13 Tools for Working with Lists, 2 array class – Similar to list, but for homogeneous data – Stores data more compactly >>> from array import array >>> a = array('H', [4000, 10, 700, 22222]) >>> sum(a) 26932 >>> a[1:3] array('H', [10, 700]) ‘H’ is the typecode for 2 byte unsigned integer data

14 Tools for Working with Lists, 3 deque class – Provides faster appends and pops from the left side – Lookups in the middle are slower – Ideal for implementing a queue >>> from collections import deque >>> d = deque([0, 1, 2]) >>> d.append(3) >>> print d deque([0, 1, 2, 3]) >>> print d.popleft() 0 >>> print d deque([1, 2, 3])

15 Tools for Working with Lists, 4 bisect module – provides methods for manipulating sorted lists >>> import bisect >>> mylist = [0, 4, 8] >>> bisect.insort(mylist, 3) >>> mylist [0, 3, 4, 8]

16 Tools for Working with Lists, 5 heapq module – Provides functions for using heaps – Recall that a heap has the property that if node A is the parent of node B, then the value of A is less than or equal to the value of B >>> import heapq >>> h = [8, 2, 3, 4] >>> heapq.heapify(h) >>> h [2, 4, 3, 8] >>> heapq.heappush(h, 5) >>> h [2, 4, 3, 8, 5] >>> heapq.heappop(h) 2 >>> h [3, 4, 5, 8]

17 Decimal, 1 decimal module – Provides the Decimal class for floating point arithmetic – Especially useful for: Controlling precision Control over rounding Tracking of significant digits Matching work done by hand

18 Decimal, 2 Examples >>> from decimal import Decimal >>> Decimal('1.00') % Decimal('.10') Decimal('0.00') >>> 1.00 % 0.10 0.09999999999999995 >>> from decimal import Decimal >>> Decimal('0.35').quantize(Decimal('0.1')) # round to one decimal place Decimal('0.4') >>> round(0.35, 1) # standard rounding 0.3

19 Resources All information found in the Python tutorial – http://docs.python.org/tutorial/stdlib2.html


Download ppt "Python’s Standard Library Part 2 Francis Ryan. Output formatting, 1 repr module – When using large or deeply nested containers, allows better printing."

Similar presentations


Ads by Google