from itertools import count
next_id = count(0).next
print next_id() # 0
print next_id() # 1
print next_id() # 2
If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions
Sunday, June 25, 2006
Quick ID Generator
Sometimes I find myself in the need to assign unique id for objects, count from itertools is a quick solution.
Thursday, June 15, 2006
Using distutils to build SWIG packages
SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages.
Using SWIG makes connecting libraries written in C/C++ to Python very simple.
However you also need to compile the SWIG generated sources with all the right compiler flags (for finding the Python header files and libraries).
We can use distutils to save us this work as well.
Assume our C library is hello.c and our SWIG interface definition is in hello.i, write the following code in setup.py
Note the underscore in the module name, SWIG generated a hello.py which will call import _hello somewhere.
To compile run python setup.py build_ext -i.
For a long article on SWIG and Python see here.
EDIT (3/2010): UnixReview is no longer with us, I'll try to locate the article.
Using SWIG makes connecting libraries written in C/C++ to Python very simple.
However you also need to compile the SWIG generated sources with all the right compiler flags (for finding the Python header files and libraries).
We can use distutils to save us this work as well.
Assume our C library is hello.c and our SWIG interface definition is in hello.i, write the following code in setup.py
from distutils.core import setup, Extension
setup(
ext_modules = [
Extension("_hello", sources=["hello.c", "hello.i"])
]
)
Note the underscore in the module name, SWIG generated a hello.py which will call import _hello somewhere.
To compile run python setup.py build_ext -i.
For a long article on SWIG and Python see here.
EDIT (3/2010): UnixReview is no longer with us, I'll try to locate the article.
Tuesday, June 06, 2006
Visitor Design Pattern
The Visitor Design Pattern can be written a bit differently in Python using its rich introspection abilities.
(This is a modified version of the code found in compiler/visitor.py in the Python standard library)
(This is a modified version of the code found in compiler/visitor.py in the Python standard library)
Wednesday, May 24, 2006
Thursday, May 18, 2006
Using Python Syntax in Configuration Files
As a rule it thumb, it's best to place as many configuration options in a configuration file. This way you don't need to edit/compile the code to change its behavior.
However writing a parser for configuration files takes time, and the standard ones (.ini and .xml) have their limitations. Lucky for us each Python program comes with the full interpreter bundled. We can do stuff like:
Then using
To view this approach taken to extreme, have a look at http://www.unixreview.com/documents/s=9133/ur0404e/
However writing a parser for configuration files takes time, and the standard ones (.ini and .xml) have their limitations. Lucky for us each Python program comes with the full interpreter bundled. We can do stuff like:
# settings.py
from sys import platform
if platform == "win32":
APP_HOME = "c:\\my_cool_app"
else:
APP_HOME = "/opt/my_cool_app"
Then using
__import__ in our application we can load the configuration file and parse it without a sweat:
# my_cool_app.py
settings = __import__("settings.py")
print settings.APP_HOME
To view this approach taken to extreme, have a look at http://www.unixreview.com/documents/s=9133/ur0404e/
Wednesday, May 10, 2006
"Hard" breakpoints in Python
Sometimes, when debugging, you want a to stop at abreakpoint no matter what.
Use the following code:
When you run the script with python (not with
BTW: You can do the same trick on MSVC if you write
Use the following code:
from pdb import set_trace
def buggy_function():
pass
pass
set_trace() # Break here
pass
pass
if __name__ == "__main__":
buggy_function()
When you run the script with python (not with
pdb), set_trace will cause the program to stop at this line and pdb console will show.BTW: You can do the same trick on MSVC if you write
__asm int 3
Here We Go
Starting a blog! How original :)
Welcome!
I plan to post a weekly tip on Python programming and programming in general, hope you'll find it useful.
Miki
Welcome!
I plan to post a weekly tip on Python programming and programming in general, hope you'll find it useful.
Miki
Subscribe to:
Posts (Atom)
