TupleDict.py

#!/usr/bin/env python

"""
$Id: TupleDict.py 277 2001-11-05 13:51:05Z david $

This module contains the TupleDict class, used to create dictionaries
that support the retrieving of values using tuples.

For example:
	td = TupleDict( { "X" : 1, "Y" : 2, "Z" : 3 } )
	x, y, z = td["X", "Y", "Z"]
"""

from types import TupleType
from UserDict import UserDict


class TupleDict (UserDict):
	"""
	Allows you to create a special dictionary that supports
	the retrieval of multiple elements at once as a tuple.
	"""
	def __init__ (self, data={}):
		UserDict.__init__(self)
		self.data = data

	def __getitem__ (self,key):
		if type(key) is TupleType:
			result = []
			for k in key:
				result.append(self[k])
			return tuple(result)
		return self.data[key]