Python 2 4 Quick Reference Card
Python 2 4 Quick Reference Card
Python 2.4 Quick Reference Card Styles : keyword function/method type replaced_expression variable
basestring1 bool buffer complex dict exception file float
literal module module_filename language_syntax
©2005-2006 - Laurent Pointal <laurent.pointal@laposte.net> Notations : frozenset global int list long object set slice str tuple
V0.56 — 2006531 — PQRC at http://laurent.pointal.org/python/pqrc f(…)→ return value f(…)➤return nothing (procedure) type unicode xrange
1
[x] for a list of x data, (x) for a tuple of x data, may have x{n}→ n basestring is virtual superclass of str and unicode.
Long Python Quick Reference at http://rgruet.free.fr/
times x data. This doc uses string when unicode and str can apply.
Original Python reference at http://www.python.org/doc
Functions
License : Creative Commons [by nc sa]. ENVIRONMENT VARIABLES Constructor functions of builtin types are directly accessible in builtins.
CARDS CONTENT PYTHONCASEOK 1
no case distinction in module→file mapping __import__ abs apply1 callable chr classmethod cmp coerce
1
Environment Variables............................1 Copying Containers............................8 PYTHONDEBUG = -d command-line option compile delattr dir divmod enumerate eval execfile filter
Command-line Options............................1 Overriding Containers Operations......8 PYTHONHOME Modify standard Python libs prefix and exec prefix getattr globals hasattr hash help hex id input intern2
Files Extensions.......................................1 Sequences...............................................8 locations. Use <prefix>[:<execprefix>].
Language Keywords................................1 Lists & Tuples......................................8 1
isinstance issubclass iter len locals map max min oct open
PYTHONINSPECT = -i command-line option
Builtins....................................................1 Operations on Sequences...................8 1 ord pow property range raw_input reduce reload repr
Types...................................................1 Indexing..............................................8 PYTHONOPTIMIZE = -O command-line option
Functions............................................1 Operations on mutable sequences.....8 PYTHONPATH Directories where Python search when importing reversed round set setattr sorted staticmethod sum super
Statements..............................................1 Overriding Sequences Operations......8 modules/packages. Separator : (posix) or ; unichr vars zip
Blocks.................................................1 Mappings (dictionaries)...........................8 1
(windows). Under windows use registry Use f(*args,**kargs) in place of apply(f,args,kargs).
Assignment Shortcuts.........................1 Operations on Mappings.....................8 2
Don't use intern.
HKLM\Sofware\….
Console & Interactive Input/Output.........2 Overriding Mapping Operations..........8
PYTHONSTARTUP File to load at begining of interactive sessions.
Objects, Names and Namespaces...........2
Identifiers............................................2
Other Mappings..................................8
Sets.........................................................9
1
PYTHONUNBUFFERED = -u command-line option STATEMENTS
1
Objects and Names, Reference Operations on Sets.............................9 PYTHONVERBOSE = -v command-line option One statement per line1. Can continue on next line if an expression or a
1
Counting.............................................2 Other Containers Structures, Algorithms 9 If set to non-empty value. string is not finished ( ( [ { """ ''' not closed), or with a \ at end of
Mutable/Immutable Objects...............2 Array...................................................9
line.
Namespaces.......................................2 Queue.................................................9 COMMAND-LINE OPTIONS
Constants, Enumerations...................2 Priority Queues...................................9 Char # start comments up to end of line.
Flow Control............................................2 Sorted List...........................................9 python [-dEhiOQStuUvVWx] [-c cmd | -m mod | file | -] [args]
-d Output debugging infos from parser. pass Null statement.
Condition............................................2 Iteration Tools.....................................9
Loop....................................................2 Date & Time............................................9 -E Ignore environment variables. assert expr[,message] Assertion check expression true.
Functions/methods exit......................2 Module time........................................9 -h Print help and exit. del name[,…] Remove name → object binding.
Exceptions..........................................2 Module datetime...............................10 -i Force interactive mode with prompt (even after script print [>>obj,][expr[,…][,] Write expr to sys.stdout2.
Iterable Protocol.................................2 Module timeit....................................10
Interpretation / Execution.......................2 Other Modules..................................10 execution). exec expr [in globals [, locals]] Execute expr in namespaces.
Functions Definitions & Usage.................2 Files.......................................................10 -O Optimize generated bytecode, remove assert checks. fct([expr[,…]],[name=expr[,…]] Call any callable object fct with given
Parameters / Return value..................2 File Objects.......................................10 -OO As -O and remove documentation strings. [,*args][,**kwargs]) arguments (see Functions Definitions &
Lambda functions...............................2 Low-level Files...................................10 -Q arg Division option, arg in [old(default),warn,warnall,new]. Usage - p2).
Callable Objects..................................2 Pipes.................................................10 Assignment operator3.
-S Don't import site.py definitions module. name[,…] = expr
Calling Functions.................................2 In-memory Files................................10
Functions Control................................3 Files Informations..............................11
-t Warn inconsistent tab/space usage (-tt exit with error).
-u Use unbuffered binary output for stdout and stderr.
1
Multiple statements on same line using ; separator - avoid if not
Decorators..........................................3 Terminal Operations..........................11
Types/Classes & Objects..........................3 Temporary Files.................................11 -U Force use of unicode literals for strings. necessary.
2
Class Definition...................................3 Path Manipulations...........................11 -v Trace imports. Write to any specified object following file interface (write method).
Object Creation...................................3 Directories........................................11 -V Print version number and exit. Write space between expressions, line-return at end of line except with
Classes & Objects Relations...............3 Special Files......................................12 -W arg Emit warning for arg "action:message:category:module:lineno" a final ,.
Attributes Manipulation......................3 Copying, Moving, Removing.............12 3
Left part name can be container expression. If expr is a sequence of
Special Methods.................................3 Encoded Files....................................12
-x Skip first line of source (fort non-Unix forms of #!cmd).
Descriptors protocol...........................3 Serialization......................................12 -c cmd Execute cmd. multiple values, can unpack into multiple names. Can have multiple
Copying Objects..................................3 Persistence.......................................12 -m mod Search module mod in sys.path and runs it as main script. assignments of same value on same line : a = b = c = expr.
Introspection.......................................3 Configuration Files............................12 file Python script file to execute. Other statements (loops, conditions…) introduced in respective parts.
Modules and Packages............................3 Exceptions.............................................12 args Command-line arguments for cmd/file, available in Blocks
Source encodings...............................3 Standard Exception Classes.............12
Special Attributes...............................3 Warnings...........................................13
sys.argv[1:]. A : between statements defines dependant statements, written on same
Main Execution / Script Parameters........3 Exceptions Processing......................13
Operators................................................4 Encoding - Decoding.............................13 FILES EXTENSIONS line or written on following line(s) with deeper indentation.
Blocks of statements are simply lines at same indentation level.
Priority................................................4 Threads & Synchronization...................13 .py=source, .pyc=bytecode, .pyo=bytecode optimized, .pyd=binary
Arithmetic Operators..........................4 Threading Functions.........................13
if x<=0 : return 1
module, .dll/.so=dynamic library. if asin(v)>pi/4 :
Comparison Operators........................4 Threads.............................................13
Operators as Functions.......................4 Mutual Exclusion...............................13 .pyw=source associated to pythonw.exe on Windows platform, to run a = pi/2
Booleans..................................................4 Events...............................................13 without opening a console. b = pi/2
Numbers..................................................4 Semaphores......................................13
Operators............................................4 Condition Variables...........................13 LANGUAGE KEYWORDS else :
Functions............................................4 Synchronized Queues.......................13 a = asin(v)
Bit Level Operations................................5 Process..................................................13
List of keywords in standard module keyword. b = pi/2a
Operators............................................5 Current Process.................................13 and as1 assert break class continue def del elif else Statement continuation lines don't care indentation.
Strings.....................................................5 Signal Handling.................................14 except exec finally for from global if import in is lambda To avoid problems, configure your editor to use 4 spaces in place of
Escape sequences..............................5 Simple External Process Control.......14 not or pass print raise return try while yield tabs.
Unicode strings...................................5 Advanced External Process Control. .15 1
not reserved, but avoid to redefine it.
Methods and Functions.......................5 XML Processing.....................................15 Assignment Shortcuts
Formating...........................................6 SAX - Event-driven............................15 Don't redefine these constants : None, True, False.
Constants............................................6 DOM - In-memory Tree......................16 a += b a = b a *= b a /= b
Regular Expressions...........................6 Databases.............................................17 BUILTINS a //= b a %= b a **= b
Localization.........................................7 Generic access to DBM-style DBs.....17 Available directly everywhere with no specific import. Defined also in a &= b a |= b a ^= b a >>= b a <<= b
Multilingual Support............................7 Standard DB API for SQL databases .17
Containers...............................................8 Tools......................................................18 module __builtins__. Evaluate a once, and assign to a the result of operator before = applied
Operations on Containers...................8
1a 1b 1c
to current a and b. Example : a%=b ≈ a=a%b from enclosing definitions, then globals, then builtins. enumerate(iterable)→ iterator returning tuples (index,value) from iterable
Out-of-scope name → use the dotted attribute notation x.y (maybe Iterators Objects Interface
CONSOLE & INTERACTIVE INPUT/OUTPUT x.y.z.t)… where x is a name visible within the current scope.
print expression[,…] next(self)→ next item1
Class namespace → names defined in a class (class members).
input([prompt]) → evaluation of user input (typed data) __iter__(self)→ iterator object itself
Object namespace → names usable with object.name notation (attributes, 1
When reach end of collection, raise StopIteration exception on
raw_input([prompt]) → str: user input as a raw string methods). subsequent calls (ie. iterator usable only one time on a collection).
Direct manipulation (redefinition) of stdin/stdout/stderr via sys module :
Namespaces can be nested, inner namespaces hidding identical names Generators
sys.stdin sys.stdout sys.stderr from outer namespaces.
sys.__stdin__ sys.__stdout__ sys.__stderr__ dir([object]) → list: names defined in object namespace1 Functions retaining their state between two calls. Return values using
All are files or files-like objects. The __xxx__ forms keep access to yield. Stop generation via simple return or via raise StopIteration.
vars([object]) → dict2: identifier:value of object as a namespace1
original standard IO streams. 1
if object not specified use nearest namespace (locals). 1) build generator from function : gen=generatorfct(args)
Ctrl-C raises KeyboardInterrupt exception. 2
must not be modified. 2) use gen.next() values until StopIteration is raised.
_ → value of last expression evaluation
Constants, Enumerations Generator iterable expressions with : (x for x in iterable where cond)
help([object]) ➤ print online documentation
Use uppercase and _ for constants identifiers (good practice). May define Operations with/on Iterable
sys.displayhook → (rw) fct(value) called to display value namespaces to group constants. Cannot avoid global/local name
sys.__displayhook__ → backup of original displayhook function redefinition (can eventually define namespaces as classes with See Operations on Containers (p8).
sys.ps1 → str: primary interpreter prompt attributes access control - not in Python spirit, and execution cost). See Iteration Tools (p9).
sys.ps2 → str: secondary (continuation) interpreter prompt See third party modules pyenum for strict enum-like namespace.
INTERPRETATION / EXECUTION
See external package ipython for an enhanced interactive Python shell. FLOW CONTROL compile(string1,filename,kind2[,flags3[,dont_inherit3]]) → code object
OBJECTS, NAMES AND NAMESPACES Condition Loop eval(expression[,globals[,locals]]) → value: evaluation4 of expression string
if cond : inst eval(code_object[,globals[,locals]]) → value: evaluation4 of code object
Identifiers for var[,…] in iterable : inst
[ elif cond : inst ] exec5 statements [in globals[,locals]] ➤ statements string1 executed4
Use : [a-zA-Z_][a-zA-Z0-9_]* [ else : inst ]
Special usage for underscore : [ else : inst ] execfile(filename[,globals[,locals]])➤ file interpreted4
_xxx global not imported by import * while cond : inst 1
There is no 'switch' or 'case'. Multi-line statements in source code must use \n as newline, and
_xxx implementation detail, for internal use (good practice) Can use if elif elif… else. [ else : inst ] must be terminated by a newline.
__xxx 'private' class members, defined as _ClassName__xxx Can use a mapping with Exit loop with break. 2
Kind relative to string content, 'exec' → sequence of statements,
__xxx__ normally reserved by Python functions bound to cases. Go to next iteration with continue. 'eval' → single expression, 'single' → single interactive statement.
Case is significant : This_Name != THIS_NAME. 3
Flags and dont_inherit are for future statements (see doc).
Loops else blocs only executed when 4
Objects and Names, Reference Counting In context of globals and locals namespaces.
loop exit normally (without break). 5
Exec is a langage statement, others are builtin functions.
Data are typed objects (all data), names are dynamically bound to Functions/methods exit
objects.
Exit function/method with return [value] FUNCTIONS DEFINITIONS & USAGE
= assignment statement bind result of right part evaluation into left part
Exit from generator body with yield value def fctname([paramname[=defaultvalue][,…]] [,*args][,**kwargs]) :
name(s)/container(s). Examples :
Multiple returned values using tuple data. instructions
a = 3*c+5 a,b = ("Hello","World") x,y,tabz[i] = fct(i)
Cannot yield within a try/finally block.
s = "Hello" pi,e = 3.14,2.71 a,b = b,a Parameters / Return value
Exceptions Parameters are passed by references to objects.
When an object is no longer referenced (by names or by containers), it is
destroyed (its __del__ method is then called). try : inst You can modify values of mutable objects types.
except
[
[ ,
except_class value
]] :
inst You cannot modify values of immutable objects types - as if they were
sys.getrefcount(object)→ int: current reference counter of object … passed by value.
Standard module weakref define tools to allow objects to be garbage [ else : inst ] Notation * → variable list of anonymous parameters in a tuple.
collected when necessary and dynamically re-created on-demand. Can have a tuple of classes for except_class. Not specifying a class Notation ** → variable list of named parameters in a dict.
catch all exceptions. Return value(s) with return [value[,…]]
Mutable/Immutable Objects
Block else executed when try block exit normally. For multiple values, return a tuple. If no return value specified or if end
Mutable objects can be modified in place. Immutable objects cannot be
try : inst of function definition reached, return None value.
modified (must build a new object with new value).
Immutable : bool, int, long, float, complex, string, unicode, finally : inst Lambda functions
tuple, frozenset, buffer, slice. lambda param[,…] : expression
Process finally block in all execution paths (normal or exception).
Mutable : list, set, dict and other high level class objects. Anonymous functions defined inline. Result of expression evaluation is
raise exception_class[,value[,traceback]]
There is no constant definition. Just use uppercase names to identify returned (it must be an expression, no loop, no condition).
symbols which must not be modified. raise exception_object
Expression uses values known at definition time (except for params).
raise
Namespaces Last form re-raise the currently catched exception in an exception Callable Objects
Places where Python found names. handler. Objects having a __call__ method can be used as functions.
Builtins namespace → names from module __builtins__, already Iterable Protocol Methods bound to objects can be used as functions : f = o.meth
available. callable(x) → bool: test x callable with x(…)
Generic and simple protocol allowing to iterate on any collection of data.
Global namespace → names defined at module level (zero indentation).
Local namespace → names defined in methods/functions. Objects of class defining __iter__ or __getitem__ are iterable (directly Calling Functions
usable in for loops). [name=] fctname([expr[,…]][,name=expr[,…][,*args][,**args])
del name ➤ remove existing name from namespace (remove object binding)
__iter__(self) → iterator on self Anonymous parameters passed in parameters order declaration.
globals() → dict: identifier→value of global namespace
iter(object) → iterator on iterable object Params having default value can be omitted.
locals() → dict: identifier→value of local namespace
iter(callable,sentinel) → iterator returning callable() values up to sentinel Notation * → pass variable list of anonymous parameters in a tuple.
Current scope → names directly usable. Searched in locals, then locals
2a 2b 2c
Notation ** → pass variable list of named parameters in a dict. Object Life For copying, objects can define pickling protocol too (see Files -
Functions Control __new__(classref,initargs…)→ object of classref type, already initialized1 Serialization - p12), in place of __copy__ and __deepcopy__.
sys.getrecursionlimit()→ int: current recursion limit for functions __init__ (self,initargs…)➤ called to initialize object with initargs Introspection
sys.setrecursionlimit(limit) ➤ set recursion limit for functions __del__ (self)➤ called when object will be destroyed Beyond this documentation. Many __xxx___ attributes are defined,
1
If don't return a classref object, then object.__init__ is called with some are writable (see other docs).
Decorators
initargs. See standard module inspect to manipulate these data.
Glue code (functions) called at functions and methods definitions time,
return the final function/method (generally with wrapping code). Object Cast Example of Introspection Attributes
@decoratorname
( decorator_arguments
[ ] […]
) __repr__(self)→ str: called for repr(self) and `self` Note: classes are objects too!
def fct(fct_rguments):… __str__(self) → str: called for str(self) and print self __base__ → list: parent classes of a class
@dec1 @dec2(args) @dec3 def fct(…):… __coerce__(self,other) → value, called for coerce(self,other) __slots__ → tuple: allowed objects attributes names1 of a class
like __class__ → class/type: object's class
def fct(…):… ➤ fct = dec1(dec2(args)(dec3(fct)))) Object Hash Key
__dict__ → dict: defined attributes (object namespace) of an instance
See page PythonDecoratorLibrary in python.org Wiki for some decorators __hash__(self)→ int: hash code for object, used for hash(obj)and quick
__doc__ → string: documentation string of a package, module, class, function
definitions. mapping keys comparison - default implementation use hash(id(self))
__name__ → str: object definition name of a function
TYPES/CLASSES & OBJECTS Attributes access
__file__ → string: pathname of loaded module .pyc, .pyo or .pyd
All data are typed objects relying to classes. See also "Descriptors protocol" infra. 1
List of allowed attributes names. Usage discouraged.
type(o) → type: type object of o __getattr__(self,name)→ value, called for undefined attributes
__getattribute__(self, name)→ value, always called
Standard module types define type objects for builtins types. MODULES AND PACKAGES
__setattr__(self, name, value)➤ called for obj.name=value
Class Definition File gabuzo.py ➤ module gabuzo.
__delattr__(self, name)➤ called for del obj.name
class classname [(parentclass[,…])] : Directory kramed/ with a file __init__.py ➤ package kramed.
__hash__(self)→ int: 32 bits hash code, called for hash(obj) and dict
varname = expr ➤ varname defined in classname namespace Can have sub-packages (subdirectories having __init__.py file).
operations
def metname(self[,…]): ➤ define methods like functions __call__(self, *args, **kwargs)→ value, called for obj(…) Searched in the Python PATH.
Support multiple inheritance. Can inherit from builtin class. Current Python PATH stored in sys.path list. Contains directories and
Inherit at least from object base class => Python 'new style class'. Static method / Class method
.zip files paths. Built from location of standard Python modules,
First parameter of methods is target object, standard use self name. Use standard decorators (see Decorators p3). PYTHONPATH environment variable, directory of main module given on
Access class members via class name, object members via self. class ClassName : command line, data specified in lines of .pth files found in Python home
This doc consider you use new style class (inheriting from object). @staticmethod directory, and data specified in registry under Windows.
def methodname(…): … Current list of loaded modules stored in sys.modules map (main module
Metaclass
@classmethod is under key __main__).
Class definition create a new type. It can be done 'by hand' with :
def methodname(classref,…): … import module [as alias] [,…]
x = type('classname',(parentclass,[…]),{varname:expr[,…]}
Descriptors protocol from module import name [as alias] [,…]
def metname(self[,…]):
Descriptors are attribute objects controling access to attributes values. from module import *
x.metname = metname
This allow creation of metaclass classes (class building other class). They must define some of following methods : reload(module) ➤ module is reloaded (but old references still on old module
__get__(self,obj,ownerclass)→ attribute value for obj content)
Object Creation Import can use package path (ex:from encoding.aliases import…).
__set__(self,obj,value)➤ modify attribute in obj, set to value
obj = ClassName(initargs…) Direct import from a package use definitions from __init__.py file.
__delete__(self,obj)➤ remove attribute from obj
In cas of exception during initialization, object is destroyed when exiting Very careful with import * as imported names override names already
init code (reference counter reach zero). In these methods self is the descriptor object, and obj is the target
defined.
object which attribute is manipulated.
Classes & Objects Relations To limit your modules names exported and visible by import *, define
Properties
isinstance(obj,classinfo) → bool: test object of type/class classinfo module global __all__ with list of exported names (or use global
A descriptor to directly bind methods/functions to control attribute names _xxx).
issubclass(aclass, aparent) → bool: test same class or parent relationship
access. Use builtin type property with init args. See __import__ builtin function, imp builtin module and ihooks module.
Prefer isinstance() to type() for type checking.
class MyClass : __import__(modulename[, globals[,locals[,lnameslist]]])
Parent class methods are not automatically called if overriden in
attributename = property(getter,setter,deleter,description)
subclass - they must be explicitly called if necessary. Source encodings
Each init arg default to None (ie. undefined).
Call parent methods via super function : See PEP 263. Declare source files encoding in first or second line in a
super(ThisClass,self).methodname(self,args…) Copying Objects special comment.
Or the old way, via parent class namespace : Assignment only duplicate references. To shallow copy an object (build a # * coding: encoding_name *
new one with same values - referencing same content), or to deep copy If this is not specified, Python use sys.getdefaultencoding() value
ParentClass.methodname(self,args…)
an object (deep-copying referenced content), see object copy methods, (see modules sitecustomize.py and user.py).
Attributes Manipulation and functions in standard module copy. It is important to specify encoding of your modules as u"…" strings use
object.name = value copy.copy(object)→ value: shallow copy of object it to correctly build unicode literals.
setattr(object,name,value) ➤ object attribute set to value copy.deepcopy(object[[,memo],_nil])→ value: deep copy of object1 Special Attributes
object.name → value of object attribute 1
Params memo and nil are used in recursive deepcopy, their default __name__ → str: module name, '__main__' for command-line called script
getattr(object,name[,default])→ value of object attribute values are None and empty list.
__file__ → string: pathname of compiled module loaded
del object.name Copy Protocol
delattr(object,name) ➤ object attribute removed MAIN EXECUTION / SCRIPT PARAMETERS
__copy__(self)→ value: shallow copy of self, called by copy.copy(…)
Special Methods The 'main' module is the module called via command-line (or executed
__deepcopy__(self,memo)→ value: deep copy of self, called by
Other special overridable __xxx___ methods are listed in respective by shell with first script line #! /bin/env python).
copy.deepcopy(…) Command-line parameters are available in sys.argv (a python list).
sections.
3a 3b 3c
At end of module, we may have : Comparison Overriding BOOLEANS
if __name__=='__main__' :
__lt__(self, other)→ bool1 : called for self < other False : None, zero numbers, empty containers. False → 0.
# main code
__le__(self, other)→ bool1 : called for self <= other True : if not false. True → 1.
# generally call a 'main' function:
mainfunction(sys.argv[1:]) __gt__(self, other)→ bool1 : called for self > other bool(expr) → True | False
# or in lib modules, execute test/demo code... __ge__(self, other)→ bool1 : called for self >= other Logical not : not expr
__eq__(self, other)→ bool1 : called for self == other Logical and : expr1 and expr2
Execution exit after last main module instruction (in multithread, wait
__ne__(self, other)→ bool1 : called for self != other Logical or : expr1 or expr2
also for end of non-daemon threads), unless interactive mode is forced.
and for self <> other Logical and and or use short path evaluation.
Can force exit with calling sys.exit(code), which raise a SystemExit
exception - see Current Process - Exiting (p13). __cmp__(self,other)→ int : called for self compared to other, Bool Cast Overriding
self<other→value<0, self==other→value=0, self>other→value>0 __nonzero__(self) → bool : test object itself1
OPERATORS 1
Any value usable as boolean value, or a NotImplemented value if 1
If __nonzero__ undefined, look at __len__, else object is true.
Deal with arithmetic, boolean logic, bit level, indexing and slicing. cannot compare with such other type.
Priority Operators as Functions
NUMBERS
1 (a,…) [ , ] 6 x+y 11 x<y x<=y x>y x>=y x==y x!=y Builtin integer types : int (like C long), long (unlimited integer)
a … Operators are also defined as functions in standard operator module.
{
a : }
, …
b ̀
… ̀
xy x<>y x is y x is not y x in s
int(expr[,base=10]) → int: cast of expr
Comparison
x not in s long(expr[,base=10]) → long: cast of expr
lt(a,b) = __lt__(a,b) ne(a,b) = __ne__(a,b) Builtin floating point types : float (like C double), complex (real and
2 s[i] s[i:j] 7 x<<y 12 not
x
le(a,b) = __le__(a,b) ge(a,b) = __ge__(a,b) imaginary parts are float).
s.attr f(…) x>>y
eq(a,b) = __eq__(a,b) gt(a,b) = __gt__(a,b) float(expr) → float: representation of expr
3 +
x
x ~
x
8 x&y 13 x and y complex(x[,y]) → complex: number: x+yj
Logical / Boolean
4 x**y 9 x^y 14 x or y [x+]yj → complex: number, ex: 3+4j -8.2j
not_(o) = __not__(o) and_(a,b) = __and__(a,b)
5 x*y x/y x%y 10 x|y c.real → float: real part of complex number
15 lambda
:
args expr
truth(o) or_(a,b) = __or__(a,b)
c.img → float: imaginary part of complex number
Arithmetic Operators is_(a,b) xor(a,b) = __xor__(a,b)
c.conjugate() → complex: conjugate of complex number (real,-img)
Can be defined for any data type. is_not(a,b)
Maximum int integer in sys.maxint.
Arithmetic Overriding Arithmetic Automatic conversions between numeric types.
abs(o) = __abs__(o) truediv(a,b) = __truediv__(a,b) Automatic conversions from int to long when result overflow max int.
__add__(self,other) → value: called for self + other Direct conversions from/to strings from/to int, long… via types
add(a,b) = __add__(a,b) floordiv(a,b) = __floordiv__(a,b)
__sub__(self,other) → value: called for self other constructors.
sub(a,b) = __sub__(a,b) neg(o) = __neg__(o) Type Decimal defined in standard module decimal.
__mul__(self,other) → value: called for self * other
mul(a,b) = __mul__(a,b) pos(o) = __pos__(o) Base fixed type compact storage arrays in standard module array.
__div__(self,other) → value: called1 for self / other
div(a,b) = __div__(a,b) pow(a,b) = __pow__(a,b)
__truediv__(self,other) → value: called2 for self / other Operators
mod(a,b) = __mod__(a,b)
__floordiv__(self,other) → value: called for self // other x +x x+y xy x*y x/y 1 x//y 1 x%y 2 x**y 2
Bit Level
__mod__(self,other) → value: called for self % other
1
With from __future__ import division, / is true division (1/2→0.5),
lshift(a,b) = __lshift__(a,b) and // is floor division (1//2→0). Else for integers / is still floor division.
__divmod__(self,other) → value: called for divmod(self,other)
rshift(a,b) = __rshift__(a,b) 2
% is remainder operator, ** is power elevation operator (same as pow).
__pow__(self,other) → value: called for self ** other
inv(o) = invert(o) = __inv__(o) = __invert__(o)
__nonzero__(self)→ value: called for nonzero(self) Functions
Sequences Some functions in builtins.
__neg__(self) → value: called for self
concat(a,b) = __concat__(a,b) abs(x) → absolute value of x
__pos__(self) → value: called for +self
contains(a,b) = __contains__(a,b) divmod(x,y) → (x/y,x%y)
__abs__(self) → value: called for abs(self)
countOf(a,b) oct(integer) → str: octal representation of integer number
__iadd__(self,other) ➤ called for self += other
indexOf(a,b) hex(integer) → str: hexadecimal representation of integer number
__isub__(self,other) ➤ called for self = other
repeat(a,b) = __repeat__(a,b) Math Functions
__imul__(self,other) ➤ called for self *= other
setitem(a,b,c) = __setitem__(a,b,c) Standard floating point functions/data in standard math module.
__idiv__(self,other) ➤ called1 for self /= other
getitem(a,b) = __getitem__(a,b) acos(x) → float: radians angle for x cosinus value : [-1…1] →[0…π]
__itruediv__(self,other) ➤ called2 for self /= other
delitem(a,b) = __delitem__(a,b) asin(x) → float: radians angle for x sinus value : [-1…1] →[-π/2…+π/2]
__ifloordiv__(self, other) ➤ called for self //= other
setslice(a,b,c,v) = __setslice__(a,b,c,v) atan(x) → float: radians angle for x tangent value : [-∞…∞] →]-π/2…+π/2[
__imod__(self,other) ➤ called for self %= other
getslice(a,b,c) = __getslice__(a,b,c) atan2(x,y) → float: randians angle for x/y tangent value
__ipow__(self,other) ➤ called for self **= other
1
without / 2 with from __futur__ import division delslice(a,b,c) = __delslice__(a,b,c) ceil(x) → float: smallest integral value >= x
Binary operators __xxx__ have also __rxxx__ forms, called when Type Testing cos(x) → float: cosinus value for radians angle x
target object is on right side. These functions must be considered as not reliable. cosh(x) → float: hyperbolic cosinus value for radians angle x
Comparison Operators isMappingType(o) exp(x) → float: exponential of x = ex
Operators can compare any data types. isNumberType(o) fabs(x) → float: absolute value of x
Compare values with < <= > >= == != <>. isSequenceType(o) floor(x) → float: largest integral value <= x
Test objects identity with is and is not (compare on id(obj)). fmod(x,y) → float: modulo = remainder of x/y
Attribute and Item Lookup
Direct composition of comparators is allowed in expressions : x<y<=z>t. frexp(x) → (float,int): (m,y) m mantissa of x,y exponent of x — where
Builtin function cmp(o1,o2) → 1 (o1 < o2), 0 (o1 == o2), 1 (o1 > o2) attrgetter(attr) → fct: where fct(x)→x.attr
x=m*2y
itemgetter(item) → fct: where fct(x)→x[item] ldepx(x,i) → x * (2**i)
4a 4b 4c
log(x) → float: neperian logarithm of x x&y → bitwise and on x and y unicode(object[,encoding[,errors]]) → unicode: unicode
log10(x) → float: decimal logarithm of x x|y → bitwise or on x and y sys.maxunicode → int: maximum unicode code=fct(compile time option)
modf(x) → (float{2}): (f,i) f signed fractional part, i signed integer part x<<n → x shifted left by n bits (zeroes inserted)
Unicode Chars Informations
pow(x,y) → float: x raised to y power x>>n → x shifted right by n bits (zeroes inserted)
Module unicodedata contains informations about Unicode chars
sin(x) → float: sinus value for radians angle x Binary structures manipulations in standard module struct. properties, names.
sinh(x) → float: hyperbolic sinus value for radians angle x Advanced binary structures mapping and manipulation in third party lookup(name) → unicode char from its name
sqrt(x) → float: square root of x (√x) modules : ctypes, xstruct,…
name(unichr[,default]) → str: unicode name - may raise ValueError
tan(x) → float: tangent value for radians angle x Bit Level Overriding
tanh(x) → float: hyperbolic tangent value for radians angle x decimal(unichr[,default]) → int: decimal value - may raise ValueError
__and__(self,other) → value: for self & other digit(unichr[,default]) → int: digit value - may raise ValueError
pi → float: value of π (pi=3.1415926535897931)
__or__(self,other) → value: for self | other numeric(unichr[,default]) → float: numeric value - may raise ValueError
e → float: value of neperian logarithms base (e=2.7182818284590451)
__xor__(self,other) → value: for self ^ other
Module cmath provides similar functions for complex numbers. category(unichr) → str: general unicode category of char
__lshift__(self,other) → value: for self << other
Random Numbers bidirectional(unichr) → str: bidir category of char, may be empty str
__rshift__(self,other) → value: for self >> other
Randomization functions in standard random module. Module functions combining(unichr) → str/0: canonical combining class of char as integer
__invert__(self) → value: for ~self
use an hidden, shared state, Random type generator (uniform east_asian_width(unichr) → str: east asian width
__iand__(self,other) ➤ called for self &= other
distribution). __ior__(self,other) ➤ called for self |= other mirrored(unichr) → int: mirrored property in bidi text, 1 if mirrored else 0
Functions also available as methods of Random objects. decomposition(unichr) → str: decomposition mapping, may be empty str
__ixor__(self,other) ➤ called for self ^= other
seed([x]) ➤ initialize random number generator
__ilshift__(self,other) ➤ called for self <<= other normalize(form, unistr) → str: normal form of string - form in 'NFC',
random()→ float: random value in [0.0, 1.0[ 'NFKC', 'NFD', 'NFKD'
__irshift__(self,other) ➤ called for self >>= other
randint(a,b)→ int: random value in [a, b] unidata_version → str: version of Unicode database used
uniform(a, b)→ float: random value in [a, b[ STRINGS
Methods and Functions
getrandbits(k)→ long: with k random bits Simple quoted 'Hello' or double-quoted "Hello".
From builtins (see also oct and hex functions for integers to strings) :
randrange([start,]stop[,step])→ int: random value in range(start, stop, Use triple [simple|double] quotes for multi-lines strings :
len(s) → int: number of chars in the string
step) """Hello,
how are you ?""" Most string methods are also available as functions in the standard
choice(seq)→ value: random item from sequence
Strings are immutable (once created a string cannot be modified in string module.
shuffle(x[,rndfct]) ➤ items of x randomly reordered using rndfct()
place). s.capitalize() → string with first char capitalized1
sample(population,k)→ list: k random items from polulation
Strings can contain binary data, including null chars (chars of code 0). s.center(width[,fillchar]) → string centered
Alternate random distributions : betavariate(alpha,beta), Strings are sequences, see Indexing (p8) for chars indexation (slicing) s.count(sub[,start[,end]]) → int: count sub occurences within start-end
expovariate(lambd), gammavariate(alpha,beta), gauss(mu,sigma), and other operations. s.decode([encoding[,errors]]) → unicode: text decoded - see encodings
lognormvariate(mu, sigma), normalvariate(mu,sigma), chr(code) → char s.encode([encoding[,errors]]) → str: ext encoded - see encodings
vonmisesvariate(mu,kappa), paretovariate(alpha), ord(char) → code s.endswith(suffix[,start[,end]]) → bool
weibullvariate(alpha,beta). str(expr)→ readable textual representation of expr - if available s.expandtabs([tabsize]) → string with tabs replaced by spaces
Alternate random generator WichmannHill class. `expr` → readable textual representation of expr - if available s.find(sub[,start[,end]]) → int/1: offset of sub within start-end
Direct generator manipulation : getstate(), setstate(state), repr(expr)→ evaluable textual representation of expr - if available s.index(sub[,start[,end]]) → int: offset of sub - may raise ValueError
jumpahead(n).
Escape sequences s.isalnum() → bool: non empty string with all alphanumeric chars1
In module os, see :
\a - bell \v - vertical tab s.isalpha() → bool: non empty string with all alphabetic chars1
os.urandom(n) → str: n random bytes suitable for cryptographic use
\b - backspace \' - single quote s.isdigit() → bool: non empty string with all digit chars1
Other Math Modules \e - escape \" - double quote s.islower() → bool: non empty string with all lower chars1
Advanced matrix, algorithms and number crunching in third party \f - form feed \\ - backslash s.isspace() → bool: non empty string with all space chars1
modules like numarray / Numeric (known as NumPy), gmpy \n - new line \ooo - char by octal ooo value
s.istitle() → bool: non empty string with titlecase words1
(multiprecision arithmetic), DecInt, scipy, … \r - carriage return \xhh - char by hexadecimal hh value
s.isupper() → bool: non empty string with all upper chars1
\t - horizontal tab \<newline> - continue string on next line.
See sites SciPy, BioPython, PyScience,… s.join(seq) → string: seq[0]+s+seq[1]+s+…+seq[n-1]
And for Unicode strings :
s.ljust(width[,fillchar]) → text string left aligned2
Numbers Casts Overriding \uxxxx - unicode char by 16 bits hexadecimal xxxx value.
s.lower() → text string lowered1
__int__(self) → int: called for int(self) \Uxxxxxxxx - unicode char by 32 bits hexadecimal xxxxxxxx value.
\N{name} - unicode char by name in the Unicode database. s.lstrip([chars]) → string text with leading chars2 removed
__long__(self) → long: called for long(self)
Keep \ escape chars by prefixing string literals with a r (or R) - for 'raw' s.replace(old,new[,count]) → string with count firsts old replaced by new
__float__(self) → float: called for float(self)
strings (note : cannot terminate a raw string with a \). s.rfind(sub[,start[,end]]) → int/1: last offset of sub within start-end
__complex__(self) → complex: called for complex(self) s.rindex(sub[,start[end]])→ int: last offset of sub - may raise ValueError
__oct__(self) → str: called for oct(self) Unicode strings
s.rjust(width[,fillchar]) → string text right aligned2
__hex__(self) → str: called for hex(self) Quoted as for str, but with a u (or U) prefix before the string : u"Voiçi"
s.rsplit([sep[,maxsplit]])→ [string]: rightmost words delim. by sep2
__coerce__(self,other) → value: called for coerce(self,other) U"""Une bonne journée
s.rstrip([chars]) → string with trailing chars2 removed
en perspective."""
BIT LEVEL OPERATIONS Can mix strings prefixs r (or R) and u (or U). s.split([sep[,maxsplit]]) → [string]: words delimited by sep2
You must define your source file encoding so that Python knows how to s.splitlines([keepends]) → [string]: lines
Work with int and long data.
convert your source literal strings into internal unicode strings. s.startswith(suffix[,start[,end]]) → bool
Operators
unichr(code) → unicode: string of one unicode char s.strip([chars]) → string text with leading+trailing chars2 removed
~x → inverted bits of x
ord(unicode char) → int: unicode code s.swapcase() → string with case switched1
x^y → bitwise exclusive or on x and y
s.title() → string with words capitalized1
5a 5b 5c
s.translate(table[,deletechars]) → string: cleaned, converted3 See also external projects pyparsing, PLY (Python Lex-Yacc), tpg (Toy Flag Options
s.upper() → string uppered1 Parser Generator)… IGNORECASE (I) : case insensitive expression - not locale dependant.
s.zfill(witdh) → string: numeric string with zeroes if necessary Expressions LOCALE (L) : make \w \W \b \B locale dependant.
1
Locale dependant for 8 bits strings. Metacharacters : . ^ $ * + ? { } [ ] \ | ( ), may use \ escape. MULTILINE (M) : ^ and $ match begining/end of string and lines. Else ^
2
Default chars/separator/fillchar is space. and $ match only beginning and end of string.
. ➤ match any character except a newline (including newline with
3
For str table must be a string of 256 chars - see string.maketrans(). DOTALL (S) : make . match any char including newline. Else newline
DOTALL option)
For Unicode no deletechars, and table must be a map of unicode excluded.
ordinals to unicode ordinals. ^ ➤ match start of string (and start of lines with MULTILINE option)
$ ➤ match end of string (and end of lines with MULTILINE option) UNICODE (U) : make \w \W \b \B unicode dependant.
Formating VERBOSE (X) : ignore whitespaces and make # starting comments (except
expr* ➤ match 0 or more repetitions of expr (as much as possible)
Use % operator between format string and arguments : string%args expr+ ➤ match 1 or more repetitions of expr (as much as possible) when space and # are escaped or in char class).
Formating string contains %[(name)][flag][width][.precision]code expr? ➤ match 0 or 1 expr Matching and Searching
If not use %(name)… → args = single value or tuple of values. expr*? ➤ match like expr* but as few as possible
Can use re functions, or compile expressions into SRE_Pattern objects
If use %(name)… → args = mapping with name as keys. expr+? ➤ match like expr+ but as few as possible
and use their methods.
For mapping, args can be an object with __getitem__ method - see expr?? ➤ match like expr? but as few as possible
See Flag Options supra for flags parameters.
Overriding Mapping Operations (p8). expr{m} ➤ match m repetitions of expr
search(pattern,string[,flags])→ MatchObject/None: scan throught string to
expr{[m],[n]} ➤ match from m to n repetitions of expr, missing m default
Format char codes find substring matching pattern
to 0 and missing n default to infinite
d signed int. decimal : -324 i signed int. decimal : -324 match(pattern,string[,flags])→ MatchObject/None: try to match string with
expr{[m],[n]}? ➤ match like expr{[m],[n]} but as few as possible
pattern
o unsigned octal : 774 u unsigned decimal 6953 [set] ➤ match one char in the set defined by :
split(pattern,string[,maxsplit=0])→ [string]: split string by occurences of
x unsigned hexa : f3a X unsigned hexa : F3A ^ → at begining, invert set definition pattern - if maxsplit specified, remainder is put in last item of list
e float. point exp. : 3.256e12 E float. point exp. : 3.256E12 xy → chars from x to y
findall(pattern,string[,flags])→ [string]/[(string)]: find non-overlapping
f float. point dec. : 0.0000032 F float. point dec. : 0.0000032 \x → see Escape sequences for strings (p5) substrings matching pattern - eventually empty matchs - return list of tuples if
\ , \] → chars and ] ( and ] at the beginning match and ] chars) pattern has groups
g like e or f G like E or F
x → char x (including other re metacharacters) finditer(pattern,string[,flags])→ iterator over [MatchObject] - same as
c character (1 char str or code) % %% → %
exprA|exprB ➤ match exprA or exprB, short path evaluation findall but with an iterator
r object format like repr(object) s object format like str(object) (expr) ➤ match expr and build a numbered group sub(pattern,repl,string[,count=0])→ string: replace substrings matching
Templates (?[i][L][m][s][u][x]) ➤ (at least one ot iLmsux char) group match empty pattern by repl - repl as string can contain back references1 to identified
substring - repl as fct(MatchObject) return replacement string - pattern may be
With string.Template objects. Use common $ syntax : $$➤single $ ; string, modify options flags for entire expression - see I L M S U X
RE_Pattern object
options
$name or ${name} ➤ value for name. subn(pattern,repl,string[,count=0])→ (string,int): same as sub, 2nd item is
(?:expr) ➤ match expr but dont build a group
tmpl = string.Template(template_string) count of substitutions
(?P<name>expr) ➤ match expr and build a group numbered and named
tmpl.substitute(mapping[,**kwargs]) → string: template filled escape(string)→ string: non-alphanumerics backslashed
(name must be valid Python identifier)
tmpl.safe_substitute(mapping[,**kwargs]) → string: template filled (?P=name) ➤ match text matched by earlier group named name If you need to reuse a pattern, compile it one time for all.
tmpl.template → string (?#text) ➤ no match, text is just a comment pat = re.compile(pattern[,flags])→ RE_Pattern object
Can subclass Template to build your own templating (see doc, sources). (?=expr) ➤ match if match expr but don't consume input pat.match(string[,pos[,endpos]]) → same as match function2
See also modules formatter, textwrap. (?!expr) ➤ match if doesn't match expr but don't consume input pat.search(string[,pos[,endpos]]) → same as search function2
Constants (?<=expr) ➤ match if current position is immediatly preceded by a pat.split(string[,maxsplit=0])→ same as split function2
match for fixed length pattern expr
Standard module string provide several constants (do not modify, they pat.findall(string[,pos[,endpos]])→ same as findall function2
(?<!expr) ➤ match if current position is immediatly not preceded by a
are used in string manipulation functions) and some str functions are pat.finditer(string[,pos[,endpos]])→ same as finditer function2
match for fixed length pattern expr
not available as methods. pat.sub(repl,string[,count=0])→ same as sub function
(?(num/name)yesexpr[|noexpr]) ➤ try to match yesexpr if group
ascii_letters → str: lowercase and uppercase chars num/name exists, else try to match noexpr pat.subn(pattern,repl,string[,count=0])→ same as subn function
ascii_lowercase → str: lowercase a-z chars pat.flags → int: flags used at compile time
Escape Sequences
ascii_uppercase → str: uppercase A-Z chars pat.pattern → string: pattern used at compile time
\n \nn ➤ match3 group number n (nn) where first n≠0
digits → str: 0-9 decimal digit chars pat.groupindex→ dict: mapping of group names to group numbers
\ooo \0o ➤ match3 char with octal value ooo (0o)
hexdigits → str: 0-9a-fA-F hexadecimal digit chars
\A ➤ match only at the start of the string Several functions/methods return MatchObject objects.
letters → str: lowercase and uppercase chars1 \b ➤ match3 empty string at beginning or end of a word1+2 m.expand(template)→ string: do backslash substitution on template (like sub
lowercase → str: lowercase a-z chars1 \B ➤ match empty string not at beginning or end of a word1+2 method) using match object groups values
octdigits → str: 0-7 octal digit chars \d ➤ match char class decimal digit [09] m.group([group[,…]])→ string/(string): subgroups of the match from numbers
punctuation → str: ascii chars considered as punctuation in C locale \D ➤ match char class non-digit [^09] or names
printable → str: printable chars \s ➤ match char class whitespace [ \t\n\r\f\v] m.groups([default=None])→ (string): all subgroups of the match - default
\S ➤ match char class non-whitespace [^ \t\n\r\f\v] give access to subgroups not in the match
uppercase → str: uppercase A-Z chars1
\w ➤ match char class alphanumeric [azAZ09_] m.groupdict([default=None])→ dict: name→subgroup: all named subgroups
whitespace → str: whitespace chars (spc, tab, cr, lf, ff, vt)
\W ➤ match char class non-alphanumeric [^azAZ09_] of the match - default give access to subgroups not in the match
capwords(s) → str: split → capitalize → join m.start([group=0])→ int: index of start of substring matched by group, -1 if
\Z ➤ match end of string
maketrans(from,to) → translation table usable in str.translate - from and to group exist but not in match
\a \b \f \n \r \t \v \x \\ ➤ same as string escapes
must have same length m.end([group=0])→ int: index of end of substring matched by group, 1 if
1
Definition is locale dependant. \c ➤ for other c chars, match char c
group exist but not in match
1
Regular Expressions Depends on UNICODE flag. m.span([group=0])→ (int{2}): values of start and end methods for the
2
Depends on LOCALE flag. group>
Standard module re has a powerfull regexp engine. See regexp HOWTO 3
When out of char class definition ([…]) m.pos → int: pos value of search/match method
at http://www.amk.ca/python/howto/regex/.
Use raw string r"…" notation. m.endpos → int: endpos value of search/match method
6a 6b 6c
m.lastindex → int/None: index of last matched capturing group key nl_langinfo() value usage Base API
MON_1… MON_12 name of the nth month
m.lastgroup → string/None: name of last matched capturng group bindtextdomain(domain[,localedir])→ str: bounded directory - bind domain
ABMON_1… abbreviated name of the n month
th
to localedir directory if specified (used when searching for .mo files)
m.re → RE_Pattern: pattern used to produce match object
ABMON_12 bind_textdomain_codeset(domain[,codeset])→ codeset binding: bind
m.string → string: string used in match/search to produce match object domain to codeset if specified - change xxgettext() returned strings encoding
RADIXCHAR radix character (decimal dot/comma/…)
1
Back references extended to \g<groupnum> and \g<groupname>. textdomain([domain])→ global domain: set global domain if specified and not
1 THOUSEP separator character for thousands
Using part of string between pos and endpos. None
Group number 0 correspond to entire matching. YESEXPR regular expression (of C library!) usable for yes reply
gettext(message)→ string: localized translation of message - based on
Localization NOEXPR regular expression (of C library!) usable for no reply
current global domain, language, and locale directory - usually aliased as _ in
CRNCYSTR currency symbol, preceded by if should appear before local namespace
Standard module locale provide posix locale service (internationa-
the value, by + if should appear after the value, by . if lgettext(message)→ string: like gettext(), using preferred encoding
lization). should replace radix character
setlocale(category[,locale]) → current/new settings: if locale specified dgettext(domain,message)→ string: like gettext(), looking in specified
ERA era - generally not defined - same as E format in domain.
(string or tuple(language code, encoding), modify locale settings for category
strftime() ldgettext(domain,message)→ string: like dgettext(), using preferred
and return new one - if locale not specified or None, return current locale - not
thread safe ERA_YEAR year in era encoding
localeconv()→ dict: database of local conventions ERA_D_T_FMT usable as format for strftime() for date and time with era ngettext(singular,plural,n)→ string: like gettext(), but consider plural
forms (see Python and GNU gettext docs)
nl_langinfo(option)→string: locale-specific informations - not available on ERA_D_FMT usable as format for strftime() for date with era
all systems - options may vary on systems - see options p7 lngettext(singular,plural,n)→ string: like ngettext(), using preferred
ALT_DIGITS up to 100 values representing 0 to 99
encoding
getdefaultlocale([envvars])→(language code, encoding): try to determine
default locale settings localeconv keys dngettext(domain,singular,plural,n)→ string: like ngettext(), looking in
specified domain.
getlocale([category])→ current LC_* setting for category - category default key meaning
ldngettext(domain,singular,plural,n)→ string: like dngettext(), using
to LC_CTYPE - for language code and ancoding it may be None currency_symbol Local currency symbol for monetary values.
preferred encoding
getpreferredencoding([do_setlocale])→ str: user preffered encoding for decimal_point Decimal point character for numbers.
Generally _ is bound to gettext.gettext, and translatable strings are
text data - set do_setlocale to False to avoid possible call to setlocale() frac_digits Number of fractional digits used in local
written in sources using _('thestring'). See docs for usage
normalize(localename)→ normalized locale code for localename - usable with formatting of monetary values.
examples.
setlocale() - return localename if normalization fails grouping [int]: relative positions of 'thousands_sep' in
Class based API
resetlocale([category]) ➤ reset locale for category to default setting - numbers. CHAR_MAX at the end stop grouping.
0 at the end repeat last group. The recommended way. Module gettext defines a class Translations,
category default to LC_ALL
int_curr_symbol International currency symbol of monetary dealing with .mo translation files and supporting str/unicode strings.
strcoll(s1,s2)→ int: compare two strings - follow LC_COLLATE setting -
return 0 if s1==s2, <0 if s1<s2, >0 if s1>s2
values. find(domain[,localedir[,languages[,all]]])→ str/None: .mo file name for
strxfrm(string)→ string:transform string for locale-aware comparison int_frac_digits Number of fractional digits used in international translations (search in localedir/language/LC_MESSAGES/domain.mo)
formatting of monetary values. translation(domain[,localedir[,languages[,class_[,fallback[,codeset]]]]])→
format(format,val[,grouping])→ string:convert val float using format (%
mon_decimal_point Decimal point used for monetary values. Translations: object from class class_ (default to GNUTranslations,
operator conventions) - follow LC_NUMERIC settings (decimal point, + constructor take file object as parameter) - if true fallback allow to return a
mon_grouping Equivalent to 'grouping', used for monetary
grouping if it is true) NullTranslations if no .mo file is found, default to false (raise IOError) -
values.
str(float)→ string: convert float - follow LC_NUMERIC settings (decimal codeset change charset used to encode translated strings
point)
mon_thousands_sep Group separator used for monetary values.
True if currency symbol preceed negative install(domain[,localedir[,unicode[,codeset]]]) ➤ install _ function in
atof(string)→ float: convert string to float - follow LC_NUMERIC settings n_cs_precedes
monetary values, false if it follow. Python's builtin namespace, to use _('thestring')
atoi(string)→ int: convert string to integer - follow LC_NUMERIC settings
n_sep_by_space True if there is a space between currency Null Translations
CHAR_MAX → symbolic constant used by localeconv() symbol and negative monetary value.
The NullTranslations is a base class for all Translations.
Categories n_sign_posn Position of negative sign for monetary values1.
t.__init__([fp]) ➤ initialize translations: fp is a file object - call _parse(fp) if
LC_CTYPE → character type - case change behaviour negative_sign Symbol used to annotate a negative monetary it is not None
value.
LC_COLLATE → strings sorting - strcoll() and strxfrm() functions t._parse(fp) ➤ nothing: subclasses override to read data from the file
p_cs_precedes True if currency symbol preceed positive
LC_TIME → time formating - time.strftime() t.add_fallback(fallback) ➤ add fallback used if cannot found translation
monetary values, false if it follow.
LC_MONETARY → monetary values formating - options from localeconv() for a message
p_sep_by_space True if there is a space between currency
LC_MESSAGES → messages display - os.strerror() - not for Python Define methods gettext, lgettext, ngettext, lngettext as in the
symbol and positive monetary value.
messages base API. And define speciale methods ugettext and ungettext
p_sign_posn Position of positive sign for monetary values1.
LC_NUMERIC → numbers formatting - format(), atoi(), atof() and str() returning unicode strings (other forms return encoded str strings).
positive_sign Symbol used to annotate a positive monetary
of this module (dont modify normal Python number formating) Return translated message, forwarding to fallback if it is defined.
value.
LC_ALL → all locales - used to change/retrieve the locale for all categories Overriden in subclasses.
thousands_sep Character used between groups of digits in
nl_langinfo options numbers. t.info()→ return protected _info attribute
1
t.charset()→ return protected _charset attribute
key nl_langinfo() value usage Possible values : 0=currency and value surrounded by parentheses,
t.output_charset()→ return protected _output_charset attribute
CODESET name of character encoding 1=sign should precede value and currency symbol, 2=sign should (defining encoding used to return translated messages)
D_T_FMT usable as format for strftime() for time and date follow value and currency symbol, 3=sign should immediately precede
t.set_output_charset(charset) ➤ set _output_charset attribute
D_FMT usable as format for strftime() for date value, 4=sign should immediately follow value, LC_MAX=nothing
t.install([unicode]) ➤ bind _ in builtin namespace to self.gettext() or
specified in this locale.
T_FMT usable as format for strftime() for time self.ugettext() upon unicode (default to false)
T_FMT_AMPM usable as format for strftime() for time in am/pm format Multilingual Support
GNU Translations
DAY_1…DAY_7 name of the nth day of the week - first day is sunday Standard module gettext for internationalization (I18N) and localization
(L10N) services - based on GNU gettext API + higher interface. See docs The GNUTranslations class (subclass of NullTranslations) is based
ABDAY_1… abbreviated name of the nth day of the week - first day is on GNU gettext and .mo files.
sunday for explanations about tools usage.
ABDAY_7 Messages ids and texts are coerced to unicode.
Protected _info attribute contains message translations.
7a 7b 7c
Translation for empty string return meta-data (see doc). filter1(fct,sequence)→ list: new list where fct(item) is true. None fct = bool Overriding Sequences Operations
Define methods gettext, lgettext, ugettext, ngettext, lngettext, test on items __getitem__(self,index2)→ value: item at index, called for self[index]
ungettext as in NullTranslations interface - same rules for return map1(fct,sequence,…)→ list: new list where ith item is fct(ith items of __setitem__1(self,index2,value)➤ set item at index to value, called for
values (str/unicode). Message translations are searched in catalog, sequence(s))
self[index]=value
then in fallback if defined, and if no translation is found, message itself reduce(fct,sequence[,initializer])→ value: fct applied cumulatively to
sequence items, f(f(…f(f(f(initializer,a),b),c,…) __delitem__1(self,index2,value)➤ remove item at index, called for
is returned (for n… methods, return singular forms if n=1 else plural
zip1(sequence,…)→ list: list of tuples, ith tuple contains ith items of each del self[index]
forms). 1
sequences Only for mutable sequences.
CONTAINERS 1
See Iteration Tools (p9) as replacement (avoid creating a new list).
2
Parameter index can be a slice [start,stop,step] - replace old
__getslice__, __setslice__, __delslice__.
Basic containers kind : Indexing Can also override arithmetic operations __add__ (concatenation ) and
-sequences (ordered collections) : list, tuple,str, any iterable,… Use index [i] and slice [i:j[:step]] syntax. Indexs zero-based. Negative __mul__ (repetition ), container operations and object operations.
-mappings (unordered key/value) : dict… indexs indexing from end. Default step is 1, can use negative steps.
-sets (unordered collections) : set, frozenset… Sub-sequences indexs between items. MAPPINGS (DICTIONARIES)
Operations on Containers l = [e1,e2,e3,…,en2,en1,en] Builtin type dict. Store key:value pairs.
For strings, items are chars. For mappings, items are keys. l[0]→ e1 l[0:n]→[e1,e2,e3,…,en2,en1,en] Declare a dictionary : { key:value [,…]} {}
item in container → bool: test item ∈ container1 l[1]→ e2 l[:]→[e1,e2,e3,…,en2,en1,en] dict()→ dict: empty dictionary (like {})
item not in container → bool: test item ∉ container1 l[2]→ en1 l[i:]→[ei+1,ei+2,ei+3,…,en1,en] dict(**kwargs)→ dict: from named parameters and their values
for var in container: … ➤ iterate var over items of container l[1]→ en l[:i]→[e1,e2,…,ei2,ei1,ei] dict(iterable)→ dict: from (key,value) by iterable
len(container) → int: count number of items in container2 items indexs dict(otherdict)→ dict: duplicated fro another one (first level)
max(container) → value: biggest item in container -n -n+1 -n+2 … -2 -1
min(container) → value: smallest item in container Operations on Mappings
0 1 2 … n-2 n-1 See Operations on Containers (p8) too, considering operations on keys.
sum(container) → value: sum of items (items must be number-compatible)
1 d[key]→ value for key1
For strings test if expr is a substring of sequence. e1 e2 e3 …item… en-1 en
2
Container must provide direct length method - no generator. d[key]=value ➤ set d[key] to value
8a 8b 8c
SETS a.fromlist(list) ➤ extend array from values in list izip(iterable[,…])→ iterator over tuple(items at same index from iterables)
a.tolist() → list: items in a list repeat(object[,count])→ iterator returning object over and over again, up to
Unordered collections of unique items. Frozen sets are immutable once
created. a.fromstring(s) ➤ extend array from values in binary buffer s (string) count times (default to infinite)
a.tostring() → str: items in binary representation starmap(function,iterable)→ iterator over function(*tuple item from iterable)
set([iterable]) → set: using values from iterable
a.fromunicode(s) ➤ extend 'u' array from data in unicode sting takewhile(predicatefct,iterable)→ iterator over items of iterable where
frozenset([iterable]) → frozenset: using values from iterable predicatefct(item) is true
a.tounicode() → unicode: convert 'u' array to unicode string
Operations on Sets 1 tee(iterable[,n]) n independent iterators from same iterable4, default n=2
If less items than needed, get available ones then raise EOFError. 1
See Operations on Containers (p8) too. Group of items is internally used - must save it as list if needed after
Old methods read and write replaced by fromfile and tofile.
s.issubset(others)→ bool: test s ⊂ others current iteration.
Queue 2
Stop at end of shorter iterable.
s.issuperset(others)→ bool: test others ⊂ s 3
Standard module collections provides queues management. Slice parameters cannot be negative.
s.add(item) ➤ adds item to set 4
deque([iterable])→ deque: initialized from iterable Don't use iterable out of tee created iterators.
s.remove(item) ➤ removes item from set1
s.clear()➤ removes all items from (not forzen) set q.append(x)➤ add x to right side of deque DATE & TIME
s.intersection(others)→ set: s ∩ others q.appendleft(x)➤ add x to left side of deque
q.clear()➤ remove all elements from deque
Module time
s & others → set: s ∩ others
q.extend(iterable)➤ extend right side of deque Standard module time defines common functions and data.
s.union(others) → set: s ∪ others
s | others → set: s ∪ others q.extendleft(iterable)➤ extend left side of the deque Date & Time Data
s.difference(others) → set: [x / x∈s and x∉others] q.pop() → item: pop and return item from dequeue right side • float_time = float containing seconds from 'epoch' (january 1 1970
s others → set: [x / x∈s and x∉others] q.popleft() → item: pop and return item from dequeue left side on Unix - see gmtime(0)), with sub-second precision in decimal part.
s.symmetric_difference(others)→ set: [x / x∈s xor x ∈others] q.rotate(n) ➤ rotate deque from n steps, to right if n>0, to left if n<0 • tuple_time = tuple containing 9 int (see table).
Can also use standard operations on sequences : len(q), • struct_time = tuple/object with int attributes (see table).
s ^ others → set: [x / x∈s xor x ∈others]
reversed(q), copy.copy(q), copy.deepcopy(q), item in q, q[1], # attribute value # attribute value
s.copy()→ shallow copy of set
and serialization via pickling protocol. 0 tm_year int 5 tm_sec 0…61
s.update(iterable)➤ adds all values from iterable to set
1 tm_mon 1…12 6 tm_wday 0…6 (monday=0)
1
Raise KeyError if object not in set. Priority Queues
Results set have same type as s object (set/frozenset). Standard module heapq. Structure a list as a priority queue. 2 tm_mday 1…31 7 tm_yday 0…366
heapify(x) ➤ x list transformed into heap 3 tm_hour 0…23 8 tm_isdst 0 (no)
OTHER CONTAINERS STRUCTURES, ALGORITHMS 1 (yes)
heappush(heap,item)➤ push item onto heap 4 tm_min 0…59
Generally containers follow Python idioms, you can use : len(cont), 1 (unknown)
heappop(heap)→ item: pop and return smallest item from the heap
cont[i], for item in cont:… heapreplace(heap,newitem)→ item: pop and return smallest item from the • float_delay = float containing seconds, with sub-second precision.
Array heap, push new item DST is local time, UTC is universal (GMT) time.
Standard module array provides efficient array of basic types. It uses nlargest(n,iterable)→ list: n largest from iterable accept2dyear → [rw] bool: accept two-digit year values (default true),
compact storage for elements of same type. nsmallest(n,iterable)→ list: n smallest items from iterable modifiable via environment var PYTHONY2K
Sorted List altzone → int: offset (pos/neg) in seconds of DST relatively to UTC, in
Type Codes seconds, use only if daylight is true
n tc C type py type n tc C py type Standard module bisect maintains lists sorted (via basic bisection algo). daylight → int: ≠0 if a DST timezone is defined
1 'b' signed char int 1 'B' unsigned char int bisect_left(list,item[,lo[,hi]])→ int: index to insert item at leftmost sorted timezone → int: offset (pos/neg) in seconds of local (non DST) timezone
position1
1 'c' char str 2 'u' unicode char unicode tzname → (str{2}): names of local timezone (non-DST, DST)
bisect_right(list,item[,lo[,hi]])→ int: index to insert item at rightmost
2 'h' signed short int 2 'H' unsigned short int Functions
sorted position1
2 'i' signed int int 2 'I' unsigned int long bisect(…) ➤ Alias for bisect_right(…) asctime([t=2])→ str: build local time string from t (tuple_time or struct_time)
4 'l' signed long int 4 'L' unsigned long long insort_left(list,item[,lo[,hi]]) ➤ insert item at leftmost sorted position1 clock()→ float: processor time in seconds, for accurate relative time
4 'f' float float 8 'd' double float insort_right(list,item[,lo[,hi]]) ➤ insert item at rightmost sorted position1 measurement
n=size in bytes, tc=char typecode to use insort(…) ➤ Alias for insort_right(…) ctime([secs=2])→ str: build local time string from float_time second
1
Functions With list previously sorted. gmtime([secs=2])→ struct_time: convert float_time to UTC struct_time
Iteration Tools localtime([secs=2])→ struct_time: convert float_time to DST struct_time
array(tc,[iterable]) → array: with typecode tc, initialized from iterable
Standard module itertools provides some practical iterators. mktime(t)→ float_time: convert DST t (tuple_time or struct_time) to float_time -
a.typecode → str: typecode of the array
chain(iterable[,…])→ iterator over items of several iterables may raise OverflowError or ValueError
a.itemsize → int: bytes size of one array data
count([start])→ iterator over integers from start (default 0) sleep(secs) ➤ execution suspended during secs (float_delay) times, maybe
a.append(expr) ➤ append item expr to end of array less (signal catching), may be more (process/threads scheduling)
a.extend(array) ➤ append items from another array cycle(iterable)→ iterator cycling over iterable items
strftime(format[,t=2]) → str: build time string from t (tuple_time or
a.count(expr) → int: number of expr items in array dropwhile(predicatefct,iterable)→ iterator over items of iterable where
struct_time) using format string (table infra) - may raise ValueError
predicatefct(item) is false
a.index(expr) → int: first index of expr item strptime(string[,format]) → struct_time: parse string using time format1 -
groupby(iterable[,keyfct])→ iterator over (key value,group1 of items where
a.insert(index,expr) ➤ expr item inserted at index keyfct(item)=key value), default keyfct is identity may raise ValueError
a.remove(expr)➤ remove first expr item from array ifilter(predicate,iterable)→ iterator over items of iterable where time() → float_time: current UTC time
a.pop([index]) → value: return and remove item at index (default -1) predicatefct(item) is true - None predicate filter items being true tzset() ➤ resets time conversion rules accordingly to environnment variable
a.reverse() ➤ items in array are reversed ifilterfalse(predicate,iterable)→ iterator over items of iterable where TZ - unix only, see docs
a.buffer_info() → (int{2}): current storage infos (address,items count) predicatefct(item) is false - None predicate filter items being false 1
Default format "%a %b %d %H:%M:%S %Y". Missing values default to
imap(function,iterable[,…])→ iterator over function(items at same index from (1900, 1, 1, 0, 0, 0, 0, 1, -1)
a.byteswap() ➤ swap bytes of array items
iterables2), None function return tuples items 2
Param secs default to current time, param t default to local current
a.fromfile(f,n) ➤ append n items read from real binary file f1
islice(iterable,[start,]stop[,step])→ iterator over items at slice3 indexs from time.
a.tofile(f) ➤ write all items to real binary file f iterable, None stop goes up to end
9a 9b 9c
Time format strings f.readlines() → [string]: list of all lines read from file, end of lines removed O_SEQUENTIAL → xxxxxx (Windows)
%a Abbreviated weekday name1. %A Full weekday name1. f.seek(offset[,whence=0])➤ modify current position in file - whence : 0 from O_TEXT → xxxxxx (Windows)
%b Abbreviated month name1. %B Full month name1. start, 1 from current, 2 from end Pipes
%c Appropriate date and time %d Month day [01,31]. f.tell() → int: current position in file For standard process redirection using pipes, see also Simple External
representation1. f.write(string)➤ data written to file Process Control (p14).
%H Hour [00,23]. %I Hour [01,12].
f.writelines(listofstrings) ➤ data written to file (no end of line added) os.pipe() → ((int{2}){2}): create pair (fdmaster,fdslav) of fd (read,write) for
%j Year day [001,366]. %m Month [01,12].
for line in f :… ➤ iterate over lines of f a pipe
%M Minute [00,59]. %p 1
AM or PM . Old method xreadlines replaced by iteration on file object. os.mkfifo(path[,mode=0666])➤ create named pipe path - mode masked out
%S Second [00,61]. %U Year week [00,53] (Sunday based). with umask - don't open it (Unix)
For optimized direct access to random lines in text files, see module
Use os functions on file descriptors.
%w Week day [0,6] (0=Sunday). %W Year week [00,53] (Monday based). linecache.
%x Appropriate date representation1. %X Appropriate time representation1. Attributes In-memory Files
%y Year [00,99]. %Y Year (with century). Memory Buffer Files
f.closed → bool: indicator file has been closed
%Z Time zone name (no characters if %% Literal % char.
Use standard modules StringIO and cStringIO to build file-like objects
no time zone exists). f.encoding → str/None: file content encoding
1 f.name → str: name of the file storing data in memory.
Locale language representation. f = StringIO.StringIO()
f.newlines → str/tuple of str/None: encountered newlines chars
f.softspace→ bool: indicator to use soft space with print in file
Build a file-like in memory.
Module datetime f.write(string)➤ data written to file
Standard module datetime has tools for date/time arithmetics, data Low-level Files
f.…other file writing methods…
extraction and manipulation. Base low-level functions are in standard module os. f.getValue() → str: current data written to file
Defines class : timedelta, time, date, datetime, [tzinfo]. Careful of clash with builtins with os.open name. f.close()➤ file no longer usable, free buffer
Module timeit open(file,flags[,mode=0777])→ int: fd, open file - see flags infra - mode cStringIO is a compiled (more efficient) version of StringIO for writing.
Standard module timeit has functions to measure processing time of masked out with umask Optional argument allows to build memory files to read from too.
code. It can be used in scripts (see docs), or directly in command line : fdopen(fd[,mode[,bufsize]]) → file: build a file connected to fd - mode and f = cStringIO.StringIO([string])
python -mtimeit [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement […]] bufsize as for builtin open()+ mode must start with r or w or a f.read([size]) → str: block of data read from 'file' (string)
-n N / --number=N execute statement N times dup(fd)→ int: fd, duplicate file descriptor f.…other file reading methods…
-r N / --repeat=N repeat timer N times (default 3)
dup2(fd,fd2)→ int: fd, duplicate file descriptor into fd2, previously closing fd2 Memory Mapped Files (OS level)
-s S / --setup=S executed S once initially (default pass) if necessary
-t / --time use time.time() (default except Windows) close(fd)➤ close file descriptor Standard module mmap manage memory-mapped files, usable as file-like
-c / --clock use time.clock() (default on Windows) objects and as mutable string-like objects.
read(fd,n)→ str: read as most n bytes from fd file - return empty string if end
-v / --verbose print raw timing results - may repeat option of file reached
To build a memory map :
-h / --help print help and exit mm = mmap.mmap(fileno,length[,tagname[,access]]) [windows]
write(fd,str)→ int: write str to fd file - return number of bytes actually
Other Modules written mm = mmap.mmap(fileno,length[,flags[,prot[,access]]]) [unix]
Standard module calendar has functions to build calendars. lseek(fd,pos,how)➤ set file descriptor position - how : 0 from start, 1 from Use an os file descriptor (from os.open() or from file-object's
See also third party module mxDateTime.
current, 2 from end fileno()) for a file opened for update.
fdatasync(fd)➤ flush file data to disk - don't force update metadata (Unix) Length specify amount of bytes to map. On windows, file may be
FILES fsync(fd)➤ force low level OS buffers to be written extended to that length if it is shorter, it can't be empty, and 0
ftruncate(fd,length)➤ truncate file descriptor to at most length (Unix) correspond to maximum length for the file.
Normal file operations use Python file objects (or file-like objects with
Access (keyword param) : ACCESS_READ (readonly), ACCESS_WRITE
same interface). Some functions directly manipulate files path names
(strings). Functions mapping low level OS handlers (mainly those in (write-through, default on Windows), or ACCESS_COPY (copy-on-
Open Flags write).
standard os module) use numeric file descriptors (fd also known as
Constants defined in os module, use bit-wise OR (x|y|z) to mix them. On Windows, tagname allow to identify different mappings against
fileno).
O_RDONLY → read only same file (default to None).
Raw data use str type (can contain any data byte values, including 0).
O_WRONLY → write only On Unix, flags : MAP_PRIVATE (copy-on-write private to process) or
File Objects MAP_SHARED (default). And prot (memory protection mask) :
O_RDWR → read/write
Standard file type is builtin file. It defines the Python file protocol. PROT_READ or PROT_WRITE, default is PROT_READ|PROT_WRITE. If use
O_APPEND → append each write to end
Create a file : file(filename[,mode='r'[,bufsize]]) → file object prot+flags params, don't use access param.
Mode flags (combinable) : 'r' read, 'w' write new, 'a' write append, O_CREAT → create new file (remove existing)
mm.close()➤ mmap file no longer usable
'+' update, 'b' binary1, 'U' universal newline2. O_EXCL → with O_CREAT, fail if file exist (Unix)
mm.find(string[,start=0])→ int: offset / 1
Buffer size : 0 unbuffered, 1 line buffered, >1 around that size. O_TRUNC → reset existing file to zero size
mm.flush([offset,size])➤ write changes to disk
Open() is an alias for file() O_DSYNC → xxxxxx (Unix)
1
Default text mode tries to interpret newline sequences in the file. mm.move(dest,src,count)➤ copy data in file
O_RSYNC → xxxxxx (Unix)
2
Automatically choose newline sequence in CR or LF or CR+LF adapted mm.read([size])→ str: block of data read from mmap file1
O_SYNC → return from IO when data are physically written (Unix)
from file/to platform. mm.read_byte()→ str: next one byte from mmap file1
O_NDELAY → return immediatly (don't block caller during IO) (Unix)
Methods and Functions mm.readline()→ str: next line read from file, ?end of line removed?1
O_NONBLOCK → same as O_NDELAY (Unix)
mm.resize(newsize)➤ writable mmap file resizer
f.close()➤ file flushed and no longer usable O_NOCTTY → terminal device file can't become process tty (Unix)
mm.seek(offset[,whence=0])➤ modify current position in mmap file -
f.fileno() → int: low level file descriptor (fd) O_BINARY → don't process end of lines (cf+lf from/to cr) (Windows)
whence : 0 from start, 1 from current, 2 from end
f.flush()➤ buffers written to file on disk O_NOINHERIT → xxxxxx (Windows)
mm.size()→ int: length of the real os file
f.isatty() → bool: indicator file is a terminal O_SHORT_LIVED → xxxxxx (Windows)
mm.tell() → int: current position in mmap file
f.read([size]) → str: block of data read from file O_TEMPORARY → xxxxxx (Windows)
mm.write(string)➤ data written to mmapfile1
f.readline() → str: next line read from file, end of line removed O_RANDOM → xxxxxx (Windows)
mm.write_byte(byte)➤ str of one char (byte) data written to mmap file1
os.W_OK → access mode to test path writable 8 st_mtime time of most recent content modification Temporary Files
os.X_OK → access mode to test path executable 9 st_ctime time of most recent metadata change on Unix, time of Use standard tempfile module. It defines several functions to make life
creation on Windows
os.chmod(path,mode)➤ change mode of path - mode use stat.S_* easier and more secure.
constants st_blocks number of blocks allocated for file (Unix)
TemporaryFile([mode='w+b'[,bufsize=-1[,suffix[,prefix[,dir]]]]])
os.chown(path, uid, gid)➤ change path owner and group (Unix) st_blksize filesystem blocksize (Unix) → file/file-like: temp file - removed on close - not necessary visible in file-
os.lchown(path, uid, gid)➤ change path owner and group - don't follow st_rdev type of device if an inode device (Unix) system - dir and prefix as for mkstemp
symlinks(Unix)
st_rsize size of resource fork, in bytes(MacOS) NamedTemporaryFile([mode='w+b'[,bufsize=-1[,suffix[,prefix[,dir]]]]])
os.fstat(fd)→ int: status for file descriptor
st_creator file creator code (MacOS) → file/file-like: like TemporaryFile - file visible in file-system
os.fstatvfs(fd)→ statvfs_result: informations about file system
mkstemp([suffix[,prefix[,dir[,text]]]])→ (int,str): (fd,path) of new
containing file descriptor (Unix) st_type file type code (MacOS)
temporaty file - no race condition - only creator can read/write - no executable
os.stat(path)→ stat structure object: file system informations (Unix) bit - not automatically deleted - binary mode unless text specified
statvfs_result is returned by fstatvfsand statvfs functions, usable
os.lstat(path)→ stat structure object: file system informations (Unix) - dont mkdtemp([suffix[,prefix[,dir]]])→ str: path of new temporary directory created
follow symlinks as a tuple (use statvfs variable indexs) and as an object with
- no race condition - only creator can read/write/search - not automatically
os.stat_float_times([newvalue])→ bool: test/set stat function time attributes : deleted>
# attribute index var usage
stamps data type - avoid setting new value gettempdir() → str: default directory for temporary files
0 f_bsize F_BSIZE preferred file system block size
os.statvfs(path)→ statvfs_result: informations about file system gettempprefix() → str: default filename prefix for temporary files
containing path (Unix) 1 f_frsize F_FRSIZE fundamental file system block size
os.utime(path,times)➤ set access and modification times of file path - 2 total number of blocks in the filesystem
Other functions in tempfile and os modules are kept for code
f_blocks F_BLOCKS
times=(atime,mtime) (numbers) - times=None use current time compatibility, but are considered not enough secured. Also tempdir
os.fpathconf(fd,name) → str / int: system configuration information
3 f_bfree F_BFREE total number of free blocks
and template data in tempfile - which should not be used directly.
about file referenced by file descriptor - see platform documentation and 4 f_bavail F_BAVAIL free blocks available to non-super user
Path Manipulations
pathconf_names variable - name str or int (Unix) 5 f_files F_FILES total number of file nodes
Path manipulation functions are in standard os.path module.
os.pathconf(path,name)→ str / int: system configuration information 6 total number of free file nodes
f_ffree F_FFREE supports_unicode_filenames → <bool: unicode usable for file names>
about file referenced by file descriptor - see platform documentation and
pathconf_names variable - name str or int (Unix)
7 f_favail F_FAVAIL free nodes available to non-super user
abspath(path)→ str: normalized absolutized pathname
8 f_flag F_FLAG flags - see host statvfs() man page basename(path)→ str: file name part of path
os.pathconf_names → dict: name → index - names accepted by pathconf
and fpathconf → corresponding index on host (Unix) 9 f_namemax F_NAMEMAX maximum file name length commonprefix(pathlist) → str: longest common path prefix (char-by-char)
os.path.exists(path)→ bool: test existing path - no broken symlinks Stat Constants dirname(path)→ str: directory name of pathname
os.path.lexists(path)→ bool: test existing path - allow broken symlinks join(path[,…])→ str: concatenate path components
Defined in standard stat module.
os.path.getatime(path)→ float_time: last access time of path normcase(path)→ str: normalize path case for platform (see doc)
S_ISUID → xxxxx
os.path.getmtime(path)→ float_time: last modification time of path normpath(path)→ str: normalize path (// /./ /../), on windows /→ \
S_ISGID → xxxxx
os.path.getctime(path)→ float_time: creation time (windows) or last realpath(path)→ str: canonical path (remove symlinks) (unix)
S_ENFMT → xxxxx
modification time (unix) of path split(path)→ (str{2}): split into (head, last pathname component)
os.path.getsize(path)→ int: bytes size of path file S_ISVTX → xxxxx
splitdrive(path)→ (str{2}): split into (drive, tail)
os.path.isabs(path)→ bool: test absolute S_IREAD → 00400 user can read
splitext(path)→ (str{2}): split into (root, ext)
os.path.isfile(path)→ bool: test regular file (follow symlinks) S_IWRITE → 00200 user can write
Host Specific Path Data
os.path.isdir(path)→ bool: test existing directory (follow symlinks) S_IEXEC → 00100 user can execute
S_IRWXU → 00700 user can read+write+execute sys.getfilesystemencoding() → <name of encoding used by system for
os.path.islink(path)→ bool: test symlink
filenames>
os.path.ismount(path)→ bool: test mount point S_IRUSR → 00400 user can read
Following data are in os and in os.path.
os.path.samefile(path1,path2)→ bool: test refer to same real file S_IWUSR → 00200 user can write
curdir → str: string used to refer to current directory
(unix,macos) S_IXUSR → 00100 user can execute
os.path.sameopenfile(f1,f2)→ bool: test opened files refer to same real pardir → str: string used to refer to parent directory
S_IRWXG → 00070 group can read+write+execute
file (unix,macos) sep → str: char used to separate pathname components
S_IRGRP → 00040 group can read
os.path.samestat(stat1,stat2)→ bool: test stat tuples refer to same file altsep → str: alternative char used to separate pathname components
S_IWGRP → 00020 group can write
(unix,macos) extsep → str: char used to separate base filename from extension
shutil.copymode(srcpath,dstpath)➤ copy normal file permission bits S_IXGRP → 00010 group can execute
pathsep → str: conventional char to separate different paths
shutil.copystat(srcpath,dstpath)➤ copy normal file permission bits and S_IRWXO → 00007 everybody can read+write+execute
S_IROTH → 00004 everybody can read Directories
last access and modification times
S_IWOTH → 00002 everybody can write os.listdir(path)→ [str]/[unicode]: list names in path directory -
Stat Structures
S_IXOTH → 00001 everybody can execute without . and .. - arbitrary order - path string type → item strings type
stat_result is returned by stat and lstat functions, usable as a tuple os.mkdir(path[,mode=0777])➤ create directory path - mode masked out
Use subprocess module when possible (cleaner, simpler interface, see stdin/stdout return values are inverted). 1
Feature names in xml.sax.handler as feature_xxx.
docs for examples). See also external module pexpect. popen2(cmd[,bufsize[,mode]])→ (file{2}): (stdout,stdin): execute cmd as 2
Property names in xml.sax.handler as property_xxx.
sub-process
Advanced External Process Control popen3(cmd[,bufsize[,mode]])→ (file{3}): (stdout,stdin,stderr): execute cmd InputSource Interface
See following functions from os module. as sub-process Provide source of data for parser.
execl(path,[arg[,…]]) popen4(cmd[,bufsize[,mode]])→ (file{2}): stdout_stderr,stdin): execute cmd isrc.setPublicId(id) ➤ set public identifier
execle(path,[arg[,…]],env) as sub-process isrc.getPublicId() → unicode: public identifier
Where bufsize is buffer size for I/O pipes, and mode is 'b' (binary
execlp(file,[arg[,…]]) isrc.setSystemId(id) ➤ set system identifier
streams) or 't' (text streams, default). Param cmd is a string passed
execlpe(file,[arg[,…]],env) isrc.getSystemId() → unicode: system identifier
to os.system - on Unix it can be a sequence of strings passed
execv(path,args) isrc.setEncoding(encoding) ➤ set encoding - must be a string acceptable
directly to the program without shell intervention.
execve(path,args,env) for an XML encoding declaration - ignored if InputSource contains character
execvp(file,args) On Unix, popen2 module also defines Popen3 class (used in popen2 and stream
popen3 functions) and Popen4 class (used in popen4 function) : isrc.getEncoding() → str/None (if unknown)
execvpe(file,args,env)
Popen3(cmd[,capturestderr[,bufsize]])→ Popen3: cmd=shell command, isrc.setByteStream(bytefile) ➤ set input byte stream - ignored if
With exec… new program replace current process (fct don't return). 'p' InputSource contains character stream
captudestderr=bool (default False)
versions use PATH to locate executable file. 'e' versions use a dict env to isrc.getByteStream() → byte stream
Popen4(cmd[,bufsize])→ Popen4
setup new program environment. 'l' versions use a positioned arg, 'v' Popen3 and Popen4 objects have following attributes : isrc.setCharacterStream(charfile) ➤ set character (Unicode) stream
versions use list of variable args. p.poll()→ int: child return code or -1 if child not terminated isrc.getCharacterStream() → character stream
spawnl(mode,path,[arg[,…]])→ int
p.wait()→ int: child return code Locator Interface
spawnle(mode,path,[arg[,…]],env) → int
p.fromchild → file: output from child (stdout and stderr for Popen4) Instances of Locator provide these methods:
spawnlp(mode,file,[arg[,…]])→ int
p.tochild → file: input to child loc.getColumnNumber() → int: column number where current event ends
spawnlpe(mode,file,[arg[,…]],env) → int
p.childerr → file: error output from child if requested else None (None for loc.getLineNumber() → int: line number where current event ends
spawnv(mode,path,args) → int Popen4) loc.getPublicId() → str: public identifier of current event
spawnve(mode,path,args,env) → int p.pid → int: child process pid
loc.getSystemId() → str: system identifier of current event
spawnvp(mode,file,args) → int
See also module commands (Unix). Attributes Interface
spawnvpe(mode,file,args,env) → int
With spawn… new process is created. 'lpev' versions like for exec…. XML PROCESSING Also implement parts mapping protocol (copy(), get(), has_key(),
If mode is P_NOWAIT or P_NOWAIT0, return child pid (Unix) or process Several modules to process XML are available. Some with standard SAX items(), keys(), and values()).
handle (Windows). If mode is P_WAIT, wait child termination and return and DOM interfaces, others with more Pythonic interfaces. ai.getLength() → int: number of attributes
its exit code (>0) or its killing signal (<0). On Windows mode can be, See also third party PyXML extension package. ai.getNames() → [unicode]: names of attributes
P_DETACH (same as P_NOWAIT but new process detached from calling ai.getType(name)→ type of attribute name - normally 'CDATA'
SAX - Event-driven
process console) or P_OVERLAY (current process is replaced). ai.getValue(name)→ unicode: value of attribute name
Base functions in xml.sax module.
fork()→ pid: fork a child process, return 0 in child, child pid in parent (Unix)
make_parser([parser_list]) → XMLReader: built from first parser available AttributesNS Interface
forkpty()→ (int{2}): (pid,fd): fork using new pseudo-terminal for child - pid
is 0 in child, child pid in parent - fd pseudo-terminal master end (Unix) parse(filename_or_stream,content_handler[,error_handler]) ➤ parse Also implement Attributes interface.
document using first parser available ansi.getValueByQName(name)→ unicode: value of attribute qualified name
startfile(path) ➤ open file path as if double-clicked in explorer (Windows)
parseString(string,content_handler[,error_handler]) ➤ parse string using ansi.getNameByQName(name)→ (unicode{2}): (namespace, localname) for
system(cmd)→ value: execute string cmd in subshell - generally return first parser available
(pid/status) (Unix) or status (Windows) qualified name
wait()→ (int{2}): (pid,status) wait completion of a child process (Unix) - XMLReader Interface ansi.getQNameByName(namepair)→ unicode: qualified name for (namespace,
status=0xZZTT where ZZ=exit code, TT=signal num Defined in xml.sax.xmlreader. localname)
waitpid(pid,options)→ (int{2}): (pid,status) (Unix): ansi.getQNames()→ [unicode]: qualified names of all attributes
p = xml.sax.make_parser() → XMLReader object
pid>0 wait for specific process, p.parse(source) ➤ completly parse source - source is filename or URL or file- ContentHandler Interface
pid=0 wait for any child in process group,
like or InputSource- input byte streams (not character streams) Defined in xml.sax.handler. Its methods are handlers called when
pid=-1 wait for any child of current process,
pid<-1 wait for any process in process group -pid p.getContentHandler() → ContentHandler: current one parser find XML structures.
option in WNOHANG, WCONTINUED, WUNTRACED p.setContentHandler(handler) ➤ set current content handler ch = MyContentHandler() → ContentHandler subclass object
status=0xZZTT where ZZ=exit code, TT=signal num p.getDTDHandler() → DTDHandler: current one ch.setDocumentLocator(locator) ➤ set locator for origin of document events
waitpid(pid,options)→ (int{2}): (pid,status) (Windows): pid is any process p.setDTDHandler(handler) ➤ set current DTD handler ch.startDocument() ➤ beginning of document
handle (>0) - option ignored - status=0xZZ00 where ZZ=exit code p.getEntityResolver() → EntityResolver: current one ch.endDocument() ➤ beginning of document
Status informations extraction p.setEntityResolver(handler) ➤ set current entity resolver ch.startPrefixMapping(prefix,uri) ➤ begin of a prefix-URI namespace
WCOREDUMP(status)→ bool: test process generated core-dump (Unix) p.getErrorHandler() → ErrorHandler: current one mapping - see doc
p.setErrorHandler(handler) ➤ set current error handler ch.endPrefixMapping(prefix) ➤ end of a prefix-URI namespace mapping
WIFCONTINUED(status)→ bool: test process continued from a job control stop
(Unix) p.setLocale(locale) ➤ set locale for errors and warnings ch.startElement(name,attrs) ➤ start of an element - non-namespace mode
WIFSTOPPED(status)→ bool: test process stopped (Unix) p.getFeature(featurename) → current settings for feature1 - attrs has an Attributes interface (may be reused - copy data)
HierarchyRequestErr threadsafety → int: level of thread safety cu.execute(oper[,params]) ➤prepare and execute DB request - params1 is a
HIERARCHY_REQUEST_ERR
# share module share connections share cursors sequence or a mapping (see module paramstyle variable)
INDEX_SIZE_ERR IndexSizeErr
0 no no no cu.executemany(oper,params_seq) ➤like execute, with a sequence of
INUSE_ATTRIBUTE_ERR InuseAttributeErr
1 yes no no params (for multiple values)
INVALID_ACCESS_ERR InvalidAccessErr 2 yes yes no cu.fetchone()→ (column_value,…) / None: next row of query result, None
INVALID_CHARACTER_ERR InvalidCharacterErr 3 yes yes yes when no more data available
INVALID_MODIFICATION_ERR InvalidModificationErr cu.fetchmany([size])→ [(column_value)]: next set of rows of query result,
paramstyle → str: parameter marker for requests
InvalidStateErr empty list when no more data available - size default to cu.arraysize
INVALID_STATE_ERR
17a 17b 17c
cu.fetchall()→ [(column_value)]: all remaining rows of query result,
empty list when no more data available
cu.next()→ (column_value) : (optional) next row of query result, raises
StopIteration when no more data available
cu.nextset() → True/None: (optional) discards results up to next available
set
cu.scroll(value[,mode='relative']) ➤ (optional) - scroll cursor in current
result set - mode in 'relative', 'absolute'.
cu.setinputsizes(sizes) ➤ predefine memory areas for executeXXX
operations parameters - sizes=[param_size,…] - param_size=Type Object or int
(max length of a string param) - param_size=None for no predefinition
cu.setoutputsize(size[,column]) ➤ set column buffer size for fetches of
large columns (e.g. LONGs, BLOBs, etc.) by executeXXX - column is index in
result - all columns if column not specified
cu.__iter__() → Cursor: (optional) object itself
1
Method __getitem__ is used to get values in params, using position
or name. Can use tuple or dict… or your own class objects with its
__getitem__.
If next and __iter__ are defined, cursors are iterable.
DB types Constructors
Date(year,month,day)→ object to hold a date value
Time(hour,minute,second)→ object to hold a time value
Timestamp(year,month,day,hour,minute,second)→ object to hold a time
stamp value
DateFromTicks(ticks)→ object to hold a date value from a given ticks value
TimeFromTicks(ticks)→ object to hold a time value from a given ticks value
TimestampFromTicks(ticks)→ object to hold a time stamp value from a given
ticks value
Binary(string)→ object to hold a long binary string value
SQL NULL values represented by Python None.
DB types Typecodes
STRING → string-based column (CHAR)
BINARY → long binary column (LONG, RAW, BLOBs)
NUMBER → numeric column
DATETIME → date/time column
ROWID → row ID column (CHAR)
TOOLS
Providen with the snake :
Code bench : see module timeit (p10).
A must have : pychecker,.
Take a look : pylint, psyco, pyrex, pycount, trace2html.