Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install django-pandora

How to install django-pandora

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install django-pandora
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
0.1.0 Available View build log
Windows (64-bit)
0.1.0 Available View build log
Mac OS X (10.5+)
0.1.0 Available View build log
Linux (32-bit)
0.1.0 Available View build log
Linux (64-bit)
0.1.0 Available View build log
 
License
BSD
Dependencies
Imports
Lastest release
version 0.1.0 on Jan 5th, 2011

django-pandora

This is Pandora's box. Don't open it if you cannot live with the consequences.

django-pandora is a utility library for django that lets you easily store values in a thread-local. This makes it possible to introduce global variables that are thread safe and don't interfere with other requests to the server.

However a global state is a common anti-pattern in software development that you usually want to avoid. But at the end of the day it might be the easiest way to solve some of your problems.

The most common use case for django-pandora might be to store the current request in Pandora's box to make it available in places of your code that doesn't get the request passed in as argument.

Usage

The pandora.box object implements a dict like interface which you can use to store thread local values. It's really as simple as using a dictionary:

>>> from pandora import box
>>> box['foo'] = 'Hello world!'
>>> box['foo']

System Message: ERROR/3 (<string>, line 28)

Inconsistent literal block quoting.

'Hello world!' >>> 'foo' in box True >>> 'bar' in box False >>> box['bar'] Traceback (most recent call last): ... KeyError: 'bar' >>> box.get('bar', None) None

As mentioned above, you might want to use the box to store the current request object. Pandora ships with a custom middleware for this purpose. Just append 'pandora.middleware.RequestMiddleware' to your MIDDLEWARE_CLASSES setting. You can then access the request with box['request'].

There is also another middleware that stores the current user object in box['user']. To use this, add 'pandora.middleware.UserMiddleware' to your MIDDLEWARE_CLASSES setting, but make sure that it is listed after 'django.contrib.auth.middleware.AuthenticationMiddleware'.

Tips
  • You don't need to import the box object into your module if you think the name

System Message: WARNING/2 (<string>, line 54)

Bullet list ends without a blank line; unexpected unindent.

is too generic and might clash with some of your local names. Just use

>>> import pandora
>>> pandora.box['request']

System Message: ERROR/3 (<string>, line 58)

Inconsistent literal block quoting.

...

  • Even if opening Pandora's box might not seem too evil at the first glance,

System Message: WARNING/2 (<string>, line 61)

Bullet list ends without a blank line; unexpected unindent.

please try to avoid it and pass the request object or any other dependency around where possible. Having a global state makes things more difficult for everyone - especially to reliably test your code.

  • Read the `Wikipedia article about Pandora's box

    System Message: WARNING/2 (<string>, line 65); backlink

    Inline interpreted text or phrase reference start-string without end-string.

System Message: WARNING/2 (<string>, line 66)

Bullet list ends without a blank line; unexpected unindent.

<http://en.wikipedia.org/wiki/Pandora's_box>`_.

Changlog

0.1.0

Initial release.

Subscribe to package updates

Last updated Jan 5th, 2011

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.