• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content




Even though this is a trivial example, the advantages of Python stand out....

Even though this is a trivial example, the advantages of Python stand out.
Yorktown’s Computer Science I course has no prerequisites, so many of the
students seeing this example are looking at their first program. Some of them
are undoubtedly a little nervous, having heard that computer programming is
difficult to learn. The C++ version has always forced me to choose between
two unsatisfying options: either to explain the #include, void main(), {, and
} statements and risk confusing or intimidating some of the students right at
the start, or to tell them, “Just don’t worry about all of that stuff now; we will
talk about it later,” and risk the same thing. The educational objectives at
this point in the course are to introduce students to the idea of a programming
statement and to get them to write their first program, thereby introducing
them to the programming environment. The Python program has exactly what
is needed to do these things, and nothing more.
Comparing the explanatory text of the program in each version of the book
further illustrates what this means to the beginning student. There are thirteen
paragraphs of explanation of “Hello, world!” in the C++ version; in the Python
version, there are only two. More importantly, the missing eleven paragraphs
do not deal with the “big ideas” in computer programming but with the minutia
of C++ syntax. I found this same thing happening throughout the book.
Whole paragraphs simply disappear from the Python version of the text because
Python’s much clearer syntax renders them unnecessary.
Using a very high-level language like Python allows a teacher to postpone talking
about low-level details of the machine until students have the background that
they need to better make sense of the details. It thus creates the ability to put
“first things first” pedagogically. One of the best examples of this is the way in
which Python handles variables. In C++ a variable is a name for a place that
holds a thing. Variables have to be declared with types at least in part because
the size of the place to which they refer needs to be predetermined. Thus, the
idea of a variable is bound up with the hardware of the machine. The powerful
and fundamental concept of a variable is already difficult enough for beginning
students (in both computer science and algebra). Bytes and addresses do not
help the matter. In Python a variable is a name that refers to a thing. This
is a far more intuitive concept for beginning students and is much closer to the
meaning of “variable” that they learned in their math courses. I had much less
difficulty teaching variables this year than I did in the past, and I spent less
time helping students with problems using them.



Total Views
Views on SlideShare
Embed Views



7 Embeds 23

