[Grok-dev] Modeling a class that extend from list() with megrok.rdb

Hector Blanco white.lists at gmail.com
Mon Oct 25 18:02:01 EDT 2010


Hi list!

I am trying to migrate my application from ZopeDB to MySql. I am using
megrok.rdb.

I have a class which inherits from list() and has a few extra methods.

That class acts as a container of other classes and is used (as an
attribute) in other classes as well:

class MyEntry(object):
	def __init__(self):
		self.field1 = “field1”
		self.field2 = “field2”

class MyContainer(list):
	def __init__(self):
		super(MyContainer,self).__init__()

	def append(myEntry):
		if isinstance(myEntry, MyEntry):
			super(MyContainer, self).append(myEntry)

	def getByField1(self, field1):
		for element in self:
			if element.field1 == field1:
				return element
		return None
	
	[ ... more useful methods ...]

class MyClass(object):
	def __init__(self):
		self.container1 = MyContainer()
		self.container2 = MyContainer()
		self.anotherField = “hello world”
	def getContainer1():
		return self.container1
	def getContainer2():
		return self.container2

I don't really know how to model this with megrok.rdb. I kind of see
clearly (although I may be totally wrong) that MyEntry and MyClass
should extend from rdb.Model and each of them should be stored in a
table, but I don't really know what to do with “MyContainer”. I'd like
to keep it inheriting from list, but I don't know how to model that.
It really only needs to be an association between [id, id_of_my_entry]
(relationship 1:N). In MyClass, each of the containers would get an id
and then, if I want to get the “MyEntries” that are in container1, I
just need to get the MyEntries who are linked to that id.

… but I have no idea about how to implement that... Any hint I could
get would be deeply appreciated.

Thank you in advance!


More information about the Grok-dev mailing list