|
|
|
Python miscellany
Dictionary Operations
len (d)
d[k] exception KeyError
d.get(k, defaultValue) no exception. defaultValue if k not in d
d[k] = x
del d[k] exception KeyError
d.haskey(k)
k in d
d.items() list of key value pairs
d.keys()
d.values()
Sequence operations of interest
s.sort([ cmpfunc])
cmpfunc is an optional two-argument function returning -1, 0, 1
as the items compare less, equal, or greater, respectively.
Scoping
Python uses static scoping, but it has no declarations. This raises an interesting question:
what makes a variable local to a block. Answer: any assignment to a variable
in a function creates a local
binding in that function (at the time the function is called). Thus if a variable is
assigned-to in a function and is read before the assignment it will be reported as
an error at runtime. Read references use the usual static scope rules.
Exception to the above. The global statement
global var, var, var
forces both read references and assignments to the listed variables to be to outer-most
scope (module scope).
Modules
Each python file is a module; modules are part of packages
(ignore for now). To use values from another module, first use the import
statement
import modulename
The modulename is just the filename containing the code without the .py suffix.
Refer to names within a module by modulename.valuename. If you want
to avoid qualifying each reference with the modulename you can use
from mod import name, name, name
or even
from mod import *
which make the imported names behave as if they were local to the scope where
they were imported. This can be a source of confusion. Especially
from mod import *.
Where are modules found? (Recall a module is just a python source code file.)
By searching, directories on sys.path (you can change sys.path within a program)
Output
print expr, expr, expr
prints the values of the provided expressions separated by spaces and followed by a newline.
If you don't want the trailing newline end the print statement with a comma, i.e.
print expr,
File operations
To open a file use the open function:
f = open (filename [, mode = 'r’])
Files can be opened in differen modes depending on what you want to do. Valid
modes are 'r' -- read, 'w' -- write, and 'a'
append. An open file is an object with methods appropriate for the way it was opened.
For example, files opened for reading have methods such as read,
readline, and readlines.
lines = f.readlines()
# reads all the lines of a file into a list.
An open file will also behave like a list in some contexts so you can write
for line in f:
process one line
String formatting expressions
(template with % insertion points) % (tuple of values to insert)
File directory operations
- os.listdir(path) -- returns list of files in directory (short names, not full pathnames)
- os.chdir(path) -- changes the working directory to path
The module os.path has great help for path names:
- os.path.getsize(path) to find the size of a file
- isfile(path) -- is it an ordinary file?
- isdir(path) -- is it a directory?
- islink(path) -- is it a symbolic link?
Something to watch out for:
- os.chdir() changes global state: if you call a function, do an
os.chdir, and return then the working directory is still the new one.
|