datetime
(Data Types) basic date and time types
_____________________
Date & time obj can be aware or naive depending on timezone (tzinfo):
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 ordercopy
(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:
Others:
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
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
DictReader and DictWriter classes allow read/write in dict form
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:
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:
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
MagicMock Class
@patch(target)
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.)
encode(obj, encoding=’utf-8’, errors=’strict’)
decode(obj, encoding=’utf-8’, errors=’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))
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