Jon AtkinsonI'm a web developer and system administrator

Posts tagged with 'python'

Importing python modules, avoiding namespace clashes

Posted on 31st October 2007. Tagged as python

Python does dumb things when importing modules.

The scenario; one local source file called 'calendar.py', and one Python module (part of the standard libraries), 'calendar'. If, within calendar.py, you evaluate

>>> import calendar

then the script will import itself. This is both proper, and useful, but there is no way to force the interpreter to import the module. This is quite annoying. In fact, the documentation identifies this issue (emphasis mine):

"…it is important that the script not have the same name as a standard module, or Python will attempt to load the script as a module when that module is imported. This will generally be an error."

The problem here (beyond the manual acknowledging this is a common error, and nothing being done to remedy it), is that Python is encouraging non-standard and obscure naming of packages. I'm writing source which deals with a calendar, so in the interests of my own sanity (and the sanity of my successor), I'm not going to name my file 'joncalendar.py', 'calendar2.py', or anything like that. I should be able to name my files as I see correct.

So, here is the workaround. By removing the current directory from the Python path, prior to importing a module, you can import from the standard Python library.

def python_import(name):
import os
import sys
current_path = os.path.dirname(os.path.abspath(__file__))
base_name = os.path.basename(current_path).split('.')[0]
sys.path[:] = [path for path in sys.path
if os.path.abspath(path) != os.path.abspath(current_path)]
original_module = sys.modules[name]
del sys.modules[name]
python_module = __import__(name)
python_module_name = 'python_%s' % name
sys.modules[python_module_name] = python_module
sys.path.append(current_path)
sys.modules[name] = original_module
return python_module

An example of usage:

calendar = python_import('calendar')
print dir(calendar)

Django's user_passes_test and generic views

Posted on 26th September 2008. Tagged as django, python, tips

Previously, I've often used a combination of django.contrib.auth and the login_required decorator as a simple way of controlling access to certain parts of an application. However, I'm working on a fairly complex application right now, which will grant access based on the is_staff and is_superuser fields.

It's trivial (and well documented) to use the user_passes_test decorator in a view, but in my case I'm using generic views. This is not so well documented. Here is a basic example of how to do it:

from django.conf.urls.defaults import *
from django.contrib.auth.decorators import user_passes_test

staff_required = user_passes_test(lambda u: u.is_staff)
superuser_required = user_passes_test(lambda u: u.is_superuser)

urls = patterns('',
(r'^staff_only/$', staff_required(generic.view.here), { ... }),
(r'^super_only/$', superuser_required(generic.view.here), { ... }),
)

Installing M2Crypto on OSX

Posted on 9th October 2008. Tagged as python, tips, osx, mac, m2crypto, openssl

I really like M2Crypto, but it is difficult to install on OSX without a few annoying prerequisites. Assuming you are running Leopard (or, I guess, newer), this is what you need to do:

First, you need to install the XCode Developer Tools. You probably have these installed already, but if not, they're on your OS install discs, or you can download them from Apple. Once they're installed, you should have a working C compiler. Test with:

$ gcc
i686-apple-darwin9-gcc-4.0.1: no input files

Now, you need to install the OpenSSL headers. Grab the latest OpenSSL package from here. Now, unpack and build it. You might want to put the kettle on, this takes a little while.

$ tar xzf openssl-x.x.x.tar.gz
$ cd openssl-x-x-x
$ ./config --prefix=/usr/local
$ make && make test
$ sudo make install

Next, download SWIG. Again, this needs unpacking and building:

$ tar xzf swig-x.x.x.tar.gz
$ cd swig-x.x.x
$ ./configure --prefix=/usr/local
$ make
$ sudo make install

Finally, you need to download and install M2Crypto. Grab the source, and install it as follows:

$ tar xzf M2Crypto-x.x
$ cd M2Crypto-x.x
$ python setup.py build build_ext --openssl=/usr/local
$ sudo python setup.py install build_ext --openssl=/usr/local

Then to test your installation, simply do the following:

$ python
Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import M2Crypto
>>>

Twitter

About Me

Picture of Jon Atkinson

Jon Atkinson is a web developer, sysadmin and occasional business guy. He works in the north west of England.

Jon can be contacted at , or on freenode as JonA. Also available: twitter, LinkedIn and Github.

84labs logo

I own and run 84labs, a company which provides bespoke web application development for businesses and startups.

If you're interested in working with me, take a look, then contact me via 84labs.

Testled logo

I'm one of the founders of Testled.com, a web service to provide simple remote usability testing of web sites and desktop applications

Testled.com is currently in private beta, but you can still signup for an invite.