Python -- MISC

Posted by Jeffery Yee | 7:18 AM

A list of online materials that you can refer to.

Learn Python:


start ipython

ipython -p sh

Map function with multiple variable

def func1(a, b, c):
        return a+b+c
map(lambda x: func1(*x), [[1,2,3],[4,5,6],[7,8,9]])

useful packages

  • psutil : providing an interface for retrieving information on all running processes and system utilization (CPU, disk, memory, network) in a portable way by using Python
  • imp
  • sys
  • os
  • re
  • Pexpect
Pexpect is a pure Python module that makes Python a better tool for controlling and automating other programs. Pexpect is similar to the Don Libes `Expect` system, but Pexpect as a different interface that is easier to understand. Pexpect is basically a pattern matching system. It runs programs and watches output. When output matches a given pattern Pexpect can respond as if a human were typing responses. Pexpect can be used for automation, testing, and screen scraping. Pexpect can be used for automating interactive console applications such as ssh, ftp, passwd, telnet, etc. It can also be used to control web applications via `lynx`, `w3m`, or some other text-based web browser. Pexpect is pure Python. Unlike other Expect-like modules for Python Pexpect does not require TCL or Expect nor does it require C extensions to be compiled. It should work on any platform that supports the standard Python pty module.
  • Pyro4 -- Pyro means PYthon Remote Objects. It is a library that enables you to build applications in which objects can talk to eachother over the network, with minimal programming effort.
  • TkInter  Tkinter is Python's de-facto standard GUI (Graphical User Interface) package. It is a thin object-oriented layer on top of Tcl/Tk. Tkinter is not the only GuiProgramming toolkit for Python. It is however the most commonly used one. CameronLaird calls the yearly decision to keep TkInter "one of the minor traditions of the Python world."

Simplifying Python script arguments

import sys
print sys.argv
and you enter:
       python a b c d e

run a program from within Python

  • execfile
if you want to add augments. use the following:['./', arg1, arg2])[sys.executable, '', 'argument1', 'argument2'])
  • subprocess.Popen
The former can be done by importing the file you're interested in. execfile is similar to importing but it simply evaluates the file rather than creates a module out of it. Similar to "sourcing" in a shell script.
The latter can be done using the subprocess module. You spawn off another instance of the interpreter and pass whatever parameters you want to that. This is similar to shelling out in a shell script using backticks.

[edit]install python module with root previlege

mkdir -p ${HOME}/opt/lib/python2.4/site-packages/
echo "PYTHONPATH=\$PYTHONPATH:\${HOME}/opt/lib/python2.4/site-packages/" >> ~/.bashrc
echo "export PYTHONPATH" >> ~/.bashrc
echo "export PATH=\$PATH:\${HOME}/opt/bin" >> ~/.bashrc
source ~/.bashrc
easy_install --prefix=${HOME}/opt MySQL-python

How do you append directories to your Python path?

     Your path (i.e. the list of directories Python goes through to search for modules and files) is stored in the path attribute of the sys module. Since path is a list, you can use the append method to add new directories to the path.
    For instance, to add the directory /home/me/mypy to the path, just do:
    import sys 
               sys.path.insert(0 , "path") #such that python will search it first. 

    How did you install the wxPython bindings? By rpm?

    As for once you know where the modules are located, you can stick something similar to the following example in $HOME/.bash_profile (or whatever the similar syntax is for your particular shell's startup scripts):

    export PYTHONPATH=$PYTHONPATH:$HOME/lib/python:$HOME/lib/misc

    What is used for?

      Files named are used to mark directories on disk as a Python package directories. If you have the files
      and mydir is on your path, you can import the code in as:
      import spam.module
      from spam import module
      If you remove the file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.
      The file is usually empty, but can be used to export selected portions of the package under more convenient names, hold convenience functions, etc. Given the example above, the contents of the __init__ module can be accessed as
        import spam 

      Python Regular Expressions


      %s Represents a value as a string
      %i Integer 
      %d Decimal integer 
      %u Unsigned integer
      %o Octal integer
      %x/%X Hexadecimal integer 
       %e/%E Float exponent
      %f/%F Floa
      %C ASCII character 

      Fancier Output Formatting for official site

      String % Dictionary
      Monica = { 
                       "Occupation": "Chef",
                       "Name" : "Monica", 
                       "Dating" : "Chandler",
                       "Income" : 40000 
      With %(Income)d, this is expressed as
      "%(Name)s %(Income)d" % Monica 

      Tips on python Collections:


      Popular Posts