Scripting >> Python >> Cheatsheet >>  Python 3.7 modules reference

Modules reference : help('modules') /  https://docs.python.org/3/py-modindex.html

 

Important !!! - Do not name your python script the same as the module being imported!!! e.g. if you want to import "calendar" module, do not name you script as calendar.py ... it will import your script instead. 


calendar
References:help('calendar') / https://docs.python.org/3/library/calendar.html

 

calendar.TextCalendar().formatyear(yyyy)

 

calendar.TextCalendar().formatmonth(yyyy,m)

 

calendar.TextCalendar().formatweekday(dayofweek)

 

 
   

  

collections
References: help('collections') / https://docs.python.org/3/library/collections.html

Examples

This module implements specialized container datatypes providing alternatives to Python's general purpose built-in containers, dict,
list, set, and tuple.

namedtuple  
  • factory function for creating tuple subclasses with named fields

deque       

  • list-like container with fast appends and pops on either end
  • .append(..)
  • .appendleft(..)
  • .clear(..)
  • .copy(..)
  • .count(..)
  • .extend(..)
  • .extendleft(..)
  • .index(..)
  • .insert(..)
  • .pop(..)
  • .popleft(..)
  • .remove(..)
  • reverse(..)
  • .rotate(..)


ChainMap    

  • dict-like class for creating a single view of multiple mappings


Counter     

  • dict subclass for counting hashable objects


OrderedDict 

  • dict subclass that remembers the order entries were added


defaultdict 

  • dict subclass that calls a factory function to supply missing values


UserDict    

  • wrapper around dictionary objects for easier dict subclassing


UserList    

  • wrapper around list objects for easier list subclassing


UserString  

  • wrapper around string objects for easier string subclassing

namedtuple example 1

>>> from collections import namedtuple
>>> Point = namedtuple('Point','x,y')
>>> pt1 = Point(1,2)
>>> pt2 = Point(3,4)
>>> dot_product = ( pt1.x * pt2.x ) +( pt1.y * pt2.y )
>>> print dot_product
11

namedtuple example 2
>>> from collections import namedtuple
>>> Car = namedtuple('Car','Price Mileage Colour Class')
>>> xyz = Car(Price = 100000, Mileage = 30, Colour = 'Cyan', Class = 'Y')
>>> print xyz
Car(Price=100000, Mileage=30, Colour='Cyan', Class='Y')
>>> print xyz.Class
Y

namedtuple example 3
#input line 1 N = no. of students
#input line 2 = column headings that include MARKS
#next N lines, columns of data for each headingfrom collections import namedtuple
N = int(input())
Student = namedtuple('Student',input().split())
print(sum([ int(Student._make(input().split()).MARKS)  for i in range(N) ]) / N)

Counter example

>>> from collections import Counter
>>> shoeList = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,1,2,3]
>>> shoeStock = Counter(shoeList)
>>> print(shoeStock)
Counter({1: 4, 2: 4, 3: 4, 4: 3, 5: 2})

 

defaultdict example

>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> d['python'].append("awesome")
>>> d['something-else'].append("not relevant")
>>> d['python'].append("language")
>>> for i in d.items():
        print i
('python', ['awesome', 'language'])
('something-else', ['not relevant'])

explanation:
when d['python'] was first used, there was no item  in the dictionary with key='python', but because default factory was set as list, it will call list() to assign  empty list to d['python'] and then allow it to call the append method (which is available to list data type] to
add items to that list value.
 

OrderedDict example

from collections import OrderedDict
N = int(input())
inventory = OrderedDict()
for i in range(N) :
   itemlist = input().split()
   item_name = ' '.join(itemlist[:-1])
   price = int(itemlist[-1])
   try:
       inventory[item_name] += price
   except:
       inventory[item_name] = price

for item,price in inventory.items():
    print(item,price)

deque example

 

 

datetime
References: help('datetime') or https://docs.python.org/3/library/datetime.html

 
Classes  

class datetime.date

class datetime.time

class datetime.datetime

class datetime.timedelta

class datetime.tzinfo

class datetime.timezone

 
Class date  
class date
  methods:
    date(year,month,day)
      create an object of type date with the specified day,month,year

    today()
      creates an object of type date with day,month,year taken from current date.

    fromtimestamp(timestamp)
      creates an object of type date from the timestamp value

  attributes:
    year
      returns year from the date object

    month
      returns the month from the date object
   
    day
      returns the day from the date object


  instance methods:
    replace(year,month,day)
      update any or all of the 3 attributes e.g. d=d.replace(month=3,day=1)

    timetuple()
      returns a tuple of the attributes for the current local time
         
 
