Standard Library#

Operating System Interface#

import os
os.getcwd()      # Return the current working directory
'/home/runner/work/python-notebooks/python-notebooks/notebooks'
import sys
if sys.platform == "darwin":
    os.environ['CC']='gcc-10' # Change the default C compiler to gcc on macos
    
os.system('mkdir today') # Run the command mkdir in the system shell
0
os.chdir('today')   # Change current working directory
os.system('touch data.db') # Create the empty file data.db
0
import shutil
shutil.copyfile('data.db', 'archive.db')
if os.path.exists('backup.db'):  # If file backup.db exists
    os.remove('backup.db')       # Remove it
shutil.move('archive.db', 'backup.db',)
shutil.os.chdir('..')

File Wildcards#

The glob module provides a function for making file lists from directory wildcard searches:

import glob
glob.glob('*.py')
['pool.py', 'process_pool.py', 'hello.py']
def recursive_replace( root, pattern, replace ) :
    """
    Function to replace a string inside a directory
    root : directory
    pattern : searched string
    replace "pattern" by "replace"
    """
    for directory, subdirs, filenames in os.walk( root ):
      for filename in filenames:
        path = os.path.join( directory, filename )
        text = open( path ).read()
        if pattern in text:
          print('occurence in :' + filename)
          open(path,'w').write( text.replace( pattern, replace ) )

Command Line Arguments#

These arguments are stored in the sys module’s argv attribute as a list.

%%file demo.py
import sys
print(sys.argv)
Writing demo.py
%run demo.py one two three
['demo.py', 'one', 'two', 'three']

Random#

import random
random.choice(['apple', 'pear', 'banana'])
'pear'
random.sample(range(100), 10)   # sampling without replacement
[0, 94, 85, 92, 45, 33, 59, 72, 65, 51]
random.random()    # random float
0.1853322794116069
random.randrange(6)    # random integer chosen from range(6)
5

Statistics#

import statistics
data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
statistics.mean(data)
1.6071428571428572
statistics.median(data)
1.25
statistics.variance(data)
1.3720238095238095

Performance Measurement#

from timeit import Timer
Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
0.020627858999887394
Timer('a,b = b,a', 'a=1; b=2').timeit()
0.02238598999997521
%%timeit a=1; b=2
a,b = b,a
20.8 ns ± 0.00904 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

The profile and pstats modules provide tools for identifying time critical sections in larger blocks of code.

Quality Control#

One approach for developing high quality software is to write tests for each function.

  • The doctest module provides a tool for scanning a module and validating tests embedded in a program’s docstrings.

  • This improves the documentation by providing the user with an example and it allows the doctest module to make sure the code remains true to the documentation:

def average(values):
    """Computes the arithmetic mean of a list of numbers.

    >>> print(average([20, 30, 70]))
    40.0
    """
    return sum(values) / len(values)

import doctest
doctest.testmod()   # automatically validate the embedded tests
TestResults(failed=0, attempted=1)

Python’s standard library is very extensive#

  • Containers and iterators: collections, itertools

  • Internet access: urllib, email, mailbox, cgi, ftplib

  • Dates and Times: datetime, calendar,

  • Data Compression: zlib, gzip, bz2, lzma, zipfile, tarfile

  • File formats: csv, configparser, netrc, xdrlib, plistlib

  • Cryptographic Services: hashlib, hmac, secrets

  • Structure Markup Processing Tools: html, xml

Check the The Python Standard Library