Referring to same interface using zope.schema.Object
Hello, I am trying to construct an object tree. Every node in the tree is of the same type. I am trying to achieve something like: class INode(Interface): parent = Object( title=u"Parent node", schema=INode ) children = List( title=u'Child nodes', value_type=Object(schema=INode) ) The above fails with "NameError: name 'INode' is not defined". Any clues as to how to solve this? Regards, Joe -- Joe Steeve HiPro IT Solutions Pvt. Ltd. http://hipro.co.in/
On Fri, Jul 22, 2011 at 04:29:32PM +0530, Joe Steeve wrote:
Hello,
I am trying to construct an object tree. Every node in the tree is of the same type. I am trying to achieve something like:
class INode(Interface):
parent = Object( title=u"Parent node", schema=INode )
children = List( title=u'Child nodes', value_type=Object(schema=INode) )
The above fails with "NameError: name 'INode' is not defined". Any clues as to how to solve this?
This would be my first guess: class INode(Interface): pass INode.parent = Object( title=u"Parent node", schema=INode ) INode.children = List( title=u'Child nodes', value_type=Object(schema=INode) ) -- Brian Sutherland
On 2011-07-22 13:26, Brian Sutherland wrote:
This would be my first guess:
class INode(Interface): pass
INode.parent = Object( title=u"Parent node", schema=INode )
INode.children = List( title=u'Child nodes', value_type=Object(schema=INode) )
And that guess would be wrong. You can't add fields to an existing schema like that (not sure if you can in other ways). You *can* change an existing field however, so a working solution would be: class INode(Interface): parent = Object( title = u'Parent node', schema = Interface, # set to INode later ) children = List( title = u'Child nodes', value_type = Object(schema=Interface), # set to INode later ) INode['parent'].schema = INode INode['children'].value_type.schema = INode Hope this helps - Jacob
Hello, On Fri, 2011-07-22 at 13:41 +0200, Jacob Holm wrote:
On 2011-07-22 13:26, Brian Sutherland wrote:
This would be my first guess:
class INode(Interface): pass
INode.parent = Object( title=u"Parent node", schema=INode )
INode.children = List( title=u'Child nodes', value_type=Object(schema=INode) )
The method suggested by Brian works without any issues. :)
And that guess would be wrong. You can't add fields to an existing schema like that (not sure if you can in other ways). You *can* change an existing field however, so a working solution would be:
class INode(Interface):
parent = Object( title = u'Parent node', schema = Interface, # set to INode later )
children = List( title = u'Child nodes', value_type = Object(schema=Interface), )
INode['parent'].schema = INode INode['children'].value_type.schema = INode
I thought this also should work without any issues. But when I ran the debug shell to list out the attributes of INode using dir(INode) I couldn't find the 'parent' and 'children' attributes in it. Even worse part is, if there is another field say name = TextLine(title=u'Node name') This 'name' attribute is also is not visible along with 'parent' and 'children' attributes. Can someone explain why is this so? (Even though the problem gets solved by Brian's method, just curious to know) -- Joshua Immanuel HiPro IT Solutions Private Limited http://hipro.co.in
On 2011-07-22 14:32, Joshua Immanuel wrote:
Hello,
On Fri, 2011-07-22 at 13:41 +0200, Jacob Holm wrote:
On 2011-07-22 13:26, Brian Sutherland wrote:
This would be my first guess:
class INode(Interface): pass
INode.parent = Object( title=u"Parent node", schema=INode )
INode.children = List( title=u'Child nodes', value_type=Object(schema=INode) )
The method suggested by Brian works without any issues. :)
It "works" only in the the sense that it doesn't throw an exception. It does not define the desired schema.
And that guess would be wrong. You can't add fields to an existing schema like that (not sure if you can in other ways). You *can* change an existing field however, so a working solution would be:
class INode(Interface):
parent = Object( title = u'Parent node', schema = Interface, # set to INode later )
children = List( title = u'Child nodes', value_type = Object(schema=Interface), )
INode['parent'].schema = INode INode['children'].value_type.schema = INode
I thought this also should work without any issues.
Trust me, it does.
But when I ran the debug shell to list out the attributes of INode using dir(INode) I couldn't find the 'parent' and 'children' attributes in it.
The fields of the schema are not *supposed* to show up in "dir()". Despite the use of the "class" statement to define them, interfaces are *not* classes. They are instances of zope.interface.interface.InterfaceClass, and act like (read-only) containers of their fields. Try using 'list(Inode)' for testing and you will see that "my" method works as expected.
Even worse part is, if there is another field say
name = TextLine(title=u'Node name')
This 'name' attribute is also is not visible along with 'parent' and 'children' attributes.
Can someone explain why is this so?
Right. As explained above, it isn't supposed to.
(Even though the problem gets solved by Brian's method, just curious to know)
I hope to have convinced you by now that it really isn't solved by Brian's method, but by mine. (Although I wouldn't call it "my" method as such. I'm sure I have taken it from somewhere else) - Jacob
On 22 July 2011 13:32, Joshua Immanuel <josh@hipro.co.in> wrote:
Hello,
On Fri, 2011-07-22 at 13:41 +0200, Jacob Holm wrote:
On 2011-07-22 13:26, Brian Sutherland wrote:
This would be my first guess:
class INode(Interface): pass
INode.parent = Object( title=u"Parent node", schema=INode )
INode.children = List( title=u'Child nodes', value_type=Object(schema=INode) )
The method suggested by Brian works without any issues. :)
No, there are issues. Take this example:
class ITest(Interface): ... title = schema.TextLine() ... ITest.names <bound method InterfaceClass.names of <InterfaceClass __main__.ITest>> ITest.names() ['title'] ITest.description = schema.Text() ITest.names() ['title']
The fields on an interface are not stored as attributes, but are accessible using item access, e.g: ITest['title']. You cannot assign that way though:
ITest['description'] = schema.TextLine() Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: 'InterfaceClass' object does not support item assignment
Adding to an interface requires messing with the '_InterfaceClass__attrs' attribute of the dictionary and is discouraged. Laurence
Hello Laurence, On Fri, 2011-07-22 at 13:57 +0100, Laurence Rowe wrote:
No, there are issues. Take this example:
class ITest(Interface): ... title = schema.TextLine() ... ITest.names <bound method InterfaceClass.names of <InterfaceClass __main__.ITest>> ITest.names() ['title'] ITest.description = schema.Text() ITest.names() ['title']
The fields on an interface are not stored as attributes, but are accessible using item access, e.g: ITest['title']. You cannot assign that way though:
ITest['description'] = schema.TextLine() Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: 'InterfaceClass' object does not support item assignment
Adding to an interface requires messing with the '_InterfaceClass__attrs' attribute of the dictionary and is discouraged.
Thanks for enlightening me with this wonderful explanation. As Jacob mentioned, list(INode) in debug shell was very handy to debug. -- Joshua Immanuel HiPro IT Solutions Private Limited http://hipro.co.in
Hello Laurence, On Fri, 2011-07-22 at 13:57 +0100, Laurence Rowe wrote:
No, there are issues. Take this example:
class ITest(Interface): ... title = schema.TextLine() ... ITest.names <bound method InterfaceClass.names of <InterfaceClass __main__.ITest>> ITest.names() ['title'] ITest.description = schema.Text() ITest.names() ['title']
The fields on an interface are not stored as attributes, but are accessible using item access, e.g: ITest['title']. You cannot assign that way though:
ITest['description'] = schema.TextLine() Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: 'InterfaceClass' object does not support item assignment
Adding to an interface requires messing with the '_InterfaceClass__attrs' attribute of the dictionary and is discouraged.
Thanks for enlightening me with this wonderful explanation. As Jacob mentioned, list(INode) in debug shell was very handy to debug. -- Joshua Immanuel HiPro IT Solutions Private Limited http://hipro.co.in
On Fri, Jul 22, 2011 at 01:41:32PM +0200, Jacob Holm wrote:
On 2011-07-22 13:26, Brian Sutherland wrote:
This would be my first guess:
And that guess would be wrong.
Thanks. Guess I should stop guessing;) -- Brian Sutherland
Hi, On 22.07.2011 12:59, Joe Steeve wrote:
Hello,
I am trying to construct an object tree. Take a look at http://pypi.python.org/pypi/node This is probably what you need.
Regards, Robert
Every node in the tree is of the same type. I am trying to achieve something like:
class INode(Interface):
parent = Object( title=u"Parent node", schema=INode )
children = List( title=u'Child nodes', value_type=Object(schema=INode) )
The above fails with "NameError: name 'INode' is not defined". Any clues as to how to solve this?
Regards, Joe
_______________________________________________ Zope-Dev maillist - Zope-Dev@zope.org https://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - https://mail.zope.org/mailman/listinfo/zope-announce https://mail.zope.org/mailman/listinfo/zope )
-- Robert Niederreiter Squarewave Computing Aflingerstraße 19 A-6176 Völs Tel: +43 699 160 20 192 Web: http://www.squarewave.at
Am 22.07.2011 um 15:30 schrieb Robert Niederreiter:
Hi,
On 22.07.2011 12:59, Joe Steeve wrote:
Hello,
I am trying to construct an object tree.
Take a look at http://pypi.python.org/pypi/node This is probably what you need.
or even http://pypi.python.org/pypi/zope.container might be enough. Mit freundlichen Grüßen -- Michael Howitz · mh@gocept.com · Softwareentwickler gocept gmbh & co. kg · Forsterstraße 29 · 06112 Halle (Saale) · Deutschland http://gocept.com · Tel +49 345 1229889 8 · Fax +49 345 1229889 1 Zope- und Plone-Beratung und -Entwicklung
participants (7)
-
Brian Sutherland -
Jacob Holm -
Joe Steeve -
Joshua Immanuel -
Laurence Rowe -
Michael Howitz -
Robert Niederreiter