Class datetime Exampes

methods
    datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])

 

eg. 1

>>> import datetime
>>> dt=datetime.datetime(2018,10,30)
>>> print(dt)
2018-10-30 00:00:00

eg. 2

>>> from datetime import datetime
>>> dt=datetime(2018,10,30)
>>> print(dt)
2018-10-30 00:00:00

 

html.parser
References:help('html.parser') / https://docs.python.org/3/library/html.parser.html

 
Methods Examples

HTMLParser.feed(data)

HTMLParser.close()

HTMLParser.reset()

HTMLParser.getpos()

HTMLParser.get_starttag_text()

HTMLParser.handle_starttag(tag, attrs)

HTMLParser.handle_endtag(tag)

HTMLParser.handle_startendtag(tag, attrs)

HTMLParser.handle_data(data)

HTMLParser.handle_entityref(name)

HTMLParser.handle_charref(name)

HTMLParser.handle_comment(data)

HTMLParser.handle_decl(decl)

HTMLParser.handle_pi(data)

HTMLParser.unknown_decl(data)


 

from html.parser import HTMLParser
from html.entities import name2codepoint

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print("Start tag:", tag)
        for attr in attrs:
            print("     attr:", attr)

    def handle_endtag(self, tag):
        print("End tag  :", tag)

    def handle_data(self, data):
        print("Data     :", data)

    def handle_comment(self, data):
        print("Comment  :", data)

    def handle_entityref(self, name):
        c = chr(name2codepoint[name])
        print("Named ent:", c)

    def handle_charref(self, name):
        if name.startswith('x'):
            c = chr(int(name[1:], 16))
        else:
            c = chr(int(name))
        print("Num ent  :", c)

    def handle_decl(self, data):
        print("Decl     :", data)

parser = MyHTMLParser()

 

 

   

 

glob
References: help('glob') / https://docs.python.org/3/library/glob.html
 
