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