Tutorials
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
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 showargs.py a b c d e
run a program from within Python
if you want to add augments. use the following:
subprocess.call(['./abc.py', arg1, arg2])
subprocess.call([sys.executable, 'abc.py', 'argument1', 'argument2'])
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.append("/home/me/mypy")
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):
Code:
export PYTHONPATH=$PYTHONPATH:$HOME/lib/python:$HOME/lib/misc
What is __init__.py used for?
Files named __init__.py are used to mark directories on disk as a Python package directories. If you have the files
mydir/spam/__init__.py
mydir/spam/module.py
and mydir is on your path, you can import the code in module.py as:
import spam.module
or
from spam import module
If you remove the __init__.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.
The __init__.py 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
Formatting
%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