Python

From Colettapedia
Jump to navigation Jump to search


General

All things Pythonic

  • Don't use istype() or hasattr() to check for types. "The pythonic way is to assume values passed to func is of whatever type is suitable. Only use try-except/hasattr if you know that text will be entirely different things at different times. In other words, don't validate."
  • Beg forgiveness instead of ask permission, (article)

Syntax

Tidbits

  • Walrus operator
  • functools
    • functools.@cache - cache function calls
    • functools.@lru_cache - least recently used cache "works best when the most recent calls are the best predictors of upcoming calls"
  • figure out include paths:
import sys
for thingy in sys.path:
  print( "{0}\n".format(thingy) )
  • add additional paths, useful for modules in weird places:
sys.path.append( '/Users/chris/the/full/path/' )
  • obj = thing.instance ... here obj is a type
  • obj = thing.instance() ... here, obj is an instance.
  • * operator = unpack a tuple, i.e., for the purpose of passing via function argument
  • ** operator = unpack a dictionary, i.e., for the purpose of passing via function argument
def function(**attrs):
  thing = attrs.get('some key')
  if thing:
    do sum'n
  else:
    do sum'n else

Data Structures

  • lists = vector
    • a_list = [1, 2, 3, "a dog"]
    • empty_list = []
  • tuple - immutable list
    • a_tuple = 1, 2, 3, "four"
    • readable_tuple = ( 1, 2, 3, "four" )
    • empty_tuple = ()
  • dictionary = hash = map
    • syntax = ({key: value})
    • a_dictionary = {"key 1":"value 1", 2:3, 4:[]}
  • set - mutable, unindexed, unordered, contains no duplicates, can do set
    • operations like union, intersection, etc.
    • some_set = {0, (), False}
    • empty_set = set() # not {}, this is a dictionary
    • immutable_set = frozenset...
  • strings - immutable
  • deque - efficient pushes & pops from both ends.
    • from collections import deque
    • q = deque( [ ... ] )
    • q.append()
    • q.popleft()

Lists

  • a c++ vector is a python list
  • DECLARED BY BRACKET ON OUTSIDE, COMMA DELIMITED WITHIN.
    • list.append(item)
    • list.extend( another_list )
    • list.insert( position_index, value )
    • list.remove( value ) - removes first instance of value from list
    • list.pop() - remove last item from list and return it
    • list.pop(i) - remove the ith item in the list and return it
    • list.index( value ) - index of first item whose value is value
    • list.count( value ) - ## times value appears in list
    • list.reverse()
  • list can be used as stack or queue
  • map( function, sequence ) - do function to each item in sequence and return list of function's retvals.
Looping over lists
  • for-in
for item in L:
  print item
  • enumerate
for index, item in L:
  print index, item
  • if need only index
for index in range(len(L)):
  print index
  • iterator
i = iter(L)
item = i.next() # fetch first value
item = i.next() # fetch second value
Modifying Lists
L = []
M = L

# modify both lists
L.append(obj)
L = []
M = L[:]

# modify L only
L.append(obj)
  • run function on every item in list:
out = map(function, L)
out = [function(object) for object in L]
# both do the same thing

Built-in Functions

