[storm] String Representation of keys
Christian Klinger
cklinger at novareto.de
Sat Jan 5 10:20:14 GMT 2008
Hi,
first it´s hard for me to explain what i need. So i hope you can
understand my problems.
I try to develope a "storm container" for zope.
I use the zalchemy product as a base for my work with storm.
I run into some Problems when i try to build a uri for my objects.
An example:
http://localhost/app/Person-(1,)
In uri consists of these relevant parts for me:
app is the container
Person is the classname of my Object
(1,) these are the primary keys of my object
Currently i construct the primary key in this way:
def _toStringIdentifier(self, obj):
""" Construct the PrimaryKey"""
store = getUtility(IZStorm).get(self.getStoreUtilityName())
ident = self._getInstanceKey(obj)
prefix = "%s-" %self._class.__name__
return "%s%s" %(prefix, ident)
def _getInstanceKey(self, obj):
""" Get an unique id form obj """
obj_info = get_obj_info(obj)
id = tuple(obj_info.variables[column].get() for column in
obj_info.cls_info.primary_key)
return id
The code for getting an object back looks like this:
def _fromStringIdentifier(self, name):
prefix = "%s-" %self._class.__name__
if not str(name).startswith(prefix):
return None
store = getUtility(IZStorm).get(self.getStoreUtilityName())
id = name[ len(str(self._class.__name__)) + 1: ]
key = self.str2tuple(id)
return store.get(self._class, key)
def str2tuple(self,s):
"""Convert tuple-like strings to real tuples.
eg '(1,2,3,4)' -> (1, 2, 3, 4)
"""
if s[0] + s[-1] != "()":
raise ValueError("Badly formatted string (missing brackets).")
items = s[1:-1] # removes the leading and trailing brackets
if items.endswith(','):
items = items[:-1]
items = items.split(',')
L = [int(x) for x in items] # clean up spaces, convert to ints
return tuple(L)
I know this is very ugly but i don´t find a better way for doing this
maybe someone of you has an better idea of doing this.
I have created a doctest for the container maybe this gives you
a better impression how it works and what the problems are.
http://stormcontainer.googlecode.com/svn/trunk/src/megrok/storm/tests/stormcontainer.txt
Christian
More information about the storm
mailing list