Accessing parts of repeated structured property as a list

359 views
Skip to first unread message

Robellard, Michael

unread,
Jan 9, 2012, 2:54:17 AM1/9/12
to appengine-...@googlegroups.com
Is there an easy way to access the list of an element in a repeated
structured property?

To be more exact what I want to do is this:

class FamilyMember(model.Model):
individual = model.KeyProperty()
relation = model.StringProperty(choices=utils.relations)

class Family(Node):
children = model.StructuredProperty(FamilyMember, repeated=True)

#What I am doing now:
isChild = False
for child in family.children:
if ind == child.individual:
isChild = True

#What I would like to do
isChild = ind in family.(some easy way to access the list of
children key properties)

Does my question make sense?
--
Michael Robellard
(216)288-2745
Play Railroad Empire: http://www.therailroadempire.com

Guido van Rossum

unread,
Jan 9, 2012, 5:31:49 PM1/9/12
to appengine-...@googlegroups.com, mi...@robellard.com
Your question makes sense, but there's no way to do that. It's more a
Python issue than an NDB issue -- in general there is no support for
projections like this. At least, I think what you are describing is
called a projection.

You want a more compact way to spell [child.individual for child in
family.children] and you're somehow hoping that something like
family.children.individual would automagically return the above list.

Alas, in order to support that, family.children would have to return
some custom sequence class that knew about the structure of the
FamilyMember class. But there are several problems with that,
including what should have priority if FamilyMember were to have a
property named 'append' or 'sort' or any other list method.

So, the short answer is 'no', and I don't think it would be a good
idea to add this feature. But thanks for asking, you never know!

--Guido

--
--Guido van Rossum (python.org/~guido)

Peter Retief

unread,
Jan 9, 2012, 5:48:31 PM1/9/12
to appengine-...@googlegroups.com
since ndb is a bit experimental cant we imagine ideal scenarios?

if i were to dream of an ideal relationship model it would probably be
something like:

class Family(Node):

class Family.FamilyMember(model.Model):

class Family.FamilyMember.ContactInfo(model.Model):

class Family.FamilyPets(model.Model):

etc

Guido van Rossum

unread,
Jan 9, 2012, 6:02:34 PM1/9/12
to appengine-...@googlegroups.com
That specific syntax isn't supported by Python. Can you make a
suggestion that would actually be valid syntax? Also, if you want me
to look at a suggestion, please show me your proposed syntax as well
as how you would write the same thing in existing NDB -- I cannot
guess what meaning you'd like it to have from your syntax proposal
alone.

--Guido

Peter Retief

unread,
Jan 9, 2012, 6:12:51 PM1/9/12
to appengine-...@googlegroups.com
apologies - a bit of a wild answer on my part

i will consider your suggestions and respond in time
(I am new to python)

-Ptr

Robert Vos

unread,
Jan 10, 2013, 12:50:33 PM1/10/13
to appengine-...@googlegroups.com
It would be nice, if you can use Python list index to find a list element in a repeated structured property.
See my question on Stackoverflow: http://stackoverflow.com/questions/14243798/how-to-find-an-instance-in-an-ndb-repeated-structured-property 

Op maandag 9 januari 2012 19:02:34 UTC+1 schreef Guido van Rossum het volgende:

Guido van Rossum

unread,
Jan 10, 2013, 4:34:56 PM1/10/13
to appengine-...@googlegroups.com

If I understand your SO question correctly this is a matter of understanding how Python works and how NDB represents repeated StructuredProperty, not a feature request.

--Guido van Rossum (sent from Android phone)

Reply all
Reply to author
Forward
0 new messages