Standard Library Flashcards
datetime
(Data Types) basic date and time types
_____________________
Date & time obj can be aware or naive depending on timezone (tzinfo):
- Aware obj have tz and dst info, not open to interp
- Naive obj less specific, open to interp
Date - year, month, day
Time - hour, minute second, microsecond, tzinfo
DateTime - combo of both
collections
(Data Types) container datatypes
_____________________
Implements specialized containers as alternatives to Python’s built-in dict, list, set, & tuple
named tuple() - factory function that creates tuple subclasses with named fields Point = named tuple('Point', ['x','y'] p = Point(11, y = 22) p => ('x'=11, 'y'=22) deque - list-like for fast appends/pops on either end, aka double sided queue ChainMap - dict-like class for single view of multiple mappings (faster than new dict or using update()) Counter(iterable/mapping) - dict subclass for counting hashables Ordered Dict - dict subclass that remembers entry order
copy
(Data Types) shallow and deep copy operations
_____________________
Shallow vs deep - only matters for compound objects
copy() - shallow, creates a new compound obj w/ references to the obj found in the original (i.e. the state may change if referenced obj has changed)
deep copy() - deep, constructs a new compound obj with copies of the objects found in the original - it avoids issues with recursive objects and copying too much (with a memo dict and user-defined overrides)
pprint
(Data Types) data pretty printer
_____________________
One class - PrettyPrinter
Lets you define indent, width, depth, etc.
call pprint(object) to print the object’s representation prettily
math
(Numeric/Math) mathematical functions
_____________________
on non-complex numbers
ceil(x)
floor(x)
Power, log, and trig functions:
sqrt(x)
log(x, base)
cos/sin/tan(x)
random
(Numeric/Math) generate pseudo-random numbers
_____________________
Integers:
- randrange(start, stop, step_) - stop exclusive
- randint(a, b) - same as randrange(a, b+1) - aka b inclusive
Others:
- random() - returns a random float 0.0-1.0
- shuffle(x, random()) - returns a list shuffled in place
itertools
(Func. Programming) functions creating iterators for efficient looping
_____________________
Infinite iterators
- count(start, step)
count(10) => 10 11 12 13 14…
count(10, 2) => 10 12 14 16…
Iterators terminate on shortest input sequence
- accumulate(p) - returns running sum
accumulate([1,2,3]) => 1 3 6
- chain(p,q,..) - makes an iterator out of all the elements passed in sequentially
chain(‘ABC’,’DEF’) => A B C D E F
Combinatoric iterators
- product(p,q, [repeat=1]) - Cartesian product, equivalent to a nested for loop
- permutations(p, r)
- r-length tuples of all possible ordering of p, no repeats
- essentially non-repeating product where p = q and repeat = 2
pickle
(Data Persistence) Python object serialization
_____________________
Serialization - translate data/state into a format that can be stored and reconstructed later
Binary serialization format - not human readable
dumps() - serializes an object hierarchy
loads() - deserializes an object hierarchy
JSON - text, human readable, used outside, no code vulnerability
pickle - binary, not human readable, Python specific, creates an arbitrary code vulnerability when deserializing that could execute unwanted functions
csv
(File Formats) CSV File Reading and Writing
_____________________
Allows reading/writing tabular data in CSV (comma separated value) format - with a specific dialect w/o knowing how the dialect works (aka Excel default)
Reader/Writer objects
- reader(csvfile, dialect=’excel’, **fmtparams)
- writer is same
- csvfile - any obj which supports iterator protocol and returns a str (lists and files)
- dialect defaults to excel
DictReader and DictWriter classes allow read/write in dict form
- DictReader(file, fieldnames=None,…)
- if filenames is not specified, first row of file becomes fieldnames/keys
- DictWriter(file, fieldnames, …)
- Must pass a fieldnames sequence, no default
- Use writerow() fxn to add records
os
(Generic OS Services) misc os interfaces
_____________________
Not for reading/writing files or path manipulation (os.path)
Environ - a mapping obj representing the environment and its vars
environ[‘HOME’] - will be the pathname of your home directory
getenv()/setenv() - getter and setter for env vars
Typical command line - chdir(), getcwd()
time
(Generic OS Services) time access and conversions
_____________________
Extended functionality related to date time module
gmtime() - converts time in seconds to UTC
localtime() - converts time in seconds to local time
Convert date to string and vice versa
perfect_timer - used in timeit
logging
(Generic OS Services) logging facility for Python
_____________________
As opposed to simple console output (print fxn), report events that occur during operation of a program
The logging fxns are named after the level of severity of the event: DEBUG INFO WARNING - default severity ERROR CRITICAL
Writing to a file is easy: basicConfig(filename='mylogfile.log', level=logging.INFO) # this will record all INFO and higher severity events
threading
(Concurrent Execution) thread-based parallelism
_____________________
Thread objects => kw argument target should be set to the callable to be invoked by run()
start() - start the thread’s activity, aka arranges a new thread to run the run() call
run() - the thread’s activity, invokes the target callable
join() - wait until the thread terminates, essentially blocks other threads until current thread is complete
multiprocessing
(Concurrent Execution) process-based parallelism
_____________________
Process objects have methods equivalent to Thread objects.
(Internet Data Handling) an email handling package
_____________________
A library for managing email messages, not sending them
Four main components:
- email object model - represents email as a tree
- parser - converts serialized emailsto email obj
- generator - converts email obj into serialized byte stream
- policy - object that controls email behavior
json
(Internet Data Handling) JSON encoder and decoder
_____________________
An API similar to pickle for json
unittest
(Dev Tools) unit testing framework
_____________________
Basic unit testing functionality for Python:
Four main components:
- Test fixture - represents the prep/cleanup for tests
- setUp()
- tearDown() - Test case - an individual test
- inherit from TestCase
- use specialized assert methods
- prefix with test_ - Test suite - a collection of test cases
- can be run from a TestSuite subclass - Test runner - orchestrates execution of test
- relies on test_ prefix and special asserts
unittest.mock
(Dev Tools) mock object library
_____________________
Allows replacing parts of your system under test with mock objects and assertions of how they’ve been called
Mock Class
- a flexible mock object meant to replace stubs/test doubles (stubs are obj that just return state info)
- record how you use them, so assertions can be made
MagicMock Class
- a Mock subclass with default implementation of magic methods
- these methods are configurable in Mock superclass, if you prefer
@patch(target)
- A decorator that replaces part of a module with a Mock obj
- pass the target object as the first parameter
pdb
(Debugging and Profiling) the Python Debugger
_____________________
Allows setting breakpoint() fxns (3x) throughout code to examine the state at the breakpoint of a running program
l(ist) - lists out the source code around the breakpoint
n(ext) - goes to the next line of code, skipping nested scopes
s(tep) - goes to the next line of code, stepping into nested scopes
c(continue) - goes to the next breakpoint
sys
(Python Runtime Services) system-specific params and functions
_____________________
sys. path - module search path as a list of strings
- current directory
- PYTHONPATH env var
- Standard Library modules
- .pth files (list out paths)
- site-packages home for third party extensions
sys. argv - list of command line args passed to when a Python script is run
sys. exit() - exit from Python
sys. exc_info() - returns a tuple of info regarding an exception being handles
- (type, value, traceback) =>
- type: class of exception
- value: instance of exception
- traceback: a traceback object that encapsulates the call stack at the original point of execution
builtins
(Python Runtime Services) built in objects can be accessed directly here
__future__
(Python Runtime Services) future statement definitions can be accessed for 3x from 2x using this module
codecs
(Binary Data Services) Codec registry and base classes
_________________________________
(A codec is a device or computer program which encodes or decodes a data stream or signal. Codec is a portmanteau of coder/decoder. They can be lossless or lossy.)
- Defines base classes for standard Python codecs (encoders and decoders) and access to the internal Python codec registry, which manages the codec and error handling lookup process
- Most standard codecs are text encodings - which encode text to bytes (there is also text to text, bytes to bytes)
encode(obj, encoding=’utf-8’, errors=’strict’)
- Encodes obj using the codec registered for encoding, default utf-8
- Errors may be used to set the desired error handling scheme, defaults to strict meaning errors raise ValueError
decode(obj, encoding=’utf-8’, errors=’strict’)
- Decodes obj using the codec registered for encoding, defaults utf-8
- Errors also default to strict
bisect
(Data Types) Array bisection algorithm
_________________________________
This module provides support for maintaining a list in sorted order without having to sort after each insertion. Called bisect because it uses a basic bisection algorithm to do this.
bisect_left(a, x, lo=0, hi=len(a))
- Locate & return insertion point for x in a to maintain sorted order
- Lo and hi can be specify subset to consider, defaults to entire list
- If x in a, inserts to left of existing entry
bisect_right(a, x, lo=0, hi=len(a))
bisect(a, x, lo=0, hi=len(a))
- Same, but to the right (after) any existing entry
insort_left(a, x, lo=0, hi=len(a))
- Insert at point returned by bisect_left
insort_right(a, x, lo=0, hi=len(a))
insort(a, x, lo=0, hi=len(a))
- Same, but at point returned by bisect/bisect_right