Usefulness Immediately Apparent

  • abs(x)
  • all( iterable ) - Return True if all elements of the iterable are true (or if the iterable is empty).
  • any( iterable )
  • bool([x])
  • chr( integer from 0 to 255 ) - ASCII code character
  • cmp (x,y) returns -1, 0, 0r 1
  • complex([real[,imag]])
  • dir() - list names of variables in this scope
  • dir(object) - list names of members ("attributes") in object
  • [quotient, remainder] = divmod(a, b)
  • enumerate( iterable[, start=0]) - joins a number to all values in iterable
  • eval( code ) - execute python code
  • execfile( filename ) - execs code in filename
  • filter( function, sequence ) - return subset of sequence for which all values the function evaluates to true.
  • filter( None, sequence ) - all elements of iterable that are false are removed.
  • type( name (str), bases (tuple), dict (dict) ) - dynamically define classes.
  • range([start,] stop[, step]) - arguments must be plain integers
    • xrange(... - same as range, except values aren't all created at once to save memory starved machines
  • str( object ) - return a string printable representation of an object
  • repr( object ) - return a string printable representation of an object that can be aceptable to eval()
  • zip( iterable, iterable[, ...]) - zips together iterables, like coordinates
  • can unzip using the * operator:
    • x_coords, y_coords = zip(*coordinates)

Usefulness Not So Apparent

  • bin( x ) - Convert an integer number to a binary string.
  • callable(object)
  • bytearray()
  • classmethod()
  • compile()
  • delattr( object, member_name ) - pare off a member from a class
  • file() - is instance(f, file)

String Functions

Sequence Type Operations (also works for list, tuple, bytearray, buffer, xrange

  • x in s - true/false, substring test
  • x not in s - true/false, substring test
  • s + t - concatenation, could be costly, use str.join()
  • s * n, n * s - n shallow copies of s concatenated. Modify s and all n copies will be changed.
  • s[i] - ith item of s, origin 0. If i is negative, index is relative to end of string
  • s[i:j] - slice of s from i to j
  • s[i:j:k] - slice of s from i to j with step k
  • len(s)
  • min(s)
  • max(s)
  • s.index(i) - index of first occurrence of i in s
  • s.count(i) - total number of occurrences of i in s

String specific functions

Usefulness immediately apparent

  • str.capitalize()
  • str.center(width[, fillchar]) - padding is done with fillchar, default is space
  • str.count( substr[, start, end])
  • str.endswith( suffix[ start[, end]]) - suffix can also be a tuple of strings to look for
  • str.statrswith(...)
  • str.expandtabs( tabsize = 8)
  • str.find(substr[, start[, end]]) - use only if you need to know position of substr, otherwise use in operator. returns lowest index where substr is found.
    • str.rfind(...) start from the right
    • str.index() - like find, but raise ValueError exception if substr not found
    • str.rindex() - from the right
  • str.format( *args, **kwargs )
  • str.isalnum() - all chars alphanumeric, and at least 1 char
  • str.isdigit()
  • str.islower()
  • str.isupper()
  • str.isspace() - whitespace
  • str.join( iterable)
  • str.lower()
    • str.upper()
  • str.strip( [chars] )
    • str.lstrip( [chars = "whitespace"]) - strip chars from beginning of string
    • str.rstrip()
  • str.partition( sep ) - return 3-tuple of before, sep, after
    • str.rpartition(...)
  • str.replace(old, new[, count])
  • str.split([sep[, maxsplit]]) - if sep is None, separator is whitespace
  • str.splitlines( [keepends = false])
  • str.swapcase()
  • str.translate( table[, deletechars])
  • str.zfill(width) - zerofill

usefulness not immediately apparent

  • str.decode([encoding[, errors]])
  • str.encode([encoding[, errors]])
  • str.ljust() and str.rjust()

Exceptions

Built-in Exceptions

  • BaseException - don't inherit from this, inherit from Exception
    • SystemExit - can associate exit value, None associated with return 0
    • KeyboardInterrupt
    • GeneratorExit
    • Exception - user-derived exceptions should inherit from here
      • StopIteration
      • StandardError
        • BufferError - raised when buffer operations can't be performed
        • ArithmeticError
          • FloatingPointError
          • OverflowError
          • ZeroDivisionError
        • AssertionError - raised when assert statement fails
        • AtributeError
        • EnvironmentError - base class for exceptions that occur outside of python
          • IOError - file not found, disk full
          • OSError
            • WindownError
            • VMSError
        • EOFError
        • ImportError - when import statement fails
        • LookupError
          • IndexError - subscript out of range
          • KeyError
        • MemoryError
        • NameError
          • UnboundLocalError
        • ReferenceError
        • RuntimeError
          • NotImplementedError
        • SyntaxError
          • IndentationError
            • TabError
        • SystemError
        • TypeError
        • ValueError
          • UnicodeError
            • UnicodeDecodeErooro
            • UnicodeEncodeErro
            • UnicodeTranslateError
      • Warning
        • DeprecationWarning
        • PendingDeprecationWarning
        • RuntimeWarning
        • SyntaxWarning
        • UserWarning
        • FutureWarning
        • ImportWarning
        • UnicodeWarning
        • BytesWarning

pdb

  • breakpoint()
    • for Python 3.6 and earlier use: import pdb; pdb.set_trace() - put in a script to invoke debugger
  • if interactive, can use postmortem:
        >>> <a statement>
        <exception traceback>
        >>> import pdb
        >>> pdb.pm()
  • can put aliases in a .pdbrc file

Commands

  • EOF - handles the receipt of EOF as a command
  • alias [name [command [parameter parameter ...] ]]
    • Creates an alias called 'name' the executes 'command'. The command must *not* be enclosed in quotes. Replaceable parameters are indicated by %1, %2, and so on, while %* is replaced by all the parameters. If no command is given, the current alias for name is shown. If no name is given, all aliases are listed.
    • Aliases may be nested and can contain anything that can be legally typed at the pdb prompt. Note! You *can* override internal pdb commands with aliases! Those internal commands are then hidden until the alias is removed. Aliasing is recursively applied to the first word of the command line; all other words in the line are left alone.
    • Some useful aliases (especially when placed in the .pdbrc file) are:
#Print instance variables (usage "pi classInst")
alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]

#Print instance variables in self
alias ps pi self
  • a(rgs) - Print the arguments of the current function.
  • b(reak) ([file:]lineno | function) [, condition]
    • With a line number argument, set a break there in the current file. With a function name, set a break at first executable line of that function. Without argument, list all breaks. If a second argument is present, it is a string specifying an expression which must evaluate to true before the breakpoint is honored.
    • The line number may be prefixed with a filename and a colon, to specify a breakpoint in another file (probably one that hasn't been loaded yet). The file is searched for on sys.path; the .py suffix may be omitted.
  • bt
  • w(here)
    • Print a stack trace, with the most recent frame at the bottom. An arrow indicates the "current frame", which determines the context of most commands.
  • c(ont(inue)) Continue execution, only stop when a breakpoint is encountered.
  • cl(ear) filename:lineno
  • cl(ear) [bpnumber [bpnumber...]]
    • With a space separated list of breakpoint numbers, clear those breakpoints. Without argument, clear all breaks (but first ask confirmation). With a filename:lineno argument, clear all breaks at that line in that file.
    • Note that the argument is different from previous versions of the debugger (in python distributions 1.5.1 and before) where a linenumber was used instead of either filename:lineno or breakpoint numbers.
  • commands [bpnumber]
    • Specify a list of commands for breakpoint number bpnumber. The commands themselves appear on the following lines. Type a line containing just 'end' to terminate the commands.
    • To remove all commands from a breakpoint, type commands and follow it immediately with end; that is, give no commands.
    • With no bpnumber argument, commands refers to the last breakpoint set.
    • You can use breakpoint commands to start your program up again. Simply use the continue command, or step, or any other command that resumes execution.
    • Specifying any command resuming execution (currently continue, step, next, return, jump, quit and their abbreviations) terminates the command list (as if that command was immediately followed by end). This is because any time you resume execution (even with a simple next or step), you may encounter another breakpoint--which could have its own command list, leading to ambiguities about which list to execute.
    • If you use the 'silent' command in the command list, the usual message about stopping at a breakpoint is not printed. This may be desirable for breakpoints that are to print a specific message and then continue. If none of the other commands print anything, you see no sign that the breakpoint was reached.
  • condition bpnumber str_condition
    • str_condition is a string specifying an expression which must evaluate to true before the breakpoint is honored. If str_condition is absent, any existing condition is removed; i.e., the breakpoint is made unconditional.
  • d(own)
    • Move the current frame one level down in the stack trace (to a newer frame).
  • debug code
    • Enter a recursive debugger that steps through the code argument (which is an arbitrary expression or statement to be executed in the current environment).
  • disable bpnumber [bpnumber ...]
    • Disables the breakpoints given as a space separated list of bp numbers.
  • enable bpnumber [bpnumber ...]
    • Enables the breakpoints given as a space separated list of bp numbers.
  • q(uit) or exit
    • Quit from the debugger. The program being executed is aborted.
  • h(elp)
  • ignore bpnumber count
    • Sets the ignore count for the given breakpoint number. A breakpoint becomes active when the ignore count is zero. When non-zero, the count is decremented each time the breakpoint is reached and the breakpoint is not disabled and any associated condition evaluates to true.
  • j(ump) lineno</lineno>
    • Set the next line that will be executed.
  • l(ist) [first [,last]]
    • List source code for the current file. Without arguments, list 11 lines around the current line or continue the previous listing. With one argument, list 11 lines starting at that line. With two arguments, list the given range; if the second argument is less than the first, it is a count.
  • n(ext)
    • Continue execution until the next line in the current function is reached or it returns.
  • p expression
    • Print the value of the expression.
  • pp expression
    • Pretty-print the value of the expression.
  • r(eturn)
    • Continue execution until the current function returns.
  • run [args...] or restart [args...]
    • Restart the debugged python program. If a string is supplied, it is splitted with "shlex" and the result is used as the new sys.argv. History, breakpoints, actions and debugger options are preserved.
  • s(tep)
    • Execute the current line, stop at the first possible occasion (either in a function that is called or in the current function).
  • tbreak [args]
    • Same arguments as break, but breakpoint is removed when first hit.
  • u(p)
    • Move the current frame one level up in the stack trace (to an older frame).
  • unalias name
    • Deletes the specified alias.
  • unt(il)
    • Continue execution until the line with a number greater than the current one is reached or until the current frame returns
  • whatis arg
    • Prints the type of the argument.
  • (!) statement
    • Execute the (one-line) statement in the context of the current stack frame.

The exclamation point can be omitted unless the first word of the statement resembles a debugger command. To assign to a global variable you must always prefix the command with a 'global' command, e.g.:

(Pdb) global list_options; list_options = ['-l']

Magic Methods

Individual Modules