Python

From Tmplab

(Difference between revisions)
Jump to: navigation, search
(Intro)
Current revision (11:15, 18 November 2009) (edit) (undo)
 
(12 intermediate revisions not shown.)
Line 7: Line 7:
= Tricks =
= Tricks =
 +
 +
== Importing ==
 +
 +
X = __import__(‘X’)
 +
 +
works like
 +
import X
 +
 +
with the difference that you 1) pass the module name as a string, and 2) explicitly assign it to a variable in your current namespace.
 +
 +
== Content of objects ==
 +
Finding what's in an object
Finding what's in an object
>>> dir(obj)
>>> dir(obj)
 +
 +
== Copying objects ==
 +
 +
Regular:
 +
obj2 = obj1
 +
 +
Complete:
 +
obj2 = copy.deepcopy(obj1)
 +
 +
== Key, Values of dicts ==
 +
 +
ah iteritems() ...
 +
 +
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
 +
>>> for k, v in knights.iteritems():
 +
... print k, v
 +
 +
== Any kind of garbage ==
 +
 +
.strip(string.whitespace + string.punctuation)
 +
 +
== Glob me baby! File completion in a snap ==
 +
 +
self.list = glob.glob("*.db")
 +
 +
== Getting content AND index from lists ==
 +
 +
for index,line in enumerate(content):
 +
...
 +
 +
= Debugging =
 +
 +
See http://pythonconquerstheuniverse.wordpress.com/category/the-python-debugger/
 +
 +
import pdb
 +
pdb.set_trace()
 +
= Editor tricks =
= Editor tricks =
Line 18: Line 67:
* C-c <: Shift the region to the left
* C-c <: Shift the region to the left
* C-c >: Shift the region to the right
* C-c >: Shift the region to the right
 +
 +
= HOW TO =
 +
 +
== Modules & Extensions: distutils ==
 +
* http://docs.python.org/extending/building.html
 +
* http://docs.python.org/distutils/
 +
 +
One mention, sometime you get this error message when building your extension under Mac OS X:
 +
/usr/bin/ld: can't locate file for: -lnameofmylib
 +
 +
This seems to be a weird problem with Mac OS X where even if the library (here a dynamic library ending with .dylib) is in /usr/lib, it won't be "seen" by the ld linker. To mitigate this problem, create a link in /usr/local/lib/ to the /usr/lib/XXXX.dylib library and add the path in the setup.py file.
 +
 +
Here is a sample setup.py that fixes that:
 +
"""
 +
setup.py
 +
 +
Created by Philippe Langlois on 2009-11-02.
 +
Copyright (c) 2009 Philippe Langlois. All rights reserved.
 +
"""
 +
 +
from distutils.core import setup, Extension
 +
 +
setup(name='pysctp',
 +
version='0.5',
 +
py_modules=['sctp'],
 +
ext_modules=[Extension('pysctp', sources=['_sctp.c'],
 +
include_dirs=['.', '/usr/include'],
 +
libraries=['sctp'],
 +
library_dirs=['/usr/lib/', '/usr/local/lib/'],
 +
)
 +
],
 +
data_files=['_sctp.h'],
 +
author='Elvis Pfutzenreuter',
 +
author_email='epx __AAAAATTTT_____ epx.com.br',
 +
maintainer='Philippe Langlois',
 +
maintainer_email='Philippe.Langlois __AAAAATTT____ gmail.com',
 +
)
Line 42: Line 128:
/Library/Python/2.5/site-packages/
/Library/Python/2.5/site-packages/
^D
^D
 +
 +
= Good packages / extensions =
 +
 +
== dpkt ==
 +
For bit-manipulation in python
 +
* http://code.google.com/p/dpkt/
 +
 +
= References =
 +
 +
== Bit operations in python ==
 +
* http://wiki.python.org/moin/BitManipulation
 +
 +
== Quick Cheat Sheet ==
 +
* http://rgruet.free.fr/PQR24/PQR2.4.html

Current revision

Contents

Intro

This cheat sheet helps troubleshoot common problems...

Some good documentation source:

Tricks

Importing

X = __import__(‘X’) 

works like

import X

with the difference that you 1) pass the module name as a string, and 2) explicitly assign it to a variable in your current namespace.

Content of objects

Finding what's in an object

>>> dir(obj)

Copying objects

Regular:

obj2 = obj1

Complete:

obj2 = copy.deepcopy(obj1)

Key, Values of dicts

ah iteritems() ...

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.iteritems():
...     print k, v

Any kind of garbage

.strip(string.whitespace + string.punctuation)

Glob me baby! File completion in a snap

     self.list = glob.glob("*.db")

Getting content AND index from lists

for index,line in enumerate(content):
     ...

Debugging

See http://pythonconquerstheuniverse.wordpress.com/category/the-python-debugger/

import pdb
pdb.set_trace()


Editor tricks

Emacs

See http://python.about.com/b/2007/09/24/emacs-tips-for-python-programmers.htm

  • C-c <: Shift the region to the left
  • C-c >: Shift the region to the right

HOW TO

Modules & Extensions: distutils

One mention, sometime you get this error message when building your extension under Mac OS X:

/usr/bin/ld: can't locate file for: -lnameofmylib

This seems to be a weird problem with Mac OS X where even if the library (here a dynamic library ending with .dylib) is in /usr/lib, it won't be "seen" by the ld linker. To mitigate this problem, create a link in /usr/local/lib/ to the /usr/lib/XXXX.dylib library and add the path in the setup.py file.

Here is a sample setup.py that fixes that:

"""
setup.py

Created by Philippe Langlois on 2009-11-02.
Copyright (c) 2009 Philippe Langlois. All rights reserved.
"""

from distutils.core import setup, Extension 

setup(name='pysctp',
     version='0.5',
     py_modules=['sctp'],
	  ext_modules=[Extension('pysctp', sources=['_sctp.c'],
	  						 include_dirs=['.', '/usr/include'],
	  						 libraries=['sctp'], 
	  						 library_dirs=['/usr/lib/', '/usr/local/lib/'],
							)
				  ],
	  data_files=['_sctp.h'],
	  author='Elvis Pfutzenreuter',
	  author_email='epx __AAAAATTTT_____ epx.com.br',
         maintainer='Philippe Langlois',
	  maintainer_email='Philippe.Langlois __AAAAATTT____ gmail.com',
     )


Problems

I installed some modules / packages and can't load them

echo $PYTHONPATH
$ python
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) 
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
for i in sys.path: 
    print i

You can add a .pth file in one of the already loaded dir: see http://djangotricks.blogspot.com/2008/09/note-on-python-paths.html

like:

# cat > /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/others.pth
/Library/Python/2.5/site-packages/
^D

Good packages / extensions

dpkt

For bit-manipulation in python

References

Bit operations in python

Quick Cheat Sheet

Personal tools