How I may help
LinkedIn Profile Email me!
Call me using Skype client on your machine

Reload this page Python and Jython Programming

Here are concise notes on the Python script programming language, combining the knowledge and wisdom from various news groups, books, help files, training documents, etc. — arranged in my own way, but with all text in this one large file for quick search through all topics.

This page is part of a series of related technologies:

Python official home page.

webpage article Google Computers > Programming > Languages > Python

 

Topics this page:

  • Why Python?
  • Language History
  • Starting the Env.
  • Development Workflow
  • Source Syntax Basics
  • Source Files
  • Built-in functions
  • Import Module
  • Documentation
  • Folders, Files
  • IDEs, Debugging
  • Programming Utilities
  • Instances, Libraries
  • Algorithms
  • Interfaces, Coding, Identifiers, Data Structures
  • Collections
  • Resources
  • Your comments???

  •  

    Site Map List all pages on this site 
    About this site About this site 
    Go to first topic Go to Bottom of this page


    Set screen Why Python?

      Among the reasons Pythonistas (rabid fans of Python) love the language are that it does automatic data type conversions, so data overflow errors cannot happen.

      Many are forced to learn the language in order to maintain jacl Jython wsadmin scripts administering IBM Websphere servers.

      Google chose to build Google Apps running within the Google cloud at *.appspot.com. Even though Google enabled Java in 2009, some of the underlying code is still in Python.

      Others come to Python because of the power of the CMS Plone running on the free open-source Zope web application server; the lighter-weight Django, or the PushToTest SOA testing framework written in Python.

      Set screen Language History

      The Python language is not named after a reptile, but the British TV series "Monty Python's Flying Circus", which ran from 5 Oct 1969 to 1973 on BBC-1 and on BBC-2 (without Cleese) until 5 Dec 1974. The show featured surreal sketch comedy by many of Britain's most well-known commedians who went on to create "Monty Python and the Holy Grail" and other silly movies. They are: Graham Chapman, John Cleese, Terry Gilliam, Terry Jones, Michael Palin, and Eric Idle.

      The name of the Python IDE (that comes with Linux) is the acronymn IDLE.

      Versions to 1.2 were created during 1991-1995 principally by Guido van Rossum at the Stichting Mathematisch Centrum (CWI) in the Netherlands. This is noted in the first prompt when Python starts. — Guido van Rossum and Jelke de Boer, "Interactively Testing Remote Servers Using the Python Programming Language", CWI Quarterly, Volume 4, Issue 4 (December 1991), Amsterdam, pp 283-303.

      Versions to 1.6 during 1995-2001 were created by Guido at CNRI (Corporation for National Research Initiatives) in Reston, Virginia.

      Version 2.0 in 2000 was released for the first time via SourceForge by Guido's PythonLabs team at BeOpen.com.

      Version 2.1 and beyond (2001 on) become owned by the non-profit Python Software Foundation (PSF) modeled after the Apache Software Foundation. This happened after Guido's team moved to Digital Creations, which became Zope (which offers CMS and Intranets).

      Version 2.4 introduced the decimal data type for 28 digit precision with no rounding errors with money.

      Version 2.5 string concatenation was speeded up, making previous hacks unnecessary.

      Version 2.6 added the JSON library from simplyjson. But as of 6/2/09 windows binaries are no longer created for mod_python, requiring builds. So people have avoided this version.

      The Server Side on Python

      Python 3.x (also called Python 3000) is not backward compatible with 2.x Python source.

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Python Run Environments

      Native Python runs on top of the OS.

      For tab completions, history recall, and other aids (as Aral notes), download iPython

      Jython implements the Python scripting language in Java, so it runs within Java (v1.4+) and thus can access Java libraries.

      Other implementations of Python include CPython, PyPy, IronPython, Pyrex, Psyco, etc.

      The Crimson Editor is Python-aware.

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Installers

      Most Linux distributions come with Python installed.

      All Apple computers running Mac OS X come with Python. MacPython by Jack Jansen

      Computers from Hewlett-Packard/Compaq come with Python. Some of HP/Compaq's administrative tools are written in Python.

      MS Windows users need to download an installer from one of these locations:

      There are differences among implementations, which may not fully implement all the standard python libraries.

      By default:
      ActiveState version 2.4 installer creates folder C:\Python24\
      ActiveState version 2.5 installer creates folder C:\Python25\
      ActiveState version 2.7 installer creates folder C:\Python27\
      Etc.

      The Windows installer also adds the selected folder to the front of Windows System Environment Variable Path.

      Using the default folder (instead of the C:\bin folder) does clutter up my root folder more, but many apps that depend on it (such as twistd) look for that default folder name.

      Download and run Python Extensions for Windows (such as 5.15MB file pywin32-211.win32-py2.5.exe) for Python 2.5 by Mark Hammond

      To install Jython (without the IDLE IDE):

        1. Create a folder such as C:\bin.
        2. Download the Jython jar
        3. Add the full path with file name to CLASSPATH (separated by a semicolon)
        4. Execute the jar file and specify the install path (such as C:\bin\jython2.5.0)
        5. Add that folder in your PATH environment variable.
        6. Create a folder for your python source code (such as C:\projects\python).

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen IDEs

      ActiveState's support for Visual Python (integration with MS Visual Studio) was discontinued as of December 15, 2005 in favor of the Komodo IDE.

    • The Wing IDE is a commercial IDE for Python.
    • PyDev IDE for Eclipse

      EasyGUI

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Starting the Python Environment

      Interactive Shell Start-up

      On Windows machines, Python can be started from Start > Programs > Active State 2.x > Python Interactive Shell, which invokes C:\Python25\python.exe.

      Alternately, running the installer (msi file) puts one of these folders in your machine's PATH environment variable, so python.exe can be started from any folder from the Windows Run command prompt.

        python -h

      Jython users:

        jython

      Start with Help

      When Python is started with the -h (help) flag:

      usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
      Options and arguments (and corresponding environment variables):
      -c cmd : program passed in as string (terminates option list)
      -d     : debug output from parser (also PYTHONDEBUG=x)
      -E     : ignore environment variables (such as PYTHONPATH)
      -h     : print this help message and exit
      -i     : inspect interactively after running script, (also PYTHONINSPECT=x)
               and force prompts, even if stdin does not appear to be a terminal
      -m mod : run library module as a script (terminates option list)
      -O     : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)
      -OO    : remove doc-strings in addition to the -O optimizations
      -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew
      -S     : don't imply 'import site' on initialization
      -t     : issue warnings about inconsistent tab usage (-tt: issue errors)
      -u     : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)
               see man page for details on internal buffering relating to '-u'
      -v     : verbose (trace import statements) (also PYTHONVERBOSE=x)
      -V     : print the Python version number and exit
      -W arg : warning control (arg is action:message:category:module:lineno)
      -x     : skip first line of source, allowing use of non-Unix forms of #!cmd
      file   : program read from script file
      -      : program read from stdin (default; interactive mode if a tty)
      arg ...: arguments passed to program in sys.argv[1:]
      Other environment variables:
      PYTHONSTARTUP: file executed on interactive startup (no default)
      PYTHONPATH   : ';'-separated list of directories prefixed to the
                     default module search path.  The result is sys.path.
      PYTHONHOME   : alternate  directory (or ;).
                     The default module search path uses \lib.
      PYTHONCASEOK : ignore case in 'import' statements (Windows).
      

      Upon start-up, python executes the contents of a file identified by the PYTHONSTARTUP environment variable installed by the msi file.

      Invocation Alternatives

    • We don't use PYTHONCASEOK (provided for looser case sensitivity on Windows platforms) because this leads to "sloppy" coding which reduces the portability of code among operating systems.
    • Python can also be invoked with commands followed by "-c", or modules followed by "-m".

      To find the full path of Python's "site-packages" folder:

      • python
      • >>> from distutils.sysconfig import get_python_lib
        >>> print get_python_lib()
        C:\Python27\Lib\site-packages
        

      This path is where packages like Django (and its django-admin.py) should be installed:

        C:\Python27\Lib\site-packages\Django\bin
        

      Wiki how
      Installing Django on Dreamhost


    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen The >>> Prompt for the Dynamically and Strongly Typed

      Reminder The >>> prompt shown by the standard interpreter or
      The In [1]: prompt shown by IPython
      are not shown while a called Python module is running.
      In Windows, press Alt+Tab to switch to the Python app's GUI window.

      Python does not recognize the "exit" command used in Windows Run command windows. But since it is a common mistake, when you type exit the reply is:

        Use exit() or Ctrl-Z plus Return to exit

        To exit, press Ctrl+Z (^Z) and then Enter/Return key.

      C and Java are "compiled" languages.
      Python is an "intepretive" language like Unix shell scripts and Windows command (batch) files.
      Jython (formerly Jpython) provisions Java programs to run Python programming code interactively (providing the best of both worlds).

      So Python's interactive Mode prompt >>> can be used like a calculator, where commands typed in are immediately executed.

        >>> 1+1
        2
        >>> print "2 cubed is",(2 * 2 * 2)
        2 cubed is 8
        >>> print "% cubed is" % (2,(2 * 2 * 2))
        2 cubed is 8

      Unlike languages such as C and Java, you don't need to declare variables with their datatypes before using them. Python does not require variable or argument declarations. This is because Python (and VBScript) are called "dynamically typed" languages since Python figures out at time of execution what datatype a variable is when it is first assigned, rather than at time of compilation (such as "statically typed" languages like C and Java). The largest integer on a 64-bit system:

        >>> import sys
        >>> print sys.maxint
        9223372036854775807
        

      Python automatically promotes integers to float.

      Python is called a "strongly typed" languages becuase it always enforces types. Unlike the "weakly typed" VBScript language, within Python (and Java), you cannot (without any explicit conversion) concatenate the string '12' and the integer 3 to get the string '123', then treat that as the integer 123. With Python, if you try to treat an integer as a string without first explicitly converting it, you will get a TypeError.

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Java Built-In Data Types

      The Boolean data type (added in v2.3) has 1 for True and 0 for False.

        >>> # one way:
        >>> from java.util import Random
        >>> # another way:
        >>> import java.util.Random
        >>> randnum = java.util.Random()
        >>> i = randnum.nextBoolean()
        >>> print i
        True
        >>> print randnum.nextFloat()
        0.541107296944
        

      Python does not have the new keyword to instantiate the randnum object.

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Python Source Code Basics

      Like the Monty Python show, which "offered savage broadsides against the pomposity and repression of the British establishment." the Python language aims to offer an improvement to C and Java.

      We follow the Python Code Style Guide (expanded in a mediaWiki version).

      The Main Module Upside Down

      Python begins by executing code such as this usually at the bottom of the main module's source code file:

          if __name__=="__main__":
              main_logic()
      


      """
      hello.py - Wilson Mar - 24oct2005
      Some simple program
      Coded for Jython version 2.5 for Windows 32-bit.
      """
      class MessageError(Exception):
          """Base class for errors in this package."""
      	# To raise ValueError('message')" 
      	
      def func1():
      	pass
      def func2():
      	pass
      
      dispatch = {'go': func1, 'stop': func2}  
          # Note lack of parens for funcs
      
      dispatch[get_input()]()  
          # Note trailing parens to call function
      
      def func2(a, b):
          """Returns the average of *two* (and only two) arguments.
      	:param arg1: a numeric value
      	:type arg1: **any** numeric type
      	:param arg2: another numeric value
      	:type arg2: **any** numeric type
      	:return: mid-point (or arithmetic mean) between two values
      	:rtype: numeric type compatible with the args.
      	"""
          a = 'new-value'
          a = unicode('new-value','latin-1')
      	print a.encode('latin-1','replace')
      
          b = 0xa5 + 1  # Hex decimal 165+1
          c = 010 # Octal 10 = decimal 8
          d = x010 # Octal 10 = decimal 8
          return a, b
      
      while True:
          line = f.readline()
          if not line:
              break
          #...do something with line...
      
      for line in f:
          # do something else
      
      Between """ (triple quote) markers are multi-line comments.

      Comments can contain doc strings. Doc strings can be retrieved using the print docstring.__doc__ attribute.

      The backward slash is Python's line continuation character.

      Instead of semicolons at the end of each expression (as with C or Java), like VBScript
      Python uses a carriage return to separate statements. This is perhaps the most distinguishing characteristics of Python.

      Python uses {} curly braces to define dictionaries.
      To separate code blocks Python uses indentation (4 spaces) to delimit statement groups. Sequential lines indented at the same level are treated as the same block.

      To continue a line, add a \ back slash and indent the following line.

      The # hash (or pound sign as some call it) begins comments (as with C). This can't follow a continuation character on the same line.

      "..." elipses (3 periods) precedes each multi-line construct.

      Python's high-level data types allow complex operations to be expressed in a single statement.

      The Python community has a saying: "Python comes with batteries included." Python is extensible: functions can be added by adding modules.

      The pass statement does nothing.

      A leading '0' (zero) indicates octal, '0x' indicates a hex number,
      '\u' goes in front of hex Unicodeanother page on this site code point, a different type than a string in Python v2.x but Unicode in Python v3.x. Unicode types have .encode and .decode methods not in string types. See PEP 3120, 3131.

      "_" (the underline character) stands in for the last variable used (read-only).

      "\" backslash character at the end of a line is used to specify continuation to the next line.

      "\n" is an escape characteranother page on this site for "next line" (not necessary between triple quotes).

      "\\" is thus necessary to specify back-slash separators between folders in a directory path.

      "+" is used to concantenate strings (rather than the amersand & in C).

      Strings can be encased between either double or single quotes.

      Unlike C, assignments are not allowed within expressions.

    • jpythonc -tt
    • compiles Jython source modules into Java bytecode (.class) files which then can be run directly using the java or jre command. The -tt parameter issues a compile error when tabs and spaces are not consistently used (mixed) in the source code.


    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Importing Python Source Files

        >>> import tck.hello.py
        Traceback (most recent call l
          File "<stdin>", line 1, in
        ImportError: No module named
        

        >>> import tck-hello.py File "<stdin>", line 1 import tck-hello ^ SyntaxError: invalid syntax

      Python source modules are saved by editors and IDEs as files suffixed with .py, but it does not need to be specified when importing a module.

      Reminder When naming python modules, do not use special characters such as a period or a dash,
      which have special meaning to python. The result of such attempts are shown at right.

      When a module is imported for the first time (or when the source is more recent than the current compiled file) Python parses and translates it into a bytecode files suffixed with .pyc in the same folder as the source file.

      Python scripts are made into an executable Windows scriptanother page on this site when its named with a .cmd suffix and contains this first line:

        @setlocal enableextensions & python -x %~f0 %* & goto :EOF

      Python scripts are made into a UNIX script by putting in the first line a directive containing the path where the Python interpreter is located. This example uses an absolute path:

        #!/usr/local/bin/python

      Source Structure

      Jim Roskind suggests ordering various types of code in this order within modules:

      • exports (globals, functions, and classes that don't need imported base classes)
      • import statements
      • active code (including globals that are initialized from imported values).

      Intermingling Source

      To intermingle Python and C code to increase performance:

      • Pyrex compiles a slightly modified version of Python code into a C extension, for use on many different platforms.
      • Psyco is a just-in-time compiler that translates Python code into x86 assembly language. If you can use it, Psyco can provide dramatic speedups for critical functions.
      • PyInline
      • Py2Cmod
      • Weave


    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Built-in Names (Modules, Functions, Variables)

      Adding name "L" to the local namespace, making it refer to an empty list object.

        >>> L = []
        >>> L = ['a',1] # notice mixed types >>> L.append('b')
        >>> L
        ['a',1,'b']

      Get an alphabetized list of the names containing the instance attributes and methods and attributes defined by its class:

        >>> dir(L)
        ['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

      Use a method:

        >>> L.append('b')
        >>> L
        ['a',1,'b']

      Empty dictionary:

        >>> a={}
        >>> dir(a)
        ['clear', 'copy', 'get', 'has_key', 'items', 'keys', 'update', 'values']

      Double underlines on both sides of a name designate a built-in system name.

      Double leading underscores and no trailing underscores designate that the attribute can be automatically mangled by Python with the simple class name in order to avoid name collisions should subclasses inadvertently contain attributes with the same name. However, this makes __getattr__() less convenient.

      A single leading underscore to a method or attribute designates it as private. This only discourages direct access externally to the class but does not prevent access.

      To load python's built-in libraries and list it:

        >>> import __builtin__
        >>> dir(__builtin__)
        ['ArithmeticError', 'AssertionError', 'AttributeError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'IOError', 'ImportError', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'OverflowWarning', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', 'abs', 'apply', 'basestring', 'bool', 'buffer', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min', 'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Import sys and PYTHONPATH

      Python scripts import modules only from directories specified in the PYTHONPATH environment variable. On Unix, this is usually .:/usr/local/lib/python. Internally, this is variable sys.path, which can be changed with code such as (on a Unix machine):

        >>> import sys

        >>> sys
        <module 'sys' (built-in)>

        >>> sys.path.append('/ufs/guido/lib/python')

        >>> sys.path
        ['', '/usr/local/lib/python2.2',
        '/usr/local/lib/python2.2/plat-linux2',
        '/usr/local/lib/python2.2/lib-dynload',
        '/usr/local/lib/python2.2/site-packages',
        '/usr/local/lib/python2.2/site-packages/PIL',
        '/usr/local/lib/python2.2/site-packages/piddle']

      On a Windows machine:

        >>> sys.path
        ['',
        'C:\\WINDOWS\\system32\\python24.zip',
        'C:\\Documents and Settings\\W',
        'C:\\Python24\\DLLs',
        'C:\\Python24\\lib',
        'C:\\Python24\\lib\\plat-win',
        'C:\\Python24\\lib\\lib-tk',
        'C:\\Python24',
        'C:\\Python24\\lib\\site-packages',
        'C:\\Python24\\lib\\site-packages\\win32',
        'C:\\Python24\\lib\\site-packages\\win32\\lib',
        'C:\\Python24\\lib\\site-packages\\Pythonwin']

      Idea It is good practice to import modules in this order:

      1. standard libary modules: sys, os, getopt, re.
      2. third-party library modules installed in Python's site-packages directory.
      3. locally-developed modules for your application.

      After you change a module, force rereading:

        >>> import modname
        >>> reload(modname)

        Reminder This does not affect modules which use:

        from modname import *

        So avoid using the above. They also clutter the namespace.

      Also avoid circular imports ...

      Arguments are passed by assignment in Python.

      To issue a prompt and accept an input from the user:

        >>> x = int(raw_input("Please enter an integer: "))

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Standard Library Modules

      List of Python Software

      Module Contents
      >>> dir(sys) ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__', '__stdin__', '__stdout__', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'callstats', 'copyright', 'displayhook', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'getdefaultencoding', 'getdlopenflags', 'getrecursionlimit', 'getrefcount', 'hexversion', 'maxint', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'version', 'version_info', 'warnoptions']
      >>> dir(os) ['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAI T', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'UserDict', 'W_OK', ' X_OK', '_Environ', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '_copy_reg', '_execvpe', '_exists', '_exit', '_get_exports_list', '_make_stat_r esult', '_make_statvfs_result', '_pickle_stat_result', '_pickle_statvfs_result', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'curdir', 'defpath', 'd evnull', 'dup', 'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp' , 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fstat' , 'fsync', 'getcwd', 'getcwdu', 'getenv', 'getpid', 'isatty', 'linesep', 'listdi r', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pa thsep', 'pipe', 'popen', 'popen2', 'popen3', 'popen4', 'putenv', 'read', 'remove ', 'removedirs', 'rename', 'renames', 'rmdir', 'sep', 'spawnl', 'spawnle', 'spaw nv', 'spawnve', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs _result', 'strerror', 'sys', 'system', 'tempnam', 'times', 'tmpfile', 'tmpnam', 'umask', 'unlink', 'unsetenv', 'urandom', 'utime', 'waitpid', 'walk', 'write']
      >>> dir(getopt) ['GetoptError', '__all__', '__builtins__', '__doc__', '__file__', '__name__', 'd o_longs', 'do_shorts', 'error', 'getopt', 'gnu_getopt', 'long_has_args', 'os', ' short_has_arg']
      >>> dir(re) ['DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Sc anner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__version__', '_alphanum', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_pattern_type' , '_pickle', '_subx', 'compile', 'copy_reg', 'error', 'escape', 'findall', 'find iter', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', ' subn', 'sys', 'template']
      >>> dir("") ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']


    Go to Top of this page.
    Previous topic this page
    Next topic this page

      Set screen Third-Party Class Libraries

      • PyNum (Numerical Python) handles arrays, matrices, and other scientific computations.
      • mx.DateTime for advanced date manipulation
      • ZODB for database access
      • PIL.Image for image file handling
      • To do asynchronous http requests in twisted, run IO-loop (reactor.run) to enable async features not in python by default.
      • etc.


    Go to Top of this page.
    Previous topic this page
    Next topic this page

      Set screen GUI Libraries

      Python's de-facto standard for the most commonly used GUI toolkit is iTkinter or Tkinter (short for "Tk interface", pronounced "tickle interface "). It is a thin object-oriented layer on top of Tcl/Tk from Scriptics.

      Tk originated from Sun Labs. Today it's available on most Unix and Macintosh platforms. Since release 8.0, a dll provides Microsoft Windows XP native look and feel.

      Tkinter consists of, among many modules, Tkconstants and the Tk low-level interface binary module _tkinter (which should never be used directly by application programmers).

      Tk is event driven




    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Documentation

      Follow PEP 257 Docstring conventions and styles .

      The first line should always be a short, concise summary of the object's purpose. For brevity, it should not explicitly state the object's name or type, since these are available by other means (except if the name happens to be a verb describing a function's operation). This line should begin with a capital letter and end with a period.

      If there are more lines in the documentation string, the second line should be blank, visually separating the summary from the rest of the description. The following lines should be one or more paragraphs describing the object's calling conventions, its side effects, etc.

      If you mark-up your source in structured-text docstrings format, you can use

      webpage article Python.org Main doc page

     


    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Compiling

      Modules compiled with the -O Optimization flag (which causes asserts and __doc__ strings to be removed) are suffixed with .pyo.

      __main__ modules don't become .pyc modules. But you can force compile with:

        >>> import py_compile
        >>> py_compile.compile('xxx.py')

      .pyc files are written to the same location as .py file unless overrided with the optional parameter -cfile.

      To compile all files in a directory or directories:

        python compileall.py

        You will be prompted for the directory path.

     


    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Unit Testing and Debugging Python Programs

      First, use a static analysis tool to find syntactic bugs in Python source code. These tools also warns about code complexity and style.

      • PyChecker
      • Pylint also ensure satisfaction with coding standard (such as PEP 8, the standard for writing Python library modules). Standards defined in plug-ins include line length, whether variable names are well-formed according to your coding standard, whether declared interfaces are fully implemented, etc.

      pdb is the default console debugger module.

      The doctest module finds examples in the docstrings for a module and runs them, comparing the output with the expected output given in the docstring.

      The unittest module PyUnit.sourceforge.net testing framework is modelled after Kent Beck's JUnit and smalltalk testing framework.

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen IDEs (Integrated Development Environments)

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Utilities For Python Source Code

    • Decompilers reverse engineering from executables back into (difficult to read) source:
    • C class file obfuscators reduce the size of C class files while it blocks decompilers.
    • Code Formatters:
    • Code Profilers analyze code execution to provide a time-aware view of a program's execution, to easily identify sluggish function bottlenecks.
    • Test Coverage tools reveal which lines of source code were executed during tests.
    • PDF publishing
    • API document generators from code mark-up.
      • epydoc reads notation in Epytext, JavaDoc (which looks like epytext),
      • RST (ReStructuredText), which works well with sphinx.

    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Working Folders

      For a list of folders in the current directory:

      >>> import os # POSIX module
      >>> os.listdir('.')
      

      To create a new folder (and any intermediate directories which don't exist):

      >>> os.makedirs( path )
      

      To remove a directory (and any intermediate directories which are empty):

      >>> os.removedirs( path )
      

      To remove a directory:

      >>> os.rmdir()
      

      To delete an entire directory tree and its contents:

      >>> shutil.rmtree()
      

      The shutil module also has copyfile, copytree, rmtree, and other file functions.

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Working Files

      To open a file using the built-in low-level function to get a small integer file handle for read only):

      >>> f = open( '/tmp/sx', 'r+')
      

      To read an entire file opened by the statement above:

      >>> f.read()
      

      To read n bytes from a pipe:

      >>> f.readline( n )
      

      To open a file using a high-level function to get a small integer file handle for write):

      >>> f = os.popen( '/tmp/sx', 'w')
      

      To write string s with a newline break or list L:

      >>> f.write( 'wow\n' )
      >>> f.writelines( L )
      

      To redirect print statements to a file rather the stdout screen:

      >>> import sys
      >>> temp = sys.stdout
      >>> sys.stdout = open('log.txt','a')
      >>> print x # to file
      >>> std.stdout = temp
      >>> print a # to stdout
      

      To truncate a file from the current offset seek position:

      >>> f.truncate( offset ); 
      

      To rename a file:

      >>> os.rename( old_path, new_path)
      

      To delete a single file:

      >>> os.remove(filename)
      

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Database Access

      >>> import odbchelper
      >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
      >>> print odbchelper.buildConnectionString(params) 
       server=mpilgrim;uid=sa;database=master;pwd=secret
      

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Accessing Console

      To request input of an integer from user:

        >>> int( raw_input('Enter a number:'))
        


    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Performance Algorithms

     


    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Interfaces

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Sub classes

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Object Identifier Naming & Duration

      The structure of names assigned is crucial to developer efficiency by reducing refactoring effort during construction and subsequent maintenance. We of course follow standards enforced by the compiler:

    • Identifiers start with a letter, but not a number.
    • Uppercase and lowercase alphabetic characters are differentiated.
    • Reference to global address space are explicitly defined. A tuple of global objects is passed to a "set" function in the imported source file. (In this example are Admin* variables among wsadmin to admin IBM Websphere):

        import efgh
        efgh.setAdminRefs((AdminConfig, \
        	AdminControl, AdminTask, AdminApp))
        efgh.listServers()
        
      Within file efgh.py:

        def setAdminRefs(adminTuple):
        	global AdminConfig, AdminControl
        	global AdminTask, AdminApp
        	(AdminConfig, AdminControl, AdminTask, AdminApp) \
        		= adminTuple
        def listServers():
        	print AdminConfig.list("Server")
        


    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Lists

      Python's lists are really variable-length arrays implementated as a contiguous array of references to other objects. A list head structure keeps a pointer to the array and the array's length. This gives Python its cool feature: sequences can be indexed with negative numbers.

      0 starts is the first number, as with C and Java.
      -1 is the last index
      -2 is the pentultimate (next to last) index.

      Think of seq[-n] as the same as seq[len(seq)-n].

      S[:-1] is all of the string except for its last character — useful for removing the trailing newline from a string.

      It's more efficient to use string methods foo.startswith('bar') and ''.endswith() rather than foo[:3] == 'bar'.

      Tuples - Dictionaries - hash

      Tuples are small collections of related data operated on as a group. Tuples are immutable: once a tuple has been created, it can't be replaced with a new value. Tuples can be used as keys because only immutable elements can be used as dictionary keys, which are implemented as resizable hash tables. Its members may be of different types.

        >>> d1 = {'a':1, 'b':2, 'c':3}
        >>> d1['a'] = 4
        >>> d1['a'] 
        4
        

      Dictionaries work by computing a hash code for each key stored in the dictionary using the hash() built-in function. The hash code varies widely depending on the key. For example, the string "Python" hashes to -539294296 while "python", a string that differs by a single character, hashes to 1142331976. This hash code is then used to calculate a location in an internal array where the value will be stored. Assuming that you're storing keys that all have different hash values, this means that dictionaries take constant time " O(1), in computer science notation " to retrieve a key. It also means that no sorted order of the keys is maintained, and traversing the array as the .keys() and .items() do will output the dictionary's content in some arbitrary jumbled order.

      In Python, strings are immutable. Once defined, they can't be changed.

      However, individual elements of a list can be redefined for reference directly:

      >>> s = "Hello world"
      >>> a = list(s)
      >>> print a
      ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
      >>> L[1]="h"
      

      To turn an object back into a string (so it can be written to a file):

        >>> ''.join(a)
        'Hello there!'
        

      Instead of '' above, ',' causes a comma to be inserted between each item.

      It's faster to use the array module:

      >>> import array
      >>> a = array.array('c', s)
      >>> print a
      array('c', 'Hello, world')
      >>> a[0] = 'y' ; print a
      array('c', 'yello world')
      >>> a.tostring()
      'yello, world'
      

      You can use slice notation to insert a copy of itself at the beginning of list b:

        >>> b[:0] = b

      With slice, starting and ending locations (starting from zero): two indices separated by a colon. Negative numbers count from the right.

      With print, a space is automatically inserted between items. And a newline is inserted before the next prompt if the last line was not completed.

      One neat Python trick is multiplication of strings:

        >>> "Hello" * 3
        HelloHelloHello

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

      Set screen Object modifiers

      Set screen Inner Classes


    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Developer Resources

      Certification

      Tutorials

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Style Guides: "Pythonic" syntax Numbers

      Python has no ternary operator such as a?b:c in C. Instead, Python has borrowed Smalltalk's lambda expressions

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Handing Large Numbers with Epsilon

      Python has a neat feature called an "epsilon":

      str(23) yields 23.
      oct(23) yields '027' (strings with a leading "0" are interpreted as octals).
      hex(23) yields '0x17' (strings with a leading "0X" are intrepreted as hex).

      When comparing two very large or small numbers, a regular == operator would fail because of small differences.

        >>> 0.2
        0.20000000000000001

      So check whether the difference between the two numbers is less than a certain threshold: the epsilon.

      epsilon = 0.0000000000001 # Tiny allowed error
      expected_result = 0.4
      if expected_result-epsilon <= computation() <= expected_result+epsilon:
      

     

     
    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Portions ©Copyright 1996-2004 Wilson Mar. All rights reserved. | Privacy Policy |


    Related Topics:
    another page on this site Keyboard Shortcuts 
    another page on this site Project Software 
    another page on this site Project Central 

    another page on this site Free Training! 
    another page on this site Tech Support 


    How I may help

    Send a message with your email client program


    Your rating of this page:
    Low High




    Your first name:

    Your family name:

    Your location (city, country):

    Your Email address: 



      Top of Page Go to top of page

    Thank you!