Functions  
glob.glob (pathname, *, recursive=False)

Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools/*/*.gif), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell).

If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is followed by an os.sep, only directories and subdirectories match.

Note

Using the “**” pattern in large directory trees may consume an inordinate amount of time.

Changed in version 3.5: Support for recursive globs using “**”.

glob.iglob (pathname, *, recursive=False)

Return an iterator which yields the same values as glob() without actually storing them all simultaneously.

glob.escape (pathname)

Escape all special characters ('?', '*' and '['). This is useful if you want to match an arbitrary literal string that may have special characters in it. Special characters in drive/UNC sharepoints are not escaped, e.g. on Windows escape('//?/c:/Quo vadis?.txt') returns '//?/c:/Quo vadis[?].txt'.

 

 

itertools
References: help('itertools') / https://docs.python.org/3/library/itertools.html

 
Functions Examples

count(start [, step])

Return a count object whose .__next__() method returns consecutive values.  Equivalent to:

cycle(p)

Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely.

repeat(elem [,n])

.

accumulate(p[,func])

.

chain(p,q, ....)

.

compress(data, selectors)

.

dropwhile(pred,seq)

.

filterfalse(pred,seq)

.

groupby(iterable[,key])

.

islice(seq, [start,] stop [,step])

.

starmap(func,seq)

.

takewhile(pred,seq)

.

tee(iterator,n)

.

zip_longest(p, q, ..)

.

product(p,q ....[,repeat=1])

.

permutations(p[, r])

.

combinations(p, r)

.

combinations_with_replacement(p,r)

To use the function "product()" directly do
>>> from itertools import product
>>> product(A,B)

To use it as itertools.product() do
>>> import itertools
>>> itertools.product(A,B)

cycle() example
>>> import itertools
>>> for i in itertools.cycle('ABC'):
>>>     print(i)
A
B
C
A
....

 

math
Reference: help('math') / https://docs.python.org/3/library/math.html#module-math

 
Methods Examples

    acos(x, /)
        Return the arc cosine (measured in radians) of x.

    acosh(x, /)
        Return the inverse hyperbolic cosine of x.

    asin(x, /)
        Return the arc sine (measured in radians) of x.

    asinh(x, /)
        Return the inverse hyperbolic sine of x.

    atan(x, /)
        Return the arc tangent (measured in radians) of x.

    atan2(y, x, /)
        Return the arc tangent (measured in radians) of y/x.

        Unlike atan(y/x), the signs of both x and y are considered.

    atanh(x, /)
        Return the inverse hyperbolic tangent of x.

    ceil(x, /)
        Return the ceiling of x as an Integral.

        This is the smallest integer >= x.

    copysign(x, y, /)
        Return a float with the magnitude (absolute value) of x but the sign of y.

        On platforms that support signed zeros, copysign(1.0, -0.0)
        returns -1.0.

    cos(x, /)
        Return the cosine of x (measured in radians).

    cosh(x, /)
        Return the hyperbolic cosine of x.

    degrees(x, /)
        Convert angle x from radians to degrees.

    erf(x, /)
        Error function at x.

    erfc(x, /)
        Complementary error function at x.

    exp(x, /)
        Return e raised to the power of x.

    expm1(x, /)
        Return exp(x)-1.

    fabs(x, /)
        Return the absolute value of the float x.

    factorial(x, /)
        Find x!.

        Raise a ValueError if x is negative or non-integral.

    floor(x, /)
        Return the floor of x as an Integral.

        This is the largest integer <= x.

    fmod(x, y, /)
        Return fmod(x, y), according to platform C.

        x % y may differ.

    frexp(x, /)
        Return the mantissa and exponent of x, as pair (m, e).

        m is a float and e is an int, such that x = m * 2.**e.
        If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.

    fsum(seq, /)
        Return an accurate floating point sum of values in the iterable seq.

        Assumes IEEE-754 floating point arithmetic.

    gamma(x, /)
        Gamma function at x.

    gcd(x, y, /)
        greatest common divisor of x and y

    hypot(x, y, /)
        Return the Euclidean distance, sqrt(x*x + y*y).

    isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
        Determine whether two floating point numbers are close in value.

          rel_tol
            maximum difference for being considered "close", relative to the
            magnitude of the input values
          abs_tol
            maximum difference for being considered "close", regardless of the
            magnitude of the input values

        Return True if a is close in value to b, and False otherwise.

        For the values to be considered close, the difference between them
        must be smaller than at least one of the tolerances.

        -inf, inf and NaN behave similarly to the IEEE 754 Standard.  That
        is, NaN is not close to anything, even itself.  inf and -inf are
        only close to themselves.

    isfinite(x, /)
        Return True if x is neither an infinity nor a NaN, and False otherwise.

    isinf(x, /)
        Return True if x is a positive or negative infinity, and False otherwise.

    isnan(x, /)
        Return True if x is a NaN (not a number), and False otherwise.

    ldexp(x, i, /)
        Return x * (2**i).

        This is essentially the inverse of frexp().

    lgamma(x, /)
        Natural logarithm of absolute value of Gamma function at x.

    log(...)
        log(x, [base=math.e])
        Return the logarithm of x to the given base.

        If the base not specified, returns the natural logarithm (base e) of x.

    log10(x, /)
        Return the base 10 logarithm of x.

    log1p(x, /)
        Return the natural logarithm of 1+x (base e).

        The result is computed in a way which is accurate for x near zero.

    log2(x, /)
        Return the base 2 logarithm of x.

    modf(x, /)
        Return the fractional and integer parts of x.

        Both results carry the sign of x and are floats.

    pow(x, y, /)
        Return x**y (x to the power of y).

    radians(x, /)
        Convert angle x from degrees to radians.

    remainder(x, y, /)
        Difference between x and the closest integer multiple of y.

        Return x - n*y where n*y is the closest integer multiple of y.
        In the case where x is exactly halfway between two multiples of
        y, the nearest even value of n is used. The result is always exact.

    sin(x, /)
        Return the sine of x (measured in radians).

    sinh(x, /)
        Return the hyperbolic sine of x.

    sqrt(x, /)
        Return the square root of x.

    tan(x, /)
        Return the tangent of x (measured in radians).

    tanh(x, /)
        Return the hyperbolic tangent of x.

    trunc(x, /)
        Truncates the Real x to the nearest Integral toward 0.


 

 

operator
References:help('operator') / https://docs.python.org/3/library/operator.html

 
Methods Examples
operator.lt(a, b)
operator.le(a, b)
operator.eq(a, b)
operator.ne(a, b)
operator.ge(a, b)
operator.gt(a, b)
operator.not_(obj)


 

   

 

os
References:help('os') / https://docs.python.org/3/library/os.html

 
Methods Examples

os.rename(current_filename,new_filename)

os.remove(filename)

os.mkdir(dirname)
    create directory

os.chdir(dirpath)
    change directory

os.getcwd()
    get current directory

os.rmdir(dirname)
    remove the directory

os.path.exists(path)
    determine if the dir or file specified in the path exists

os.system(command)
    run Operating System command

os.mkdir(path)
    creates new dir

 

 Eg. 1 list files and then rename to specific format

find files that match "p (n).jpg" and rename to "p00n.jpg"

import os
import re
for f in os.listdir():
   m = re.match(r'(p) [(]([0-9]+)[)](.jpg)',f)
   if m is not None:
      nf = "{}{:03d}{}".format(m.group(1),int(m.group(2)),m.group(3))
      os.rename(f,nf)
 

   

 

re
References: help('re') or https://docs.python.org/3/library/re.html
Examples
   

CLASSES

re.A

re.ASCII

re.DEBUG

re.I

re.IGNORECASE

re.L

re.LOCALE

re.M

re.MULTILINE

re.S

re.DOTALL

re.X

re.VERBOSE

re.search = search(pattern, string, flags=0)
    Scan through string looking for a match to the pattern, returning
    a Match object, or None if no match was found

re.match = match(pattern, string, flags=0)
    Try to apply the pattern at the start of the string, returning
    a Match object, or None if no match was found.

re.fullmatch = fullmatch(pattern, string, flags=0)
    Try to apply the pattern to all of the string, returning
    a Match object, or None if no match was found.

re.split = split(pattern, string, maxsplit=0, flags=0)
    Split the source string by the occurrences of the pattern,
    returning a list containing the resulting substrings.  If
    capturing parentheses are used in pattern, then the text of all
    groups in the pattern are also returned as part of the resulting
    list.  If maxsplit is nonzero, at most maxsplit splits occur,
    and the remainder of the string is returned as the final element
    of the list.

re.findall = findall(pattern, string, flags=0)
    Return a list of all non-overlapping matches in the string.

    If one or more capturing groups are present in the pattern, return
    a list of groups; this will be a list of tuples if the pattern
    has more than one group.

    Empty matches are included in the result

re.finditer = finditer(pattern, string, flags=0)
    Return an iterator over all non-overlapping matches in the
    string.  For each match, the iterator returns a Match object.

    Empty matches are included in the result.

re.sub = sub(pattern, repl, string, count=0, flags=0)
    Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl.  repl can be either a string or a callable;
    if a string, backslash escapes in it are processed.  If it is
    a callable, it's passed the Match object and must return
    a replacement string to be used.

re.subn = subn(pattern, repl, string, count=0, flags=0)
    Return a 2-tuple containing (new_string, number).
    new_string is the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in the source
    string by the replacement repl.  number is the number of
    substitutions that were made. repl can be either a string or a
    callable; if a string, backslash escapes in it are processed.
    If it is a callable, it's passed the Match object and must
    return a replacement string to be used.

re.escape = escape(pattern)
    Escape special characters in a string.

re.purge = purge()
    Clear the regular expression caches

exception re.error(msg, pattern=None, pos=None)

    Exception raised when a string passed to one of the functions here is not a valid regular expression (for example, it might contain unmatched parentheses) or when some other error occurs during compilation or matching.

 


random
References:help('random') / https://docs.python.org/3/library/random.html

 
Methods Exampes

random.randint(fromInteger,toInteger)
    Generate random integer N where fromInteger <= N <= toInteger

 
random.choice(seq)
    Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError
 

 
 


zipfile
References:help('random') / https://docs.python.org/3/library/zipfile.html

 
Methods Exampes
ZipFile.write() from zipfile import ZipFile
zip=ZipFile("/var/tmp/myzipfile.zip",'w')
zip.write("/var/tmp/file1.txt")
zip.write("/var/tmp/file2.log")
zip.close()
 
ZipFile.close() from zipfile import ZipFile
zip=ZipFile("/var/tmp/myzipfile.zip",'w')
zip.write("/var/tmp/file1.txt")
zip.write("/var/tmp/file2.log")
zip.close()