http://www.slideshare.net 14
http://www.techgig.com 2
http://www.linkedin.com 2
https://www.linkedin.com 2
http://localhost 1
http://empirical.trilan 1
http://empirical.local 1



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Python Python Presentation Transcript

    • Types 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> frozenset global int list long object set slice str tuple  Notations : type unicode xrange V0.56 — 2006­5­31 — PQRC at http://laurent.pointal.org/python/pqrc f(…)→ return value f(…)®return nothing (procedure) 1 basestring is virtual superclass of str and unicode. [x] for a list of x data, (x) for a tuple of x data, may have x{n}→ n Long Python Quick Reference at http://rgruet.free.fr/ This doc uses string when unicode and str can apply. times x data. Original Python reference at http://www.python.org/doc Functions ENVIRONMENT VARIABLES License : Creative Commons [by nc sa]. Constructor functions of builtin types are directly accessible in builtins. CARDS CONTENT PYTHONCASEOK no case distinction in module→file mapping __import__ abs apply1 callable chr classmethod cmp coerce  1 1 PYTHONDEBUG = -d command-line option compile delattr dir divmod enumerate eval execfile filter  Environment Variables............................1 Copying Containers............................8 PYTHONHOME Modify standard Python libs prefix and exec prefix Command-line Options............................1 Overriding Containers Operations......8 getattr globals hasattr hash  help hex id input intern2  Files Extensions.......................................1 Sequences...............................................8 locations. Use <prefix>[:<execprefix>]. isinstance issubclass iter len locals map max min oct open  Language Keywords................................1 Lists & Tuples.................................... ..8 1 PYTHONINSPECT = -i command-line option ord pow property range raw_input reduce reload repr  Builtins....................................................1 Operations on Sequences...................8 1 PYTHONOPTIMIZE = -O command-line option Types...................................................1 Indexing...................................... ........8 reversed round set setattr sorted staticmethod sum super  PYTHONPATH Directories where Python search when importing Functions............................................1 Operations on mutable sequences.....8 unichr vars zip modules/packages. Separator : (posix) or ; Statements..............................................1 Overriding Sequences Operations......8 1 Blocks.................................................1 Mappings (dictionaries)...........................8 Use f(*args,**kargs) in place of apply(f,args,kargs). (windows). Under windows use registry Assignment Shortcuts.........................1 Operations on Mappings.....................8 2 Don't use intern. HKLMSofware…. Console & Interactive Input/Output.........2 Overriding Mapping Operations..........8 PYTHONSTARTUP File to load at begining of interactive sessions. STATEMENTS Objects, Names and Namespaces...........2 Other Mappings..................................8 1 PYTHONUNBUFFERED = -u command-line option Identifiers............................................2 Sets.........................................................9 1 One statement per line1. Can continue on next line if an expression or a PYTHONVERBOSE = -v command-line option Objects and Names, Reference Operations on Sets.............................9 1 If set to non-empty value. string is not finished ( ( [ { quot;quot;quot; ''' not closed), or with a at end of Counting.............................................2 Other Containers Structures, Algorithms 9 Mutable/Immutable Objects...............2 Array............................................. .....9 . line. COMMAND-LINE OPTIONS Namespaces.......................................2 Queue.................................................9 Char # start comments up to end of line. Constants, Enumerations...................2 Priority Queues...................................9 python [-dEhiOQStuUvVWx] [-c cmd | -m mod | file | -] [args] Flow Control............................................2 Sorted List...........................................9 pass Null statement. -d Output debugging infos from parser. Condition............................................2 Iteration Tools.....................................9 Assertion check expression true. assert expr[,message] -E Ignore environment variables. Loop....................................................2 Date & Time............................................9 del name[,…] Remove name → object binding. Functions/methods exit......................2 Module time........................................9 -h Print help and exit. Exceptions..........................................2 Module datetime...............................10 Write expr to sys.stdout2. print [>>obj,][expr[,…][,] -i Force interactive mode with prompt (even after script Iterable Protocol.................................2 Module timeit....................................10 execution). exec expr [in globals [, locals]] Execute expr in namespaces. Interpretation / Execution.......................2 Other Modules..................................10 Optimize generated bytecode, remove assert checks. -O fct([expr[,…]],[name=expr[,…]] Call any callable object fct with given Functions Definitions & Usage.................2 Files.......................................................10 -OO As -O and remove documentation strings. arguments (see Functions Definitions & Parameters / Return value..................2 File Objects.......................................10 [,*args][,**kwargs]) 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. name[,…] = expr -S Don't import site.py definitions module. Calling Functions.................................2 In-memory Files................................10 -t Warn inconsistent tab/space usage (-tt exit with error). Functions Control................................3 Files Informations..............................11 Multiple statements on same line using ; separator - avoid if not 1 -u Use unbuffered binary output for stdout and stderr. Decorators..........................................3 Terminal Operations..........................11 necessary. -U Force use of unicode literals for strings. Types/Classes & Objects..........................3 Temporary Files.................................11 2 Write to any specified object following file interface (write method). -v Trace imports. Class Definition...................................3 Path Manipulations...........................11 Write space between expressions, line-return at end of line except with Object Creation...................................3 Directories........................................11 -V Print version number and exit. a final ,. Classes & Objects Relations...............3 Special Files......................................12 -W arg Emit warning for arg quot;action:message:category:module:linenoquot; Attributes Manipulation......................3 Copying, Moving, Removing.............12 3 Left part name can be container expression. If expr is a sequence of -x Skip first line of source (fort non-Unix forms of #!cmd). Special Methods.................................3 Encoded Files.................................... 12 multiple values, can unpack into multiple names. Can have multiple -c cmd Execute cmd. Descriptors protocol...........................3 Serialization......................................12 assignments of same value on same line : a = b = c = expr. -m mod Search module mod in sys.path and runs it as main script. Copying Objects..................................3 Persistence.......................................12 Other statements (loops, conditions…) introduced in respective parts. Python script file to execute. file Introspection.......................................3 Configuration Files............................12 Modules and Packages............................3 Exceptions.............................................12 Command-line arguments for cmd/file, available in args Blocks Source encodings...............................3 Standard Exception Classes.............12 sys.argv[1:]. A : between statements defines dependant statements, written on same Special Attributes...............................3 Warnings...........................................13 Main Execution / Script Parameters........3 Exceptions Processing......................13 FILES EXTENSIONS line or written on following line(s) with deeper indentation. Operators................................................4 Encoding - Decoding.............................13 Blocks of statements are simply lines at same indentation level. Priority................................................4 Threads & Synchronization...................13 .py=source, .pyc=bytecode, .pyo=bytecode optimized, .pyd=binary if x<=0 : return 1 Arithmetic Operators..........................4 Threading Functions.........................13 module, .dll/.so=dynamic library. if asin(v)>pi/4 : Comparison Operators........................4 Threads.............................................13 .pyw=source associated to pythonw.exe on Windows platform, to run     a = pi/2 Operators as Functions.......................4 Mutual Exclusion...............................13 without opening a console. Booleans..................................................4 Events...............................................13     b = ­pi/2 Numbers..................................................4 Semaphores......................................13 LANGUAGE KEYWORDS else : Operators............................................4 Condition Variables...........................13     a = asin(v) Functions............................................4 Synchronized Queues.......................13 List of keywords in standard module keyword.     b = pi/2­a Bit Level Operations................................5 Process..................................................13 and as1 assert break class continue def del elif else  Operators............................................5 Current Process.................................13 Statement continuation lines don't care indentation. except exec finally for from global if import in is lambda  Strings....................................................5 . Signal Handling.................................14 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. Assignment Shortcuts Methods and Functions.......................5 XML Processing.....................................15 Don't redefine these constants : None, True, False. Formating...........................................6 SAX - Event-driven............................15 a += b a ­= b a *= b a /= b Constants............................................6 DOM - In-memory Tree......................16 BUILTINS a //= b a %= b a **= b Regular Expressions...........................6 Databases.............................................17 Localization.........................................7 Generic access to DBM-style DBs.....17 a &= b a |= b a ^= b a >>= b a <<= b Available directly everywhere with no specific import. Defined also in Multilingual Support............................7 Standard DB API for SQL databases .17 module __builtins__. Evaluate a once, and assign to a the result of operator before = applied Containers............................................. .8. Tools......................................................18 Operations on Containers...................8 1a 1b 1c
    • from enclosing definitions, then globals, then builtins. enumerate(iterable)→ iterator returning tuples (index,value) from iterable to current a and b. Example : a%=b ≈ a=a%b 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. next(self)→ next item1 print expression[,…] Class namespace → names defined in a class (class members). __iter__(self)→ iterator object itself input([prompt]) → evaluation of user input (typed data) 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__ Functions retaining their state between two calls. Return values using dir([object]) → list: names defined in object namespace1 yield. Stop generation via simple return or via raise StopIteration. All are files or files-like objects. The __xxx__ forms keep access to vars([object]) → dict2: identifier:value of object as a namespace1 1) build generator from function : gen=generatorfct(args) original standard IO streams. if object not specified use nearest namespace (locals). 1 Ctrl-C raises KeyboardInterrupt exception. 2) use gen.next() values until StopIteration is raised. must not be modified. 2 _ → value of last expression evaluation Generator iterable expressions with : (x for x in iterable where cond) Constants, Enumerations 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 See Operations on Containers (p8). redefinition (can eventually define namespaces as classes with sys.ps1 → str: primary interpreter prompt attributes access control - not in Python spirit, and execution cost). See Iteration Tools (p9). See third party modules pyenum for strict enum-like namespace. sys.ps2 → str: secondary (continuation) interpreter prompt INTERPRETATION / EXECUTION FLOW CONTROL See external package ipython for an enhanced interactive Python shell. compile(string1,filename,kind2[,flags3[,dont_inherit3]]) → code object OBJECTS, NAMES NAMESPACES eval(expression[,globals[,locals]]) → value: evaluation4 of expression string AND Condition Loop eval(code_object[,globals[,locals]]) → value: evaluation4 of code object if cond : inst Identifiers for var[,…] in iterable : inst exec5 statements [in globals[,locals]] ® statements string1 executed4 [ elif cond : inst ] Use : [a-zA-Z_][a-zA-Z0-9_]* [ else : inst ] execfile(filename[,globals[,locals]])® file interpreted4 [ else : inst ] Special usage for underscore : while cond : inst _xxx global not imported by import * 1 Multi-line statements in source code must use n as newline, and There is no 'switch' or 'case'. [ else : inst ] _xxx implementation detail, for internal use (good practice) must be terminated by a newline. Can use if elif elif… else. __xxx 'private' class members, defined as _ClassName__xxx 2 Kind relative to string content, 'exec' → sequence of statements, Exit loop with break. Can use a mapping with __xxx__ normally reserved by Python 'eval' → single expression, 'single' → single interactive statement. functions bound to cases. Go to next iteration with continue. 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 In context of globals and locals namespaces. Objects and Names, Reference Counting 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 FUNCTIONS DEFINITIONS & USAGE objects. Exit function/method with return [value] = 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 = (quot;Helloquot;,quot;Worldquot;) x,y,tabz[i] = fct(i) Cannot yield within a try/finally block. Parameters / Return value s = quot;Helloquot; pi,e = 3.14,2.71 a,b = b,a Exceptions Parameters are passed by references to objects. When an object is no longer referenced (by names or by containers), it is try : inst You can modify values of mutable objects types. destroyed (its __del__ method is then called). You cannot modify values of immutable objects types - as if they were   [,    ]] : inst except  except_class   value         [                   sys.getrefcount(object)→ int: current reference counter of object passed by value. … Notation *  → variable list of anonymous parameters in a tuple. [ else : inst ] Standard module weakref define tools to allow objects to be garbage Notation ** → variable list of named parameters in a dict. Can have a tuple of classes for except_class. Not specifying a class collected when necessary and dynamically re-created on-demand. Return value(s) with return [value[,…]] catch all exceptions. 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 of function definition reached, return None value. try : inst modified (must build a new object with new value). Lambda functions Immutable : bool, int, long, float, complex, string, unicode, finally : inst 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). raise exception_object symbols which must not be modified. Expression uses values known at definition time (except for params). raise Namespaces Callable Objects Last form re-raise the currently catched exception in an exception Places where Python found names. Objects having a __call__ method can be used as functions. handler. Builtins namespace → names from module __builtins__, already Methods bound to objects can be used as functions : f = o.meth Iterable Protocol 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). Objects of class defining __iter__ or __getitem__ are iterable (directly Calling Functions Local namespace → names defined in methods/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 - Serialization - p12), in place of __copy__ and __deepcopy__. __new__(classref,initargs…)→ object of classref type, already initialized1 Functions Control Introspection __init__ (self,initargs…)® called to initialize object with initargs sys.getrecursionlimit()→ int: current recursion limit for functions Beyond this documentation. Many __xxx___ attributes are defined, __del__ (self)® called when object will be destroyed sys.setrecursionlimit(limit) ® set recursion limit for functions 1 some are writable (see other docs). If don't return a classref object, then object.__init__ is called with Decorators initargs. See standard module inspect to manipulate these data. Glue code (functions) called at functions and methods definitions time, Object Cast Example of Introspection Attributes return the final function/method (generally with wrapping code). @  ( __repr__(self)→ str: called for repr(self) and `self` [   )]   decoratorname   decorator_arguments   […] Note: classes are objects too!         __str__(self) → str: called for str(self) and print self __base__ → list: parent classes of a class def fct(fct_rguments):… __coerce__(self,other) → value, called for coerce(self,other) __slots__ → tuple: allowed objects attributes names1 of a class @dec1 @dec2(args) @dec3 def fct(…):… like __class__ → class/type: object's class fct = dec1(dec2(args)(dec3(fct)))) Object Hash Key def fct(…):… ® __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 mapping keys comparison - default implementation use hash(id(self)) definitions. __name__ → str: object definition name of a function TYPES/CLASSES & OBJECTS Attributes access __file__ → string: pathname of loaded module .pyc, .pyo or .pyd See also quot;Descriptors protocolquot; infra. 1 List of allowed attributes names. Usage discouraged. All data are typed objects relying to classes. __getattr__(self,name)→ value, called for undefined attributes type(o) → type: type object of o __getattribute__(self, name)→ value, always called MODULES PACKAGES Standard module types define type objects for builtins types. AND __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 Searched in the Python PATH. __call__(self, *args, **kwargs)→ value, called for obj(…) Support multiple inheritance. Can inherit from builtin class. Current Python PATH stored in sys.path list. Contains directories and Static method / Class method Inherit at least from object base class => Python 'new style class'. .zip files paths. Built from location of standard Python modules, Use standard decorators (see Decorators p3). First parameter of methods is target object, standard use self name. PYTHONPATH environment variable, directory of main module given on class ClassName : Access class members via class name, object members via self. command line, data specified in lines of .pth files found in Python home     @staticmethod directory, and data specified in registry under Windows. This doc consider you use new style class (inheriting from object). Current list of loaded modules stored in sys.modules map (main module     def methodname(…): … Metaclass is under key __main__).     @classmethod 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[,…]} from module import name [as alias] [,…] Descriptors protocol def metname(self[,…]): from module import * Descriptors are attribute objects controling access to attributes values. x.metname = metname reload(module) ® module is reloaded (but old references still on old module They must define some of following methods : This allow creation of metaclass classes (class building other class). __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 In these methods self is the descriptor object, and obj is the target init code (reference counter reach zero). defined. object which attribute is manipulated. To limit your modules names exported and visible by import *, define Classes & Objects Relations Properties module global __all__ with list of exported names (or use global isinstance(obj,classinfo) → bool: test object of type/class classinfo 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 Copying Objects super(ThisClass,self).methodname(self,args…) special comment. Assignment only duplicate references. To shallow copy an object (build a # ­*­ coding: encoding_name ­*­ Or the old way, via parent class namespace : 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 uquot;…quot; 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 Params memo and nil are used in recursive deepcopy, their default object.name → value of object attribute 1 __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 MAIN EXECUTION / SCRIPT PARAMETERS delattr(object,name) ® object attribute removed __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 by shell with first script line #! /bin/env python). Other special overridable __xxx___ methods are listed in respective copy.deepcopy(…) Command-line parameters are available in sys.argv (a python list). sections. 3a 3b 3c
    • BOOLEANS At end of module, we may have : Comparison Overriding 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: __gt__(self, other)→ bool1 : called for self > other bool(expr) → True | False     mainfunction(sys.argv[1:]) __ge__(self, other)→ bool1 : called for self >= other Logical not : not expr     # or in lib modules, execute test/demo code... __eq__(self, other)→ bool1 : called for self == other Logical and : expr1 and expr2 Execution exit after last main module instruction (in multithread, wait Logical or : expr1 or expr2 __ne__(self, other)→ bool1 : called for self != other also for end of non-daemon threads), unless interactive mode is forced. Logical and and or use short path evaluation. and for self <> other Can force exit with calling sys.exit(code), which raise a SystemExit  __cmp__(self,other)→ int : called for self compared to other, Bool Cast Overriding exception - see Current Process - Exiting (p13). self<other→value<0, self==other→value=0, self>other→value>0 __nonzero__(self) → bool : test object itself1 OPERATORS Any value usable as boolean value, or a NotImplemented value if 1 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. NUMBERS Priority Operators as Functions Builtin integer types : int (like C long), long (unlimited integer) 1 (a,…) [a,…  6 x+y 11 x<y x<=y x>y x>=y x==y x!=y Operators are also defined as functions in standard operator module.        ] int(expr[,base=10]) → int: cast of expr {a:b,…  `…  x­y x<>y x is y x is not y x in s              `   } Comparison long(expr[,base=10]) → long: cast of expr x not in s 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). x>>y s.attr f(…) float(expr) → float: representation of expr eq(a,b) = __eq__(a,b) gt(a,b) = __gt__(a,b) 13 x and y 3 +x  ­x  ~x   8 x&y          complex(x[,y]) → complex: number: x+yj Logical / Boolean [x+]yj → complex: number, ex: 3+4j -8.2j 4 x**y 9 x^y 14 x or y not_(o) = __not__(o) and_(a,b) = __and__(a,b) c.real → float: real part of complex number 5 x*y x/y x%y 10 x|y 15 lambda    :expr truth(o) or_(a,b) = __or__(a,b)     args        c.img → float: imaginary part of complex number is_(a,b) xor(a,b) = __xor__(a,b) Arithmetic Operators c.conjugate() → complex: conjugate of complex number (real,-img) is_not(a,b) Can be defined for any data type. Maximum int integer in sys.maxint. Arithmetic Automatic conversions between numeric types. Arithmetic Overriding Automatic conversions from int to long when result overflow max int. abs(o) = __abs__(o) truediv(a,b) = __truediv__(a,b) __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 x­y x*y x/y 1 x//y 1 x%y 2 x**y 2 Bit Level With from __future__ import division, / is true division (1/2→0.5), __mod__(self,other) →  value: called for self % other 1 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) % is remainder operator, ** is power elevation operator (same as pow). 2 __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 abs(x) → absolute value of x concat(a,b) = __concat__(a,b) __pos__(self) →  value: called for +self divmod(x,y) → (x/y,x%y) contains(a,b) = __contains__(a,b) __abs__(self) →  value: called for abs(self) oct(integer) → str: octal representation of integer number countOf(a,b) __iadd__(self,other) ® called for self += other hex(integer) → str: hexadecimal representation of integer number indexOf(a,b) __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 delslice(a,b,c) = __delslice__(a,b,c) 1 without / 2 with from __futur__ import division 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. cosh(x) → float: hyperbolic cosinus value for radians angle x These functions must be considered as not reliable. 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 attrgetter(attr) → fct: where fct(x)→x.attr Builtin function cmp(o1,o2) → ­1 (o1 < o2), 0 (o1 == o2), 1 (o1 > o2) 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 unicode(object[,encoding[,errors]]) → unicode: unicode x&y → bitwise and on x and y log10(x) → float: decimal logarithm of x sys.maxunicode →  int: maximum unicode code=fct(compile time option) x|y → bitwise or on x and y 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 decimal(unichr[,default]) →  int: decimal value - may raise ValueError tanh(x) → float: hyperbolic tangent value for radians angle x __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 category(unichr) →  str: general unicode category of char Module cmath provides similar functions for complex numbers. __lshift__(self,other) → value: for self << other bidirectional(unichr) →  str: bidir category of char, may be empty str Random Numbers __rshift__(self,other) → value: for self >> other combining(unichr) →  str/0: canonical combining class of char as integer Randomization functions in standard random module. Module functions __invert__(self) → value: for ~self east_asian_width(unichr) →  str: east asian width use an hidden, shared state, Random type generator (uniform __iand__(self,other) ® called for self &= other mirrored(unichr) →  int: mirrored property in bidi text, 1 if mirrored else 0 distribution). __ior__(self,other) ® called for self |= other 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 normalize(form, unistr) →  str: normal form of string - form in 'NFC', __ilshift__(self,other) ® called for self <<= other 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 STRINGS uniform(a, b)→ float: random value in [a, b[ Methods and Functions getrandbits(k)→ long: with k random bits Simple quoted 'Hello' or double-quoted quot;Helloquot;. 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 quot;quot;quot;Hello, step) Most string methods are also available as functions in the standard how are you ?quot;quot;quot; choice(seq)→ value: random item from sequence string module. Strings are immutable (once created a string cannot be modified in shuffle(x[,rndfct]) ® items of x randomly reordered using rndfct() s.capitalize() → string with first char capitalized1 place). 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 v - vertical tab s.isalpha() → bool: non empty string with all alphabetic chars1 - bell os.urandom(n) →  str: n random bytes suitable for cryptographic use b ' - single quote - backspace s.isdigit() → bool: non empty string with all digit chars1 Other Math Modules e quot; - double quote - escape s.islower() → bool: non empty string with all lower chars1 f - backslash - form feed Advanced matrix, algorithms and number crunching in third party s.isspace() → bool: non empty string with all space chars1 n ooo - char by octal ooo value - new line modules like numarray / Numeric (known as NumPy), gmpy  s.istitle() → bool: non empty string with titlecase words1 r xhh - char by hexadecimal hh value - carriage return (multiprecision arithmetic), DecInt, scipy, … s.isupper() → bool: non empty string with all upper chars1 t <newline> - continue string on next line. - horizontal tab 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 Uxxxxxxxx - unicode char by 32 bits hexadecimal xxxxxxxx value. __int__(self) → int: called for int(self) s.lstrip([chars]) → string text with leading chars2 removed N{name} - unicode char by name in the Unicode database. __long__(self) → long: called for long(self) s.replace(old,new[,count]) → string with count firsts old replaced by new Keep escape chars by prefixing string literals with a r (or R) - for 'raw' __float__(self) → float: called for float(self) s.rfind(sub[,start[,end]]) → int/­1: last offset of sub within start-end strings (note : cannot terminate a raw string with a ). __complex__(self) → complex: called for complex(self) s.rindex(sub[,start[end]])→ int: last offset of sub - may raise ValueError Unicode strings __oct__(self) → str: called for oct(self) s.rjust(width[,fillchar]) → string text right aligned2 Quoted as for str, but with a u (or U) prefix before the string : uquot;Voiçiquot; __hex__(self) → str: called for hex(self) s.rsplit([sep[,maxsplit]])→ [string]: rightmost words delim. by sep2 Uquot;quot;quot;Une bonne journée __coerce__(self,other) → value: called for coerce(self,other) s.rstrip([chars]) → string with trailing chars2 removed en perspective.quot;quot;quot; BIT LEVEL OPERATIONS s.split([sep[,maxsplit]]) → [string]: words delimited by sep2 Can mix strings prefixs r (or R) and u (or U). s.splitlines([keepends]) → [string]: lines You must define your source file encoding so that Python knows how to Work with int and long data. s.startswith(suffix[,start[,end]]) → bool convert your source literal strings into internal unicode strings. Operators unichr(code) → unicode: string of one unicode char s.strip([chars]) → string text with leading+trailing chars2 removed ~x → inverted bits of x s.swapcase() → string with case switched1 ord(unicode char) → int: unicode code x^y → bitwise exclusive or on x and y s.title() → string with words capitalized1 5a 5b 5c
    • See also external projects pyparsing, PLY (Python Lex-Yacc), tpg (Toy Flag Options s.translate(table[,deletechars]) → string: cleaned, converted3 Parser Generator)… IGNORECASE (I) : case insensitive expression - not locale dependant. s.upper() → string uppered1 Expressions LOCALE (L) : make w W b B locale dependant. s.zfill(witdh) → string: numeric string with zeroes if necessary 1 Locale dependant for 8 bits strings. MULTILINE (M) : ^ and $ match begining/end of string and lines. Else ^  Metacharacters : . ^ $ * + ? { } [ ]  | ( ), may use escape. 2 Default chars/separator/fillchar is space. and $ match only beginning and end of string. . ® match any character except a newline (including newline with For str table must be a string of 256 chars - see string.maketrans(). 3 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. ^ ® match start of string (and start of lines with MULTILINE option) ordinals to unicode ordinals. UNICODE (U) : make w W b B unicode dependant. $ ® match end of string (and end of lines with MULTILINE option) 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 when space and # are escaped or in char class). expr+ ® match 1 or more repetitions of expr (as much as possible) 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. expr?? ® match like expr? but as few as possible For mapping, args can be an object with __getitem__ method - see See Flag Options supra for flags parameters. expr{m} ® match m repetitions of expr Overriding Mapping Operations (p8). 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 match(pattern,string[,flags])→ MatchObject/None: try to match string with d i signed int. decimal : -324 signed int. decimal : -324 expr{[m],[n]}? ® match like expr{[m],[n]} but as few as possible pattern unsigned octal : 774 unsigned decimal 6953 o u [set] ® match one char in the set defined by : split(pattern,string[,maxsplit=0])→ [string]: split string by occurences of ^ → at begining, invert set definition unsigned hexa : f3a unsigned hexa : F3A x X pattern - if maxsplit specified, remainder is put in last item of list x­y → chars from x to y float. point exp. : ­3.256e­12 float. point exp. : ­3.256E­12 e E findall(pattern,string[,flags])→ [string]/[(string)]: find non-overlapping x → see Escape sequences for strings (p5) float. point dec. : ­0.0000032 float. point dec. : ­0.0000032 f F 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) sub(pattern,repl,string[,count=0])→ string: replace substrings matching (expr) ® match expr and build a numbered group (?[i][L][m][s][u][x]) ® (at least one ot iLmsux char) group match empty Templates pattern by repl - repl as string can contain back references1 to identified substring - repl as fct(MatchObject) return replacement string - pattern may be string, modify options flags for entire expression - see I L M S U X  With string.Template objects. Use common $ syntax : $$®single $ ; 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 escape(string)→ string: non-alphanumerics backslashed tmpl.substitute(mapping[,**kwargs]) → string: template filled (name must be valid Python identifier) tmpl.safe_substitute(mapping[,**kwargs]) → string: template filled If you need to reuse a pattern, compile it one time for all. (?P=name) ® match text matched by earlier group named name pat = re.compile(pattern[,flags])→ RE_Pattern object (?#text) ® no match, text is just a comment tmpl.template → string 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 (?<=expr) ® match if current position is immediatly preceded by a Constants 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 3 n nn ® match 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 [0­9] 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 [^0­9] or names m.groups([default=None])→ (string): all subgroups of the match - default s ® match char class whitespace [ tnrfv] printable → str: printable chars give access to subgroups not in the match S ® match char class non-whitespace [^ tnrfv] uppercase → str: uppercase A-Z chars1 m.groupdict([default=None])→ dict: name→subgroup: all named subgroups w ® match char class alphanumeric [a­zA­Z0­9_] whitespace → str: whitespace chars (spc, tab, cr, lf, ff, vt) of the match - default give access to subgroups not in the match W ® match char class non-alphanumeric [^a­zA­Z0­9_] 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 c ® for other c chars, match char c 1 Definition is locale dependant. group exist but not in match 1 Depends on UNICODE flag. m.span([group=0])→ (int{2}): values of start and end methods for the Regular Expressions 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/. m.endpos → int: endpos value of search/match method Use raw string rquot;…quot; notation. 6a 6b 6c
    • Base API key nl_langinfo() value usage m.lastindex → int/None: index of last matched capturing group name of the nth month MON_1… MON_12 bindtextdomain(domain[,localedir])→ str: bounded directory - bind domain m.lastgroup → string/None: name of last matched capturng group 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 bind_textdomain_codeset(domain[,codeset])→ codeset binding: bind ABMON_12 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/…) textdomain([domain])→ global domain: set global domain if specified and not 1 Back references extended to g<groupnum> and g<groupname>. THOUSEP separator character for thousands 1 None Using part of string between pos and endpos. YESEXPR regular expression (of C library!) usable for yes reply Group number 0 correspond to entire matching. gettext(message)→ string: localized translation of message - based on NOEXPR regular expression (of C library!) usable for no reply current global domain, language, and locale directory - usually aliased as _ in Localization currency symbol, preceded by ­ if should appear before CRNCYSTR 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 dgettext(domain,message)→ string: like gettext(), looking in specified setlocale(category[,locale]) → current/new settings: if locale specified era - generally not defined - same as E format in ERA 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 ERA_YEAR year in era encoding thread safe ngettext(singular,plural,n)→ string: like gettext(), but consider plural localeconv()→ dict: database of local conventions ERA_D_T_FMT usable as format for strftime() for date and time with era 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 lngettext(singular,plural,n)→ string: like ngettext(), using preferred all systems - options may vary on systems - see options p7 ALT_DIGITS up to 100 values representing 0 to 99 encoding getdefaultlocale([envvars])→(language code, encoding): try to determine dngettext(domain,singular,plural,n)→ string: like ngettext(), looking in localeconv keys default locale settings 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 decimal_point getpreferredencoding([do_setlocale])→ str: user preffered encoding for Decimal point character for numbers. Generally _ is bound to gettext.gettext, and translatable strings are frac_digits Number of fractional digits used in local text data - set do_setlocale to False to avoid possible call to setlocale() written in sources using _('thestring'). See docs for usage formatting of monetary values. normalize(localename)→ normalized locale code for localename - usable with examples. grouping [int]: relative positions of 'thousands_sep' in setlocale() - return localename if normalization fails Class based API numbers. CHAR_MAX at the end stop grouping. resetlocale([category]) ® reset locale for category to default setting - The recommended way. Module gettext defines a class Translations, 0 at the end repeat last group. category default to LC_ALL dealing with .mo translation files and supporting str/unicode strings. int_curr_symbol International currency symbol of monetary strcoll(s1,s2)→ int: compare two strings - follow LC_COLLATE setting - find(domain[,localedir[,languages[,all]]])→ str/None: .mo file name for values. return 0 if s1==s2, <0 if s1<s2, >0 if s1>s2 int_frac_digits Number of fractional digits used in international translations (search in localedir/language/LC_MESSAGES/domain.mo) strxfrm(string)→ string:transform string for locale-aware comparison translation(domain[,localedir[,languages[,class_[,fallback[,codeset]]]]])→ formatting of monetary values. 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 mon_thousands_sep Group separator used for monetary values. point) install(domain[,localedir[,unicode[,codeset]]]) ® install _ function in True if currency symbol preceed negative n_cs_precedes atof(string)→ float: convert string to float - follow LC_NUMERIC settings Python's builtin namespace, to use _('thestring') monetary values, false if it follow. 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. Position of negative sign for monetary values1. Categories n_sign_posn t.__init__([fp]) ® initialize translations: fp is a file object - call _parse(fp) if negative_sign Symbol used to annotate a negative monetary LC_CTYPE → character type - case change behaviour it is not None value. t._parse(fp) ® nothing: subclasses override to read data from the file LC_COLLATE → strings sorting - strcoll() and strxfrm() functions p_cs_precedes True if currency symbol preceed positive t.add_fallback(fallback) ® add fallback used if cannot found translation LC_TIME → time formating - time.strftime() monetary values, false if it follow. for a message LC_MONETARY → monetary values formating - options from localeconv() True if there is a space between currency p_sep_by_space Define methods gettext, lgettext, ngettext, lngettext as in the LC_MESSAGES → messages display - os.strerror() - not for Python symbol and positive monetary value. base API. And define speciale methods ugettext and ungettext  messages Position of positive sign for monetary values1. p_sign_posn returning unicode strings (other forms return encoded str strings). LC_NUMERIC → numbers formatting - format(), atoi(), atof() and str()  positive_sign Symbol used to annotate a positive monetary Return translated message, forwarding to fallback if it is defined. of this module (dont modify normal Python number formating) value. Overriden in subclasses. LC_ALL → all locales - used to change/retrieve the locale for all categories thousands_sep Character used between groups of digits in t.info()→ return protected _info attribute numbers. nl_langinfo options t.charset()→ return protected _charset attribute 1 Possible values : 0=currency and value surrounded by parentheses, key nl_langinfo() value usage 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) follow value and currency symbol, 3=sign should immediately precede D_T_FMT usable as format for strftime() for time and date t.set_output_charset(charset) ® set _output_charset attribute value, 4=sign should immediately follow value, LC_MAX=nothing D_FMT usable as format for strftime() for date 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) Multilingual Support T_FMT_AMPM usable as format for strftime() for time in am/pm format GNU Translations Standard module gettext for internationalization (I18N) and localization name of the nth day of the week - first day is sunday DAY_1…DAY_7 The GNUTranslations class (subclass of NullTranslations) is based (L10N) services - based on GNU gettext API + higher interface. See docs ABDAY_1… abbreviated name of the nth day of the week - first day is on GNU gettext and .mo files. for explanations about tools usage. sunday ABDAY_7 Messages ids and texts are coerced to unicode. Protected _info attribute contains message translations. 7a 7b 7c
    • filter1(fct,sequence)→ list: new list where fct(item) is true. None fct = bool Overriding Sequences Operations Translation for empty string return meta-data (see doc). Define methods gettext, lgettext, ugettext, ngettext, lngettext, __getitem__(self,index2)→ value: item at index, called for self[index] test on items 1 map (fct,sequence,…)→ list: new list where i item is fct(i items of ungettext as in NullTranslations interface - same rules for return th th __setitem__1(self,index2,value)® set item at index to value, called for sequence(s)) values (str/unicode). Message translations are searched in catalog, self[index]=value reduce(fct,sequence[,initializer])→ value: fct applied cumulatively to then in fallback if defined, and if no translation is found, message itself __delitem__1(self,index2,value)® remove item at index, called for sequence items, f(f(…f(f(f(initializer,a),b),c,…) is returned (for n… methods, return singular forms if n=1 else plural del self[index] zip1(sequence,…)→ list: list of tuples, ith tuple contains ith items of each forms). 1 Only for mutable sequences. sequences CONTAINERS 2 Parameter index can be a slice [start,stop,step] - replace old 1 See Iteration Tools (p9) as replacement (avoid creating a new list). __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. MAPPINGS (DICTIONARIES) -sets (unordered collections) : set, frozenset… Sub-sequences indexs between items. Operations on Containers l = [e1,e2,e3,…,en­2,en­1,en] Builtin type dict. Store key:value pairs. l[0]→   e1 For strings, items are chars. For mappings, items are keys. l[0:n]→[e1,e2,e3,…,en­2,en­1,en] Declare a dictionary : { key:value [,…]} {} l[1]→   e2 item in container → bool: test item ∈ container1 l[:]→[e1,e2,e3,…,en­2,en­1,en] dict()→ dict: empty dictionary (like {}) l[­2]→   en­1 item not in container → bool: test item ∉ container1 dict(**kwargs)→ dict: from named parameters and their values l[i:]→[ei+1,ei+2,ei+3,…,en­1,en] l[­1]→   en for var in container: … ® iterate var over items of container dict(iterable)→ dict: from (key,value) by iterable l[:i]→[e1,e2,…,ei­2,ei­1,ei] len(container) → int: count number of items in container2 dict(otherdict)→ dict: duplicated fro another one (first level) items indexs max(container) → value: biggest item in container -n -n+1 -n+2 … -2 -1 Operations on Mappings min(container) → value: smallest item in container 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) d[key]→ value for key1 1 For strings test if expr is a substring of sequence. e1 e2 e3 …item… en-1 en d[key]=value ® set d[key] to value 2 Container must provide direct length method - no generator. del d[key]® removes d[key] from d1 0 1 2 3 … n-2 n-1 n Copying Containers d.fromkeys(iterable[,value=None])→ dict: with keys from iterable and all Default containers constructors build new container with references to -n -n+1 -n+2 -n+3 … -2 -1 same value existing objects (shallow copy). To duplicate content too, use standard slicing indexs d.clear() ® removes all items from d module copy. See Copying Objects (p3). d.copy() → dict: hallow copy of d Slice objects Overriding Containers Operations d.has_key(k)→ bool: test key presence - same as k in d Defines index range objects, usable in [] notation. __len__(self)→  int: called for len(self) d.items()→ list: copy of d's list of (key, item) pairs slice([start,]stop[,step])→ slice object __contains__(self,item)→  bool: called for item [not] in self d.keys()→ list: copy of d's list of keys slice.indices(len)→ (int{3}): (start,stop,stride) You can override iterable protocol on containers too. d.update(otherd)® copy otherd pairs into d Ordered sets of data indexed from 0. Members start, stop, step. SEQUENCES d.update(iterable) ® copy (key,value) pairs into d Extended Slicing d.update(**kwargs) ® copy name=value pairs into d Sequences are ordered collections : str, unicode, list, tuple, buffer, Multiple slices notation - corresponding to a selection in a multi- d.values()→ list: copy of d's list of values xrange, array.array… any user class defining sequences interface, or dimension data - can be written using notation like d.get(key,defval)→ value: d[key] if key∈d, else defval any iterable data. [ a , x:y:z , : , : , : , m:n ]. d.setdefault(key[,defval=None]) → value: if key∉d set d[key]=defval, return Ellipsis notation can be used to fill multiple missing slices, like Lists & Tuples d[key] [ a , x:y:z , ... , m:n ]. See docs. Builtin types list and tuple store sequences of any objects. d.iteritems()→ iterator over (key, value) pairs Three dot notation ... is replaced internally by Ellipsis object. Lists are mutable, tuples are immutable. d.iterkeys()→ iterator over keys Declare a list : [item[,…]] Operations on mutable sequences d.itervalues()→ iterator over values Declare a tuple : (item[,…]) Mutable sequences (ex. list) can be modified in place. d.pop(key[,defval]) → value: del key k and returns the corresponding value. If Notes : []® empty list ;()® empty tuple ; (item,)® one item tuple. Can use mutable sequence indexing in left part of assignment to modify key is not found, defval is returned if given, otherwise KeyError is raised list(object) → list: new list (cast from object / duplicate existing) its items : seq[index]=expr ; seq[start:stop]=expr ; d.popitem() → removes and returns an arbitrary (key, value) pair from d tuple(object) → tuple: new tuple (cast from object / duplicate existing) 1 seq[start:stop:step]=expr If key doesn't exist, raise KeyError exception. range([start,]stop[,step])→ [int]: list, arithmetic progression of integers seq.append(item) ® add item at end of sequence Overriding Mapping Operations xrange1([start,]stop[,step]) → xrange: object generating arithmetic seq.extend(otherseq) ® concatenate otherseq at end of sequence __getitem__(self,key)→ value for key, called for self[key] progression of integers seq.count(expr) → int: number of expr items in sequence __setitem__(self,key,value)® set value for key, called for Unless using a sequence as a mapping key, or ensuring it is immutable seq.index(expr[,start[,stop]])→ int: first index of expr item self[key]=value data, prefer list to tuple. seq.insert(index,item) ® item inserted at index 1 Use in place of range to avoid building huge lists just for indexing. __delitem__(self,key,value)® remove value for key, called for seq.remove(expr) ® remove first expr item from sequence del self[key] Operations on Sequences seq.pop([index]) → item: remove and return item at index (default -1) Can also override container operations and object operations. See Operations on Containers (p8) too. seq.reverse() ® items reversed in place seq1 + seq2 → concatenation of seq1 and seq2 Other Mappings seq.sort([cmp][,key][,reverse])® items sorted in place - cmp : custom For on-disk mappings, see standard module shelve, and database sequence * n → concatenation of sequence duplicated n times comparison fct(a,b), retval <0 or = 0 or >0 - key : name of items attribute to modules . n * sequence →  concatenation of sequence duplicated n times compare - reverse : bool For ordered mappings see third party modules OrderedDict. reversed(sequence)→ iterator throught sequence in reverse order del seq[index] ® remove item from sequence sorted(sequence[,cmp[,key[,reverse]]])→ list: new list, sorted items from del seq[start:stop[:step]] ® remove items from sequence iterable - see list.sorted 8a 8b 8c
    • SETS izip(iterable[,…])→ iterator over tuple(items at same index from iterables) a.fromlist(list) ® extend array from values in list repeat(object[,count])→ iterator returning object over and over again, up to a.tolist() → list: items in a list Unordered collections of unique items. Frozen sets are immutable once count times (default to infinite) a.fromstring(s) ® extend array from values in binary buffer s (string) created. starmap(function,iterable)→ iterator over function(*tuple item from iterable) a.tostring() → str: items in binary representation set([iterable]) → set: using values from iterable takewhile(predicatefct,iterable)→ iterator over items of iterable where a.fromunicode(s) ® extend 'u' array from data in unicode sting frozenset([iterable]) → frozenset: using values from iterable predicatefct(item) is true a.tounicode() → unicode: convert 'u' array to unicode string tee(iterable[,n]) n independent iterators from same iterable4, default n=2 Operations on Sets 1 If less items than needed, get available ones then raise EOFError. 1 Group of items is internally used - must save it as list if needed after See Operations on Containers (p8) too. Old methods read and write replaced by fromfile and tofile. current iteration. s.issubset(others)→ bool: test s ⊂ others Queue 2 Stop at end of shorter iterable. s.issuperset(others)→ bool: test others ⊂ s 3 Slice parameters cannot be negative. Standard module collections provides queues management. s.add(item) ® adds item to set 4 Don't use iterable out of tee created iterators. deque([iterable])→ deque: initialized from iterable s.remove(item) ® removes item from set1 DATE & TIME q.append(x)® add x to right side of deque s.clear()® removes all items from (not forzen) set q.appendleft(x)® add x to left side of deque s.intersection(others)→ set: s ∩ others Module time q.clear()® remove all elements from deque s & others → set: s ∩ others Standard module time defines common functions and data. q.extend(iterable)® extend right side of deque s.union(others) → set: s ∪ others Date & Time Data q.extendleft(iterable)® extend left side of the deque s | others → set: s ∪ others • float_time = float containing seconds from 'epoch' (january 1 1970 q.pop() → item: pop and return item from dequeue right side s.difference(others) → set: [x / x∈s and x∉others] on Unix - see gmtime(0)), with sub-second precision in decimal part. q.popleft() → item: pop and return item from dequeue left side s ­ others → set: [x / x∈s and x∉others] • tuple_time = tuple containing 9 int (see table). q.rotate(n) ® rotate deque from n steps, to right if n>0, to left if n<0 s.symmetric_difference(others)→ set: [x / x∈s xor x ∈others] • struct_time = tuple/object with int attributes (see table). Can also use standard operations on sequences : len(q),  s ^ others → set: [x / x∈s xor x ∈others] # attribute value # attribute value reversed(q), copy.copy(q), copy.deepcopy(q), item in q, q[­1], s.copy()→ shallow copy of set int 0…61 tm_year tm_sec 0 5 and serialization via pickling protocol. s.update(iterable)® adds all values from iterable to set 1 6 1…12 0…6 (monday=0) tm_mon tm_wday Priority Queues 1 Raise KeyError if object not in set. 1…31 0…366 tm_mday tm_yday 2 7 Standard module heapq. Structure a list as a priority queue. Results set have same type as s object (set/frozenset). heapify(x) ® x list transformed into heap 3 8  0 (no) 0…23 tm_hour tm_isdst OTHER CONTAINERS STRUCTURES, ALGORITHMS  1 (yes) heappush(heap,item)® push item onto heap 0…59 tm_min 4 ­1 (unknown) Generally containers follow Python idioms, you can use : len(cont), heappop(heap)→ item: pop and return smallest item from the heap cont[i], for item in cont:… • float_delay = float containing seconds, with sub-second precision. heapreplace(heap,newitem)→ item: pop and return smallest item from the Array heap, push new item DST is local time, UTC is universal (GMT) time. nlargest(n,iterable)→ list: n largest from iterable accept2dyear → [rw] bool: accept two-digit year values (default true), Standard module array provides efficient array of basic types. It uses nsmallest(n,iterable)→ list: n smallest items from iterable modifiable via environment var PYTHONY2K compact storage for elements of same type. altzone → int: offset (pos/neg) in seconds of DST relatively to UTC, in Sorted List Type Codes seconds, use only if daylight is true Standard module bisect maintains lists sorted (via basic bisection algo). daylight → int: ≠0 if a DST timezone is defined n tc C type py type n tc C py type bisect_left(list,item[,lo[,hi]])→ int: index to insert item at leftmost sorted 'b' int 'B' int 1 signed char 1 unsigned char timezone → int: offset (pos/neg) in seconds of local (non DST) timezone position1 'c' str 'u' unicode 1 char 2 unicode char tzname → (str{2}): names of local timezone (non-DST, DST) bisect_right(list,item[,lo[,hi]])→ int: index to insert item at rightmost 'h' int 'H' int 2 signed short 2 unsigned short Functions sorted position1 'i' int 'I' long 2 signed int 2 unsigned int bisect(…) ® Alias for bisect_right(…) asctime([t=2])→ str: build local time string from t (tuple_time or struct_time) 'l' int 'L' long 4 signed long 4 unsigned long insort_left(list,item[,lo[,hi]]) ® insert item at leftmost sorted position1 clock()→ float: processor time in seconds, for accurate relative time 'f' float 'd' float 4 float 8 double insort_right(list,item[,lo[,hi]]) ® insert item at rightmost sorted position1 measurement insort(…) ® Alias for insort_right(…) ctime([secs=2])→ str: build local time string from float_time second n=size in bytes, tc=char typecode to use 1 With list previously sorted. gmtime([secs=2])→ struct_time: convert float_time to UTC struct_time Functions localtime([secs=2])→ struct_time: convert float_time to DST struct_time Iteration Tools array(tc,[iterable]) → array: with typecode tc, initialized from iterable mktime(t)→ float_time: convert DST t (tuple_time or struct_time) to float_time - Standard module itertools provides some practical iterators. a.typecode → str: typecode of the array may raise OverflowError or ValueError chain(iterable[,…])→ iterator over items of several iterables a.itemsize → int: bytes size of one array data sleep(secs) ® execution suspended during secs (float_delay) times, maybe count([start])→ iterator over integers from start (default 0) a.append(expr) ® append item expr to end of array less (signal catching), may be more (process/threads scheduling) cycle(iterable)→ iterator cycling over iterable items a.extend(array) ® append items from another array strftime(format[,t=2]) → str: build time string from t (tuple_time or dropwhile(predicatefct,iterable)→ iterator over items of iterable where a.count(expr) → int: number of expr items in array 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 may raise ValueError a.insert(index,expr) ® expr item inserted at index keyfct(item)=key value), default keyfct is identity time() → float_time: current UTC time ifilter(predicate,iterable)→ iterator over items of iterable where a.remove(expr)® remove first expr item from array tzset() ® resets time conversion rules accordingly to environnment variable predicatefct(item) is true - None predicate filter items being true a.pop([index]) → value: return and remove item at index (default -1) ifilterfalse(predicate,iterable)→ iterator over items of iterable where TZ - unix only, see docs a.reverse() ® items in array are reversed predicatefct(item) is false - None predicate filter items being false 1 Default format quot;%a %b %d %H:%M:%S %Yquot;. Missing values default to a.buffer_info() → (int{2}): current storage infos (address,items count) 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) O_TEXT → xxxxxx (Windows) f.seek(offset[,whence=0])® modify current position in file - whence : 0 from Abbreviated weekday name1. Full weekday name1. %a %A start, 1 from current, 2 from end Abbreviated month name1. Full month name1. %b %B Pipes f.tell() → int: current position in file Appropriate date and time Month day [01,31]. %c %d For standard process redirection using pipes, see also Simple External representation1. Process Control (p14). f.write(string)® data written to file %H %I Hour [00,23]. Hour [01,12]. os.pipe() → ((int{2}){2}): create pair (fdmaster,fdslav) of fd (read,write) for f.writelines(listofstrings) ® data written to file (no end of line added) %j %m Year day [001,366]. Month [01,12]. for line in f :… ® iterate over lines of f a pipe os.mkfifo(path[,mode=0666])® create named pipe path - mode masked out %M %p 1 Minute [00,59]. AM or PM . Old method xreadlines replaced by iteration on file object. with umask - don't open it (Unix) %S %U Second [00,61]. Year week [00,53] (Sunday based). For optimized direct access to random lines in text files, see module Use os functions on file descriptors. linecache. %w %W Week day [0,6] (0=Sunday). Year week [00,53] (Monday based). In-memory Files %x Appropriate date representation1. %X Appropriate time representation1. Attributes %y %Y Year [00,99]. 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 f.encoding → str/None: file content encoding no time zone exists). storing data in memory. f.name → str: name of the file 1 Locale language representation. f = StringIO.StringIO() f.newlines → str/tuple of str/None: encountered newlines chars Build a file-like in memory. f.softspace→ bool: indicator to use soft space with print in file Module datetime f.write(string)® data written to file Low-level Files Standard module datetime has tools for date/time arithmetics, data f.…other file writing methods… Base low-level functions are in standard module os. extraction and manipulation. 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 Optional argument allows to build memory files to read from too. masked out with umask f = cStringIO.StringIO([string]) fdopen(fd[,mode[,bufsize]]) → file: build a file connected to fd - mode and code. It can be used in scripts (see docs), or directly in command line : 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… repeat timer N times (default 3) -r N / --repeat=N dup2(fd,fd2)→ int: fd, duplicate file descriptor into fd2, previously closing fd2 Memory Mapped Files (OS level) executed S once initially (default pass) -s S / --setup=S if necessary Standard module mmap manage memory-mapped files, usable as file-like -t / --time use time.time() (default except Windows) close(fd)® close file descriptor objects and as mutable string-like objects. -c / --clock use time.clock() (default on Windows) read(fd,n)→ str: read as most n bytes from fd file - return empty string if end To build a memory map : -v / --verbose print raw timing results - may repeat option of file reached mm = mmap.mmap(fileno,length[,tagname[,access]]) -h / --help print help and exit [windows] write(fd,str)→ int: write str to fd file - return number of bytes actually mm = mmap.mmap(fileno,length[,flags[,prot[,access]]]) [unix] written Other Modules Use an os file descriptor (from os.open() or from file-object's lseek(fd,pos,how)® set file descriptor position - how : 0 from start, 1 from Standard module calendar has functions to build calendars. fileno()) for a file opened for update. current, 2 from end See also third party module mxDateTime. 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 extended to that length if it is shorter, it can't be empty, and 0 fsync(fd)® force low level OS buffers to be written correspond to maximum length for the file. ftruncate(fd,length)® truncate file descriptor to at most length (Unix) 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 (write-through, default on Windows), or ACCESS_COPY (copy-on- (strings). Functions mapping low level OS handlers (mainly those in 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). same file (default to None). O_RDONLY → read only Raw data use str type (can contain any data byte values, including 0). On Unix, flags : MAP_PRIVATE (copy-on-write private to process) or O_WRONLY → write only 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. O_CREAT → create new file (remove existing) Mode flags (combinable) : 'r' read, 'w' write new, 'a' write append, mm.close()® mmap file no longer usable O_EXCL → with O_CREAT, fail if file exist (Unix) '+' update, 'b' binary1, 'U' universal newline2. mm.find(string[,start=0])→ int: offset / ­1 O_TRUNC → reset existing file to zero size Buffer size : 0 unbuffered, 1 line buffered, >1 around that size. mm.flush([offset,size])®  write changes to disk O_DSYNC → xxxxxx (Unix) Open() is an alias for file() mm.move(dest,src,count)® copy data in file 1 Default text mode tries to interpret newline sequences in the file. O_RSYNC → xxxxxx (Unix) mm.read([size])→ str: block of data read from mmap file1 2 Automatically choose newline sequence in CR or LF or CR+LF adapted O_SYNC → return from IO when data are physically written (Unix) mm.read_byte()→ str: next one byte from mmap file1 from file/to platform. O_NDELAY → return immediatly (don't block caller during IO) (Unix) mm.readline()→ str: next line read from file, ?end of line removed?1 Methods and Functions O_NONBLOCK → same as O_NDELAY (Unix) mm.resize(newsize)® writable mmap file resizer O_NOCTTY → terminal device file can't become process tty (Unix) f.close()® file flushed and no longer usable mm.seek(offset[,whence=0])® modify current position in mmap file - O_BINARY → don't process end of lines (cf+lf from/to cr) (Windows) f.fileno() → int: low level file descriptor (fd) whence : 0 from start, 1 from current, 2 from end O_NOINHERIT → xxxxxx (Windows) f.flush()® buffers written to file on disk mm.size()→ int: length of the real os file O_SHORT_LIVED → xxxxxx (Windows) f.isatty() → bool: indicator file is a terminal mm.tell() → int: current position in mmap file O_TEMPORARY → xxxxxx (Windows) f.read([size]) → str: block of data read from file mm.write(string)® data written to mmapfile1 O_RANDOM → xxxxxx (Windows) f.readline() → str: next line read from file, end of line removed mm.write_byte(byte)® str of one char (byte) data written to mmap file1 10a 10b 10c
    • 1 Terminal Operations File-like methods use and move file seek position. and as object with attributes ; # attribute usage os.openpty()→ (int{2}): open pseudo-terminal1 pair Files Informations 0 protection bits st_mode (fdmaster,fdslave)=(pty,tty) (Unix)> Functions to set/get files informations are in os and in os.path module, os.ttyname(fd)→ str: terminal device associated to fd (Unix) st_ino 1 inode number some in shutil module. Constants flags are defined in standard stat  os.isatty(fd)→ bool: test file descriptor is a tty-like (Unix) 2 device st_dev module. os.tcsetpgrp(fd,pg)® set process group id associted with terminal fd Some functions accessing process environment data (ex. current st_nlink 3 number of hard links (Unix) working directory) are documented in Process section. 4 user ID of owner st_uid os.tcgetpgrp(fd)→ int: process group associated with terminal fd (Unix) os.access(path,mode)→ bool: test for path access with mode using real st_gid 5 group ID of owner uid/gid - mode in F_OK, R_OK, W_OK, X_OK See also standard modules tty and pty. For user-interface control on 6 size of file, in bytes st_size os.F_OK → access mode to test path existence text terminal , see standard package curses and its sub-modules. st_atime 7 time of most recent access os.R_OK → access mode to test path readable os.W_OK → access mode to test path writable 8 time of most recent content modification st_mtime Temporary Files os.X_OK → access mode to test path executable st_ctime 9 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. number of blocks allocated for file (Unix) st_blocks constants 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 system - dir and prefix as for mkstemp type of device if an inode device (Unix) st_rdev symlinks(Unix) NamedTemporaryFile([mode='w+b'[,bufsize=-1[,suffix[,prefix[,dir]]]]]) st_rsize size of resource fork, in bytes(MacOS) os.fstat(fd)→ int: status for file descriptor → file/file-like: like TemporaryFile - file visible in file-system file creator code (MacOS) st_creator os.fstatvfs(fd)→ statvfs_result: informations about file system mkstemp([suffix[,prefix[,dir[,text]]]])→ (int,str): (fd,path) of new st_type file type code (MacOS) containing file descriptor (Unix) 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 as a tuple (use statvfs variable indexs) and as an object with follow symlinks - no race condition - only creator can read/write/search - not automatically attributes : os.stat_float_times([newvalue])→ bool: test/set stat function time deleted> # attribute index var usage gettempdir() → str: default directory for temporary files stamps data type - avoid setting new value 0 preferred file system block size f_bsize F_BSIZE os.statvfs(path)→ statvfs_result: informations about file system gettempprefix() → str: default filename prefix for temporary files f_frsize F_FRSIZE 1 fundamental file system block size containing path (Unix) Other functions in tempfile and os modules are kept for code os.utime(path,times)® set access and modification times of file path - 2 total number of blocks in the filesystem f_blocks F_BLOCKS compatibility, but are considered not enough secured. Also tempdir  times=(atime,mtime) (numbers) - times=None use current time f_bfree F_BFREE 3 total number of free blocks os.fpathconf(fd,name) → str / int: system configuration information and template data in tempfile - which should not be used directly. 4 free blocks available to non-super user f_bavail F_BAVAIL about file referenced by file descriptor - see platform documentation and Path Manipulations pathconf_names variable - name str or int (Unix) f_files F_FILES 5 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 f_favail F_FAVAIL 7 free nodes available to non-super user abspath(path)→ str: normalized absolutized pathname pathconf_names variable - name str or int (Unix) 8 flags - see host statvfs() man page f_flag F_FLAG basename(path)→ str: file name part of path os.pathconf_names → dict: name → index - names accepted by pathconf  commonprefix(pathlist) → str: longest common path prefix (char-by-char) and fpathconf → corresponding index on host (Unix) f_namemax F_NAMEMAX 9 maximum file name length dirname(path)→ str: directory name of pathname os.path.exists(path)→ bool: test existing path - no broken symlinks Stat Constants join(path[,…])→ str: concatenate path components os.path.lexists(path)→ bool: test existing path - allow broken symlinks Defined in standard stat module. normcase(path)→ str: normalize path case for platform (see doc) os.path.getatime(path)→ float_time: last access time of path S_ISUID → xxxxx normpath(path)→ str: normalize path (// /./ /../), on windows /→ os.path.getmtime(path)→ float_time: last modification time of path S_ISGID → xxxxx realpath(path)→ str: canonical path (remove symlinks) (unix) os.path.getctime(path)→ float_time: creation time (windows) or last S_ENFMT → xxxxx split(path)→ (str{2}): split into (head, last pathname component) modification time (unix) of path S_ISVTX → xxxxx os.path.getsize(path)→ int: bytes size of path file splitdrive(path)→ (str{2}): split into (drive, tail) S_IREAD → 00400 user can read os.path.isabs(path)→ bool: test absolute splitext(path)→ (str{2}): split into (root, ext) S_IWRITE → 00200 user can write os.path.isfile(path)→ bool: test regular file (follow symlinks) Host Specific Path Data S_IEXEC → 00100 user can execute os.path.isdir(path)→ bool: test existing directory (follow symlinks) sys.getfilesystemencoding() → <name of encoding used by system for S_IRWXU → 00700 user can read+write+execute os.path.islink(path)→ bool: test symlink filenames> S_IRUSR → 00400 user can read os.path.ismount(path)→ bool: test mount point Following data are in os and in os.path. S_IWUSR → 00200 user can write os.path.samefile(path1,path2)→ bool: test refer to same real file curdir → str: string used to refer to current directory S_IXUSR → 00100 user can execute (unix,macos) pardir → str: string used to refer to parent directory os.path.sameopenfile(f1,f2)→ bool: test opened files refer to same real S_IRWXG → 00070 group can read+write+execute sep → str: char used to separate pathname components file (unix,macos) S_IRGRP → 00040 group can read altsep → str: alternative char used to separate pathname components os.path.samestat(stat1,stat2)→ bool: test stat tuples refer to same file S_IWGRP → 00020 group can write extsep → str: char used to separate base filename from extension (unix,macos) S_IXGRP → 00010 group can execute shutil.copymode(srcpath,dstpath)® copy normal file permission bits pathsep → str: conventional char to separate different paths S_IRWXO → 00007 everybody can read+write+execute shutil.copystat(srcpath,dstpath)® copy normal file permission bits and Directories S_IROTH → 00004 everybody can read last access and modification times os.listdir(path)→ [str]/[unicode]: list names in path directory - S_IWOTH → 00002 everybody can write Stat Structures without . and .. - arbitrary order - path string type → item strings type S_IXOTH → 00001 everybody can execute stat_result is returned by stat and lstat functions, usable as a tuple os.mkdir(path[,mode=0777])® create directory path - mode masked out 11a 11b 11c
    • option - may raise NoSectionError codecs.BOM_UTF32 → str: alias for BOM_UTF32_LE or BOM_UTF32_BE with umask os.makedirs(path[,mode=0777])® create directory path, recursively - mode rp.remove_section(section)→ bool: return True if there was such section codecs.BOM_UTF32_BE → str: 'x00x00xfexff' masked out with umask - don't handle Windows' UNC path rp.optionxform(option)→ str: normalized internal form of option codecs.BOM_UTF32_LE → str: 'xffxfex00x00' os.rmdir(path)® remove directory path See Encoding - Decoding (p13) for details about encoding and errors. Normal Interface os.removedirs(path)® remove directories, recursively Serialization cp.get(section,option[,raw[,vars]])→string: value for option in section - %  os.walk(top[,topdown=True [,onerror=None]])→ iterable: go throught dirs Standard modules pickle and cPickle (speed up to 1000x) have interpolation expanded unless raw is true - vars is a dict of additional defaults under top, for each dir yield tuple(dirpath, dirnames, filenames) - - reference expansion names are processed by optionxform() for matching support for data serialization of objects hierarchies. onerror=fct(os.error) - see docs os.path.walk(path,visit,arg) ® call visit(arg,dirname,names) for dirs rooted See Python documentation. cp.items(section[,raw[,vars]])→ [(name,value)]: for given section - raw See also module marshal (read/write of Python data in platform at path - may modify names (files list) to influence walk, may prefer to use and vars as in get() os.walk independant binary format - but can broke format between releases). Safe Interface Special Files Persistence sp.set(section,option,value) ® set value string for section and option os.link(src,dst)® create hard link named dst referencing src (Unix) Standard module shelve use pickling protocol to store objects in DBM Exceptions os.symlink(src,dst)® create symbolic link named dst pointing to src (Unix) files (see p17) and access them via a dictionnary-like interface with (Exception) keys as str. os.readlink(path)→ str: path pointed to by symbolic link Error open(filename[,flag[,protocol[,writeback[,binary]]]])→  os.mknod(path[,mode=0666,device])® create FS node (file, device special dictionary-like object ParsingError - flag as anydbm.open (p17), default to 'c' - protocol default to 0 (ascii file, named pipe) - mode = permissions | nodetype - node type in S_IFREG, NoSectionError format) - writeback: cache accessed entries in memory and written them back S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO defined in stat module DuplicateSectionError at close time, default to False - binary is deprecated, use protocol. os.major(device) → int: raw device major number MissingSectionHeaderError Configuration Files os.minor(device) → int: raw device minor number NoOptionError Standard module ConfigParser. It uses standard .INI files to store os.makedev(major,minor)® compose raw device from major and minor InterpolationError configudation data : InterpolationDepthError numbers> [section] Values can contain %(name)s references which InterpolationMissingOptionError Copying, Moving, Removing name:value may be expanded using values in same section or InterpolationSyntaxError os.remove(path)® remove file path (not directory) in defaults name=value os.rename(src,dst)® rename src to dst - on same filesystem- may remove # and ; start comment lines. For similar file format supporting nested subsections, see ConfigObj  existing dst file The module defines three configuration classes with different data config parser. For windows users, standard module _winreg. os.renames(old,new)® rename old to new, recursively - try to create access level : For text-file configs, can use XML tools, and see also third party YAML intermediate directories RawConfigParser parsers like PyYaml. os.unlink(path)® remove file path (not directory) - same as remove ConfigParser SafeConfigParser EXCEPTIONS Standard module shutil provide high level functions on files and rp=RawConfigParser([defaults]) → RawConfigParser directories. Standard exceptions defined in exceptions module, and available in cp=ConfigParser([defaults]) → ConfigParser copyfile(src,dst)® copy normal file content - overwrite destination2. current scope. sp=SafeConfigParser([defaults]) → SafeConfigParser copyfileobj(fsrc,fdst[,length=16kb])® copy file-like object content by All exceptions must be subclasses of Exception root class. In the three constructors, defaults is a dict of option:value for blocks of length size (<0=one chunk) Use standard exceptions if their meaning correspond to you errors. copy(src,dst)® copy normal file content to file/directory2 - in case of references expansion. Subclass standard exceptions when needed. MAX_INTERPOLATION_DEPTH → int: max recursive depth for get() when raw directory use same basename as src - overwrite destination - copy permission Standard Exception Classes bits. parameter is false copy2(src,dst)® same as copy + copy last access and modification times2. Exception DEFAULTSECT → str: name of defaut section copytree(src,dst[,symlinks=False])® recursively copy directory tree - StopIteration ― iterator's next(), no more value. Raw Interface SystemExit ― sys.exit() called destination must be new - files copied via copy - if symlinks is False, copy rp.defaults()→dict: default values for references expansion StandardError ― built-in exceptions symbolic links files content, else just make symbolic links. 1 rmtree(path[,ignore_errors=False[,onerror=None]])® recursively delete ArithmeticError ― arithmetic errors. rp.sections()→[string]: list sections in config (without DEFAULT) FloatingPointError directory tree - onerror=fct(fctref, path, excinfo).1 rp.add_section(section) ®add a new section - may raise move(src,dst)® recursively move file or directory tree - may rename or copy.1 OverflowError DuplicateSectionError ZeroDivisionError rp.has_section(section)→bool: test if section exists - cant test for DEFAULT May raise shutil.Error exception. 1 AssertionError ― assert cond[,message] fails. rp.options(section)→[string]: list options in section 2 Params src and dst are files path names. AttributeError ― attribute set/get fail. rp.has_option(section,option)→bool: test if section and option exists Encoded Files EnvironmentError ― host system error - see arg tuple attribute rp.read([filename]/filename)→[filename]: try to load configuration data from Standard module codecs have functions and objects to transparently IOError files (continue if fail) - return names of loaded files process encoded files (used internally as unicode files). OSError rp.readfp(fp[,filename]) ® load configuration data from file/file-like codecs.open(filename,mode[,encoding[,errors[,buffering]]])→ <file-like WindowsError ― Windows error codes. rp.get(section,option)→str: option value EncodedFile object with transparent encoding/decoding> EOFError ― end-of-file with input() or raw_input(). rp.getint(section,option)→int: coerce option value to int codecs.EncodedFile(file,input[,output[,errors]]) → <file-like wrapper ImportError rp.getfloat(section,option)→float: coerce option value to float around file, decode from input encoding and encode to output encoding> KeyboardInterrupt ― user interrupt (Ctrl-C). rp.getboolean(section,option)→bool: coerce option value to bool - True is codecs.BOM → str: alias for BOM_UTF16 LookupError strings 1 yes true on - False is strings 0 no false off - may raise codecs.BOM_BE → str: alias for BOM_UTF16_BE IndexError ― non-existent sequence index. ValueError codecs.BOM_LE → str: alias for BOM_UTF16_LE KeyError ― non-existent mapping key. rp.items(section)→[(name,value)]: options in the section codecs.BOM_UTF8 → str: 'xefxbbxbf' MemoryError rp.set(section,option,value) ® set option to string value in section - may NameError ― non-existent name in current scope. codecs.BOM_UTF16 → str: alias for BOM_UTF16_LE or BOM_UTF16_BE raise NoSectionError UnboundLocalError ― reference to an unassigned local variable. codecs.BOM_UTF16_BE → str: 'xfexff' rp.write(fileobject)® write configuration data to file ReferenceError ― try accessing weak-ref disposed object. codecs.BOM_UTF16_LE → str: 'xffxfe' rp.remove_option(section,option)→ bool: return True if there was such RuntimeError ― (prefer defining ad-hoc subclasses). 12a 12b 12c
    • NotImplementedError activeCount()→ int: number of currently active threads evt.isSet()→ bool: value of event internal flag SyntaxError currentThread()→ Thread: current running thread evt.set() ® set event internal flag to true - unlock waiting threads IndentationError enumerate()→ [Thread]: list of active threads evt.clear() ® set event internal flag to False TabError settrace(func) ® install trace function called before threads run methods evt.wait([timeout]) ® wait for event internal flag to be true - timeout is a SystemError ― a bug… in Python. setprofile(func) ® install profile function called before threads run float_delay (default to None=infinite blocking) TypeError Semaphores methods ValueError ― good type, but bad value. Standard module thread supports low level thread management. Classes threading.Semaphore and threading.BoundedSemaphore  UnicodeError Use modules dummy_thread and dummy_threading on platforms provide simple semaphore for resources counting (without/with counter Warning ― warnings superclass (see Warnings infra) without multithreading. checking). UserWarning sem = threading.Semaphore([value=1]) ® semaphore with initial counter Threads PendingDeprecationWarning sem = threading.BoundedSemaphore([value]) Class threading.Thread is used to create new execution path in current DeprecationWarning sem.acquire([blocking])→ bool/None: acquire the semaphore (consume one SyntaxWarning process. It must be called with keyword arguments. Specify thread code RuntimeWarning with a callable target param or by overriding run method (remember resource). blocking unspecified : wait & return None ; blocking true : wait & return True ; blocking false : don't wait (try) & return True/False calling inherited __init__ in subclasses), give arguments in args and sem.release() ® release the semaphore (free one resource) kwargs (tuple and dict), give a name to identify the thread - group Warnings currently not used (None). Condition Variables Warnings must be subclasses of Warning root class. th = threading.Thread(group,target,name,args,kwargs) Class threading.Condition allows threads to share state (data) Standard warnings module control processing of warning exceptions. th.start() ® start thread activity (in another thread) protected via a Lock. Important : condition variables (lock) must be warn(message[,category[,stacklevel]]) acquired when calling wait, notify or notifyAll. See Python docs. th.run() ® thread code to execute - call target if not overriden warn_explicit(message,category,filename,lineno[,module[,registry]]) cond = threading.Condition([lock]) ® build new condition variable, use th.join([timeout]) ® wait for th termination or timeout elapsed (float_delay, showwarning(message,category,filename,lineno[,file]) default to None for infinite) user providen lock (Lock or RLock) else build a new RLock formatwarning(message,category,filename,lineno) th.getName()→ str: thread associated name cond.acquire(*args)→ value: acquire cond. var. lock, return lock.acquire() filterwarnings(action[,message[,category[,module[,lineno[,append]]]]]) th.setName(name) ® set thread associated name (initial name set by class) value resetwarnings() th.isAlive()→ bool: test thread alive (started and run() not terminated) cond.release() ® release cond. var. lock sys.warnoptions th.isDaemon()→ bool: test thread have daemon flag cond.wait([timeout]) ® wait until notified or timeout elapsed- timeout is a Exceptions Processing float_delay (default to None=infinite blocking). Release cond. var. lock and wait th.setDaemon(daemonic) ® set thread daemon flag - must be called before for a notification/timeout then re-acquire lock. sys.exc_info()→ (type,value,traceback) for current exception1> start. Initial flag inherited from creating thread. Python process exit only after cond.notify() ® wake up one waiting thread (if any). last non-daemon thread. sys.exc_clear() ® current exception related informations cleared cond.notifyAll() ® wake up all waiting threads. A thread can't be killed or paused externally by another thread. sys.excepthook → (rw) fct(type, value, traceback) called for uncaught Synchronized Queues exceptions Thread Local Storage sys.__excepthook__ → backup of original excepthook function Module Queue provides a class Queue to store data in a synchronized Class threading.local attributes values are thread local. sys.tracebacklimit → int: (rw) maximum levels of traceback printed, <=0 FIFO queue, and two exception classes Full and Empty. In blocking Subclass it or use it as a namespace. for none mode, full queue block producers and empty queue block consumers (in tlsdata = threading.local() 1 Or (None,None,None) if no running exception. non-blocking mode they raise exceptions). Other organization can be tlsdata.x = 1 Standard module traceback has tools to process and format these built with subclassing (see source for internal methods). Delayed Start Thread informations. q = queue.Queue(maxsize) ® build new queue - infinite queue if maxsize<=0 Class threading.Timer is a subclass of Thread which effectively run q.qsize()→ int: size of the queue - at call time ENCODING - DECODING after a specified interval from its start. q.empty()→ bool: test if queue size if 0 - at call time See also Unicode strings (p5), Source encodings (p3), t = threading.Timer(interval,function,args=[],kwargs={}) q.full()→ bool: test if queue size is maxsize - at call time Standard module codecs provide base support for encoding / decoding t.cancel() ® timer will never run - must not be already running q.put(item[,block[,timeout]])® put in queue - block can be true/false, data. This is used for character encodings, but also for data Create a timer that will run function with arguments args and keyword Timeout can be None/float_delay. May raise Queue.Full exception. compression (zip, bz2) or data representation (uu, hex). arguments kwargs, after interval seconds have passed. q.put_nowait(item)® same as put(item,False) Mutual Exclusion See functions, classes and constants for files encoding in Encoded Files q.get([block[,timeout]])→ item: removed from queue - block can be true/false, Classes threading.Lock and threading.RLock provide mutual (p12). Timeout can be None/float_delay - may raise Queue.Empty exception Module encodings.aliases. exclusion between threads. Lock doesn't allow a thread to re-acquire a q.get_nowait()® same as get(False) lock it already owns, RLock does (reentrant-lock). PROCESS lock = threading.Lock() THREADS & SYNCHRONIZATION lock = threading.RLock() Current Process Python threads use native threads. A global mutex (the GIL) lock lock.acquire([blocking])→ bool/None: acquire the lock. blocking Standard module os has tools to get information about and manipulate interpreter data during Python virtual instructions execution (it is unspecified : wait & return None ; blocking true : wait & return True ; blocking current process and its environment. unlocked during I/O or long computation in native code). Check for false : don't wait (try) & return True/False thread switching and signal processing is performed at regular interval. Exiting lock.release()® unlock a previously acquired lock sys.getcheckinterval()→ int: current thread switching check interval1 Normally Python process exit when there is no more non-daemon thread Must release a lock same times as it was acquired. sys.setcheckinterval(interval) ® set thread switching check interval1 running. Good practice to acquire/release locks in try/finally blocks. 1 sys.exit([arg=0])® exit via a SystemExit exception (may be catch) - arg is Expressed in number of Python virtual instructions. For portable inter-process mutex, see third party glock.py module. exit code Threading Functions Events os._exit(n)® exit without cleanup Use standard high level module threading which provides several Class threading.Event is a synchronisation flag with thread blocking os.abort()® exit via a SIGABRT signal (signal may be handled) classes : Thread, local (for thread local storage), Event, Lock and mechanism to wait for the flag. Following exit codes are defined in os (Unix) : RLock (mutexs), Semaphore and BoudedSemaphore, Timer. evt = threading.Event() ®new event, with internal flag set to False Module threading also provides functions : EX_OK no error 13a 13b 13c
    • EX_USAGE getpgrp()→ int: current gid (Unix) SIG_IGN → 1: ignore signal handler function command used incorrectly getgroups()→ [int]: list of supplemental associated gid (Unix) NSIG → int: highest signal number +1 EX_DATAERR incorrect input data setgroups(groups)® set list of supplemental associated gid (Unix) EX_NOINPUT unavailable/inaccessible input Module also defines signal numbers (Posix examples - runtime definition setpgrp()® call system function1 (Unix) EX_NOUSER unknown user is platform dependant) : SIGHUP getppid()→ int: parent's pid (Unix) EX_NOHOST unknown host terminal or control processus disconnection SIGINT setsid()® call system function1 (Unix) EX_UNAVAILABLE required service unavailable keyboard interrupt SIGQUIT getpgid(pid)→ int: process group id of process id pid (0=current) (Unix) EX_SOFTWARE internal error quit request from keyboard SIGILL getsid(pid)® call system function1 (Unix) EX_OSERR OS error illegal instruction SIGABRT setpgid(pid,pgrp)® set process pid group to pgrp1 (Unix) EX_OSFILE missing/inaccessible file abort stop signal SIGFPE EX_CANTCREAT can't create output floating point error 1 See manual for semantics. SIGKILL EX_IOERR error during file I/O the KILL signal Timings, Priority SIGSEGV EX_TEMPFAIL temporary failure invalid memory reference times()→ (ut, st, cut, cst, ert): float_delay: user time, system time, children's SIGPIPE EX_PROTOCOL illegal/invalid/not understood protocol exchange pipe write without reader user time, children's system time, elapsed real time> SIGALRM EX_NOPERM not enough permissions (out of file perms) alarm timer elapsed nice(increment)→ int: renice process - return new niceness (Unix) SIGTERM EX_CONFIG configuration problem termination signal Memory SIGUSR1 EX_NOTFOUND missing data user signal 1 plock(op)® lock program segments into memory - see <sys/lock.h> for op SIGUSR2 user signal 2 values (Unix) You can install exit functions (for normal exit) with module atexit. SIGCHLD terminated/stopped child Host Informations register(func[,*args[,**kargs]])® register function to be called with args and SIGCONT continue process (if stopped) strerror(code)→ str: error message for the error code kargs SIGSTOP stop process Registered functions are called in reverse order of registration. uname()→ tuple: current operating system identification, (sysname, nodename, SIGTSTP stop request from keyboard Bypassed when process is terminated by a signal, an internal error, or an release, version, machine) (recent Unix) SIGTTIN read on tty while in background os._exit. sys.byteorder → str: host native byte order big or little SIGTTOU write on tty while in background Environment Variables sys.winver → str: version number for registry keys (Windows) … → see your platform documentation (man 7 signal on Linux). environ →<dict: environment variables - modification call putenv if sys.platform → str: platform identifier (ex. linux2) Functions to send signals are in os module : supported> Following data are in os and in os.path. kill(pid,sig)® kill process pid with signal sig (Unix) getenv(varname[,default=None])→ str: environment variable value defpath → str: search path for os.exec*p*() and os.spawn*p*() if killpg(pgid,sig)® kill process group pgid with signal sig (Unix) putenv(varname,value)® set environment variable - affect later started environment PATH not defined subprocess - may cause memory leaks (see platform documentation) Simple External Process Control linesep → str: end of line char(s) for the plaftorm Some functions also in os.path : Use standard module subprocess. It wraps external process creation devnull → str: file path of null device expanduser(path)→ str: path with initial quot;~quot; or quot;~userquot; replaced and control in Popen objects. Child process exceptions raised before Python Informations execution are re-raised in parent process, exceptions will have expandvars(string)→ str: string with $name or ${name} environment child_traceback attribute (string). sys.builtin_module_names → (str): names of modules compiled into variable replaced Note : subprocess tools will never call /bin/sh implicitly. interpreter Directory, Files, Terminal sys.copyright → str: copyright of interpreter PIPE →­1: constant value used for Popen stdin stdout stderr params See also Console & Interactive Input/Output (p2), and Files - Terminal sys.hexversion → int: Python version with one digit by byte Operations (p11). call(*args,**kwargs)→ int: run command with arguments, wait for sys.version → str: interpreter version + build + compiler chdir(path)® change current working directory to path completion, return retcode - convenient wrapper around Popen object sys.dllhandle → int: handle of Python DLL (Windows) fchdir(fd)® change current working directory to thus represented by file Use Popen objects as process control tools : sys.executable → str: name of interpreter executable binary descriptor p = Popen(args,bufsize=0,executable=None,stdin=None,stdout=None,  sys.prefix → str: directory prefix for platform independant Python files getcwd()→ str: current working directory stderr=None,preexec_fn=None,close_fds=False,shell=False,cwd=None,  sys.api_version → int: version of Python C API getcwdu()→ unicode: current working directory env=None,universal_newlines=False,startupinfo=None,creationflags=0) sys.version_info → (int{3},str,int): (major, minor, micro, releaselevel, chroot(path)® change process file-system root to path (Unix) args is a string/list of strings [quot;commandquot;,quot;arg1quot;,quot;arg2quot;,…] serial) - release in alpha, beta, candidate, final umask(mask)→ int: set current numeric umask and return previous one bufsize like for file/open functions ctermid()→ str: filename of controlling terminal (Unix) Signal Handling executable can be used to provide command in place of args[0] getlogin()→ str: name of user logged on controlling terminal (Unix) Standard module signal. See doc for general rules about signals usage stdin, stdout and stderr can be PIPE to capture file and communicate in Python. with subprocess User, process, group IDs preexec_fn is called just before child process execution Signal handlers are callable f(signalnum,stackframe). pid: process id, gid: group id, uid: user id close_fds bool force subprocess inherited files to be closed, except 0 getpid()→ int: current pid alarm(time)→ float_delay: previous alarm remaining time - request a new 1 and 2 getegid()→ int: effective gid (Unix) SIGALRM in time seconds - cancel previous one - time≠0 (Unix) shell bool force execution of command throught the shell alarm(0)→ float_delay: previous alarm remaining time - cancel previous alarm setegid(egid)® set process effective gid (Unix) cwd string specify working directory to set for subprocess start (Unix) geteuid()→ int: effective uid (Unix) env dictionnary specify environment variables for subprocess getsignal(signalnum)→ fct: current signal handler or SIG_IGN or SIG_DFL or universal_newlines translate all newlines to n (like U mode for files) seteuid(euid)® set process effective uid (Unix) None (handler not installed from Python) startupinfo and creationflags are optional informations for process getgid()→ int: real gid (Unix) pause()® sleep process until a signal is received (Unix) creation under Windows setgid(gid)® set process gid (Unix) signal(signalnum,handler)→ fct: previous handler for signal (as getsignal) p.poll()→ int/None: check child process termination, return returncode getuid()→ int: current process' uid (Unix) - install new handler (maybe SIG_IGN or SIG_DFL too) - only callable in main attribute thread setuid(uid)® set process current uid (Unix) p.wait()→ int: wait for child process to terminate, return returncode Following signal constants are defined : setregid(rgid,egid)® set process real and effective gid (Unix) attribute> SIG_DFL → 0: default signal handler function p.communicate(input=None)→ (stdout,stderr): send data (input string)to setreuid(ruid,euid)® set process real and effective uid (Unix) 14a 14b 14c
    • WIFSIGNALED(status)→ bool: test exited on signal (Unix) stdin, read data from stdout/stderr until end-of-file, wait process to terminate, p.setFeature(featurename,value) ® set feature to value return read values - data read is buffered in memory WIFEXITED(status)→ bool: test process exited via exit(2) system call (Unix) p.getProperty(propertyname) → current settings for property2 p.stdin → file/None: standard input from chil process if captured WEXITSTATUS(status)→ int: if exited via exit(2), return exit parameter (Unix) p.setProperty(propertyname,value) ® set property to value p.stdout → file/None: standard output from chil process if captured WSTOPSIG(status)→ int: signal having stopped process (Unix) There is also an IncrementalParser subclass interface with : p.stderr → file/None: error output from chil process if captured WTERMSIG(status)→ int: signal having exited process (Unix) p.feed(data) ® process a chunk of data p.pid → int: process ID of child process Pipes On Process p.close() ® assume end of document, check well-formedness, cleanup p.returncode → int/None: child process return code (None if not terminated) p.reset() ® after close, prepare new parsing Three functions available in popen2 module (and in os module where - on Unix -N for subprocess terminated by signal N stdin/stdout return values are inverted). Use subprocess module when possible (cleaner, simpler interface, see Feature names in xml.sax.handler as feature_xxx. 1 popen2(cmd[,bufsize[,mode]])→ (file{2}): (stdout,stdin): execute cmd as docs for examples). See also external module pexpect. Property names in xml.sax.handler as property_xxx. 2 sub-process Advanced External Process Control InputSource Interface popen3(cmd[,bufsize[,mode]])→ (file{3}): (stdout,stdin,stderr): execute cmd See following functions from os module. Provide source of data for parser. as sub-process popen4(cmd[,bufsize[,mode]])→ (file{2}): stdout_stderr,stdin): execute cmd execl(path,[arg[,…]]) isrc.setPublicId(id) ® set public identifier as sub-process execle(path,[arg[,…]],env) 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 On Unix, popen2 module also defines Popen3 class (used in popen2 and stream execvp(file,args) isrc.getEncoding() → str/None (if unknown) popen3 functions) and Popen4 class (used in popen4 function) : 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 Popen4(cmd[,bufsize])→ Popen4 isrc.getByteStream() → byte stream 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 XML PROCESSING With spawn… new process is created. 'lpev' versions like for exec…. Also implement parts mapping protocol (copy(), get(), has_key(), If mode is P_NOWAIT or P_NOWAIT0, return child pid (Unix) or process items(), keys(), and values()). Several modules to process XML are available. Some with standard SAX handle (Windows). If mode is P_WAIT, wait child termination and return ai.getLength() → int: number of attributes and DOM interfaces, others with more Pythonic interfaces. 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 parse(filename_or_stream,content_handler[,error_handler]) ® parse Also implement Attributes interface. is 0 in child, child pid in parent - fd pseudo-terminal master end (Unix) 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 qualified name (pid/status) (Unix) or status (Windows) XMLReader Interface ansi.getQNameByName(namepair)→ unicode: qualified name for (namespace, wait()→ (int{2}): (pid,status) wait completion of a child process (Unix) - localname) Defined in xml.sax.xmlreader. status=0xZZTT where ZZ=exit code, TT=signal num ansi.getQNames()→ [unicode]: qualified names of all attributes waitpid(pid,options)→ (int{2}): (pid,status) (Unix): p = xml.sax.make_parser() → XMLReader object ContentHandler Interface pid>0 wait for specific process, p.parse(source) ® completly parse source - source is filename or URL or file- 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, p.getContentHandler() → ContentHandler: current one parser find XML structures. pid<-1 wait for any process in process group -pid option in WNOHANG, WCONTINUED, WUNTRACED p.setContentHandler(handler) ® set current content handler ch = MyContentHandler() → ContentHandler subclass object p.getDTDHandler() → DTDHandler: current one status=0xZZTT where ZZ=exit code, TT=signal num 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 p.getErrorHandler() → ErrorHandler: current one mapping - see doc WCOREDUMP(status)→ bool: test process generated core-dump (Unix) ch.endPrefixMapping(prefix) ® end of a prefix-URI namespace mapping p.setErrorHandler(handler) ® set current error handler WIFCONTINUED(status)→ bool: test process continued from a job control stop ch.startElement(name,attrs) ® start of an element - non-namespace mode p.setLocale(locale) ® set locale for errors and warnings (Unix) - attrs has an Attributes interface (may be reused - copy data) WIFSTOPPED(status)→ bool: test process stopped (Unix) p.getFeature(featurename) → current settings for feature1 15a 15b 15c
    • Features & Properties o.hasChildNodes() → bool: test any subnode existence ch.endElement(name) ® end of an element - non-namespace mode o.isSameNode(other) → bool: test other refers same node Defined in xml.sax.handler. Dont give their value, but their meaning. ch.startElementNS(name,qname,attrs) ® start of an element - namespace mode - name is (uri,localname) - qname is raw XML name - attrs has an o.appendChild(newChild) → new Child: add new child node at end of feature_namespaces : True → perform namespace processing. False → no 1 AttributesNS interface (may be reused - copy data) - qname may be None  subnodes - return new child namespace processing (so no namespace prefixes). (upon feature_namespace_prefixes) o.insertBefore(newChild,refChild) → new Child: add new child node before feature_namespace_prefixes : 1 True → report original prefixed names ch.endElementNS(name,qname) ® end of an element - namespace mode an existing subnode - at end of subnodes if refChild is None - return new child and attributes used for namespace declarations. ch.characters(content) ® character data - content is str or Unicode o.removeChild(oldChild) → old Child: remove a subnode, return it - when no feature_string_interning : 1 True → intern all names (elements, prefixes, ch.ignorableWhitespace(whitespace) ® whitespaces longer used, must call oldChild.unlink() attributes, namespace URIs, local names). ch.processingInstruction(target,data) ® processing instruction feature_validation : 1 True → report all validation errors. o.replaceChild(newChild,oldChild) ® replace existing subnode with a new ch.skippedEntity(name) ® entity not processed one feature_external_ges : 1 True → include all external general (text) entities. o.normalize() ® join adjacent text nodes DTDHandler Interface feature_external_pes : 1 True → iInclude all external parameter entities, o.cloneNode(deep) → Node: if deep, clone subnodes too - return clone including the external DTD subset. Defined in xml.sax.handler. Its methods are handlers called when all_features → list of all features NodeList parser need DTD relative work. property_lexical_handler : optional extension handler for lexical events dh = MyDTDHandler() → DTDHandler subclass object A sequence of nodes, usable as a Python sequence (maybe modifiable (like comments). upon implementation). dh.notationDecl(name,publicId,systemId) ® notation declaration property_declaration_handler : optional extension handler for DTD- o.length → int: number of nodes in the sequence dh.unparsedEntityDecl(name,publicId,systemId,ndata) ® unparsed related events other than notations and unparsed entities. o.item(i) → Node/None: ith item in the list entity declaration property_dom_node : 1 visited DOM node (if DOM iterator) when parsing, else EntityResolver Interface DocumentType root DOM node. property_xml_string : literal string source of current event (read only Defined in xml.sax.handler. Its methods are handlers called when Subclass of Node. property). o.nodeType → DOCUMENT_TYPE_NODE parser need external entity resolution. all_properties → list of all properties names er = MyEntityResolver() → EntityResolver interface object o.publicId → unicode/None: public identifier for external subset of DTD> 1 can only be read during parsing (and modified before). er.resolveEntity(publicId,systemId) → str/InputSource: default return o.systemId → unicode/None: system identifier URI for external subset of systemId DOM - In-memory Tree DTD o.internalSubset → unicode/None: complete internal subset from the Exceptions Defined in xml.dom. Two function to register/access DOM processors, document - without brackets and some constants. Defined in xml.sax module. o.name → unicode/None: name of root element (as given in DOCTYPE) registerDOMImplementation(name,factory) ® register DOM SAXException(msg[,exception]) o.entities → NamedNodeMap/None: definition of external entities implementation factory SAXParseException(msg,exception,locator) ― invalid XML getDOMImplementation([name[,features]])→ DOM implementation - name o.notations → NamedNodeMap/None: definition of notations SAXNotRecognizedException(msg[,exception]) may be None - may found name in env. var PYTHON_DOM - features is Document SAXNotSupportedException(msg[,exception]) [(featurename,version),…] Subclass of Node. EMPTY_NAMESPACE → no namespace associated with a node ErrorHandler Interface o.nodeType → DOCUMENT_NODE XML_NAMESPACE → xml prefix namespace Defined in xml.sax.handler. Its methods are handlers called when o.documentElement → Element: root element of the document XMLNS_NAMESPACE → namespace URI for namespace declarations - DOM level 2 parser detect an error. Their exception parameters get o.createElement(tagName)→ Element: new1 element node> specification definition SAXParseException objects. o.createElementNS(namespaceURI,tagName)→ Element: new1 element XHTML_NAMESPACE → URI of XHTML namespace (XHTML 1.0) eh = MyErroHandler() → ErrorHandler interface object node with namespace - tagName may have prefix DOMImplementation eh.error(exception) ® recovererable error - parsing will continue if method o.createTextNode(data) → Element: new1 text node containing data return impl.hasFeature(feature,version) → bool: test for supported feature in an o.createComment(data) → Element: new1 comment node containing data eh.fatalError(exception) ® unrecoverable error - parsing must stop implementation o.createProcessingInstruction(target,data) → Element: new1 eh.warning(exception) ® minor warning - parsing will continue if method Node processing instruction node containing target and data return o.createAttribute(name) → Element: new1 attribute node Defined in xml.dom, class Node is parent of XML components nodes SAX Utilities o.createAttributeNS(namespaceURI,qualifiedName) → Element: new1 classes. Defined in xml.sax.saxutils. attribute node with namespace- tagName may have prefix> o.nodeType → int: (ro) in ELEMENT_NODE, ATTRIBUTE_NODE, TEXT_NODE, o.getElementsByTagName(tagName) → NodeList: search for all escape(data[,entities]) → str: & < > escaped - escape other entities replacing CDATA_SECTION_NODE, ENTITY_NODE, PROCESSING_INSTRUCTION_NODE, descendants (deep search) having type name mapping strings (keys) by corresponding identifiers COMMENT_NODE, DOCUMENT_NODE, DOCUMENT_TYPE_NODE, NOTATION_NODE o.getElementsByTagNameNS(namespaceURI,localName) → NodeList: unescape(data[,entities]) → str: &amp; &lt; &gt; unescaped - unescape o.parentNode → Node/None: (ro) - None for Attr nodes search for all descendants (deep search) having namespace URI and local name other entities replacing mapping identifiers (keys) by corresponding strings (part after prefix) o.attributes → NamedNodeMap/None: attribute objects for elements, else quoteattr(data[,entities]) → str: as escape() + quote string to be used as None 1 New nodes are standalone - you must insert/associate them in/to attribute value o.previousSibling → Node/None: (ro) previous node in parent's children document parts. prepare_input_source(source[,base]) → InputSource: source is string, o.nextSibling → Node/None: (ro) next node in parent's children file-like, or InputSource - base is an URL - return InputSource for parser Element o.childNodes → [Node]: (ro) list of subnodes Class XMLGenerator is a ContentHandler writing SAX events into an Subclass of Node. o.firstChild → Node/None: (ro) first subnode XML document (ie. reproduce original document). o.nodeType → ELEMENT_NODE XMLGenerator([out[,encoding]]) → content handler: out file-like, deault to o.lastChild → Node/None: (ro) last subnode o.tagName → unicode: element type name - with namespace may contain sys.stdout - encoding default to 'iso­8859­1' o.localName → unicode/None: (ro) element name without namespace prefix colons o.prefix → unicode/None: (ro) element namespace prefix - may be empty o.getElementsByTagName(tagName) → NodeList: search for all Class XMLFilterBase is a default pass-throught events, can be string or None descendants (deep search) having type name subclassed to modify events on-fly before their processing by o.namespaceURI → unicode/None: (ro) URI associated to element namespace o.getElementsByTagNameNS(namespaceURI,localName) → NodeList: application handlers. o.nodeName → unicode/None: (ro) usage specified in subclasses search for all descendants (deep search) having namespace URI and local name XMLFilterBase(base) → events filter (part after prefix) o.nodeValue → unicode/None: (ro) usage specified in subclasses o.getAttribute(attname)→ unicode: attribute value o.hasAttributes() → bool: test any attribute existence 16a 16b 16c
    • DOM codes constants Exception value params example o.getAttributeNode(attrname)→ Attr: attribute node Question mark style1 …WHERE name=? NamespaceErr 'qmark' NAMESPACE_ERR  o.getAttributeNS(namespaceURI,localName)→ unicode: attribute value Numeric, positional style1 or 2 …WHERE name=:1 'numeric' NotFoundErr NOT_FOUND_ERR  o.getAttributeNodeNS(namespaceURI,localName)→ Attr: attribute node Named style2 …WHERE name=:name 'named' NotSupportedErr NOT_SUPPORTED_ERR  o.removeAttribute(attname) ® remove attribute by name - ignore missing ANSI C printf format codes1 …WHERE name=%s 'format' attribute NoDataAllowedErr NO_DATA_ALLOWED_ERR  Python extended format codes2 …WHERE name=%(name)s 'pyformat o.removeAttributeNode(oldAttr)→ Attr: remove and return old Attr NoModificationAllowedErr NO_MODIFICATION_ALLOWED_ERR  1 Parameters as positional values in a sequence. o.removeAttributeNS(namespaceURI,localName) ® remove attribute by SyntaxErr SYNTAX_ERR  2 Parameters as named values in a map. namespace URI and name - ignore missing attribute WrongDocumentErr WRONG_DOCUMENT_ERR  Exceptions o.setAttribute(attname,value) ® set attribute string value o.setAttributeNode(newAttr)→ Attr: set attribute from a new Attr node exception.code → int: DOM code corresponding to exception (StandardError) - return old one Warning ― important warning exception.msg → string: message for exception o.setAttributeNodeNS(newAttr)→ Attr: set attribute from a new Attr  Error ― a catch all DOMException node with namespace URI and local name - return old one InterfaceError ― problem with interface (not database) DomstringSizeErr ― implementation limit reach o.setAttributeNS(namespaceURI,qname,value)→ Attr: set attribute DatabaseError HierarchyRequestErr ― insert at wrong place string value from a namespace URI and qname (whole attribute name) - return DataError ― problem with data processing IndexSizeErr ― index range error old one OperationalError ― problem during database operations InuseAttributeErr ― Attr node already used in tree Attr IntegrityError InvalidAccessErr ― param/operation unsupported by object Subclass of Node. InternalError InvalidCharacterErr ― character invalid in the context o.nodeType → ATTRIBUTE_NODE ProgrammingError ― SQL programming related error InvalidModificationErr ― can't modify node type o.name → unicode: (ro) attribute full name - may have colons NotSupportedError InvalidStateErr ― try to use an undefined/unusable object Exceptions classes may also be available as Connection objects o.localName → unicode: (ro) attribute name - part after colons NamespaceErr ― change forbidden in namespace context attributes (optional). o.prefix → unicode: (ro) attribute prefix - part before colons - may be empty NotFoundErr ― node don't exist in referenced context Connection NotSupportedErr ― operation/type unsupported by implementation NamedNodeMap NoDataAllowedErr ― no data for this node connect(dsn[,user[,password[,host[,database]]]])→ Connection object A mapping of nodes - experimentally usable as a Python mapping. NoModificationAllowedErr ― can't modify object (interface defined as a guideline) - dsn=data source name string> o.length → int: length of attributes list SyntaxErr ― invalide/illegal string cx.errorhandler → fct: (optional) handler for connection errors - o.item(index) → Attr: attribute at index - arbitrary but consistent order WrongDocumentErr ― impl. can't migrate nodes between docs errorhandler(connection, cursor/None, errorclass, errorvalue) - default handler Comment fill cx.messages and may raise exceptions DATABASES cx.messages → [(exception class,exception value)]: (optional) messages Subclass of Node. Cannot have subnode. received from database for operations with connection See Python.org wiki for a list of database interface modules. o.nodeType → COMMENT_NODE Some interfaces are for external DB engines (MySQL, PostgreSQL, cx.close()® terminate connection (may rollback if not commited) o.data → unicode: content of the comment, without <!­­ and ­­> BerkeleyDB, SQLite, Metakit…), other for pure Python DB engines cx.commit()® commit pending transactions Text (gadfly, ZODB, KirkyBase, Buzhug…). cx.rollback()® rollback pending transactions (optionnal) Subclasses of Node. Cannot have subnode. Text part in an element. Generic access to DBM-style DBs cx.cursor()→ new Cursor object o.nodeType → TEXT_NODE Standard module anydbm is a front-end to some available DB modules : Cursor o.data → unicode: text content dbhash (→bsddb→Berkeley DB), gdbm (→GNU dbm), dbm (→unix dbm) and cu.arraysize → int: (RW) number of rows to fetch with fetchmany - default the slow portable fallback dumbdbm. CDATASection to 1 Data stored in DBM-style files are accessed via a dictionary-like interface cu.connection → Connection: (optional) connection used by cursor Subclasses of Node. Cannot have subnode. CDATA section in a where keys and values must be str. cu.description → [(name, type_code, display_size, internal_size,  document, may have multiple CDATASection nodes for one CDATA. open(filename[,flag[,mode]]) → dictionary-like object: flag in 'r' (read- o.nodeType → CDATA_SECTION_NODE precision, scale, null_ok)]/None: describe result columns default), 'w' (write), 'c' (create if doesn't exist), 'n' (create new empty) - o.data → unicode: CDATA content cu.errorhandler → fct: (optional) handler for connection errors - mode is unix mode flags for creation errorhandler(connection, cursor, errorclass, errorvalue) - default handler fill error→tuple of exception classes from DB modules (anydbm.error,…) ProcessingInstruction cx.messages and may raise exceptions - inherited from connection Uses module whichdb to identify right DB module for existing file. Subclasses of Node. Cannot have subnode. Represents a processing cu.lastrowid → int/None: (optional) row id of last modified column For new files, use first available DB module in the order of the list. instruction in the XML document; this inherits from the Node interface cu.messages → [(exception class,exception value)]: (optional) messages This is used by shelve module (see Persistence, p12). and cannot have child nodes. received from database for operations with cursor o.nodeType → PROCESSING_INSTRUCTION_NODE DB modules can have specific functions related to their backend, see cu.rowcount → int: number of rows produced/affected by last request - -1or docs. o.target → unicode: (ro) processing instruction content up to first whitespace None if request cant touch rows cu.rownumber → int/None: (optional) 0-based index of the cursor in the result Standard DB API for SQL databases o.data → unicode: (ro) processing instruction content after first whitespace set if available Generally modules for SQL databases use the Standard Python Database Exceptions cu.callproc(procname[,parameters])→ (parameters) - (optional) call db API v2 (defined in PEP249). Python map DOM error codes to exceptions. stored procedure - in result out and inout parameters may have been replaced API Informations by procedure DOM codes constants Exception apilevel → str: currently '1.0' or '2.0' - '1.0' if undefined cu.close()® close the cursor DomstringSizeErr DOMSTRING_SIZE_ERR  threadsafety → int: level of thread safety cu.execute(oper[,params]) ®prepare and execute DB request - params1 is a HierarchyRequestErr HIERARCHY_REQUEST_ERR  sequence or a mapping (see module paramstyle variable) # share module share connections share cursors IndexSizeErr INDEX_SIZE_ERR  0 no no no cu.executemany(oper,params_seq) ®like execute, with a sequence of InuseAttributeErr INUSE_ATTRIBUTE_ERR  1 yes no no params (for multiple values) InvalidAccessErr INVALID_ACCESS_ERR  cu.fetchone()→ (column_value,…) / None: next row of query result, None 2 yes yes no InvalidCharacterErr INVALID_CHARACTER_ERR  when no more data available 3 yes yes yes cu.fetchmany([size])→ [(column_value)]: next set of rows of query result, InvalidModificationErr INVALID_MODIFICATION_ERR  paramstyle → str: parameter marker for requests empty list when no more data available - size default to cu.arraysize InvalidStateErr 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 Method __getitem__ is used to get values in params, using position 1 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. 18a 18b 18c