[google-appengine] Re: calculated properties

2009-02-10 Thread David Symonds

On Tue, Feb 10, 2009 at 9:45 AM, Jason DeFontes  wrote:
>
> Is there a simple way to have a calculated property that automatically
> updates itself any time an entity is saved? For example:
>
> class Article(db.Model):
>  body = db.StringProperty()
>  word_count = db.IntegerProperty()

You should override the put method:

class Article(db.Model):
  body = db.StringProperty()
  word_count = db.IntegerProperty()

  def put(self):
self.word_count = ComputeWordCount(self.body)  # or whatever
return super(Article, self).put()



Dave.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: calculated properties

2009-02-10 Thread Alexander Kojevnikov

On Feb 10, 7:06 pm, David Symonds  wrote:
> On Tue, Feb 10, 2009 at 9:45 AM, Jason DeFontes  wrote:
>
> > Is there a simple way to have a calculated property that automatically
> > updates itself any time an entity is saved? For example:
>
> > class Article(db.Model):
> >  body = db.StringProperty()
> >  word_count = db.IntegerProperty()
>
> You should override the put method:
>
> class Article(db.Model):
>   body = db.StringProperty()
>   word_count = db.IntegerProperty()
>
>   def put(self):
>     self.word_count = ComputeWordCount(self.body)  # or whatever
>     return super(Article, self).put()
>
> Dave.

Just be aware that Model.put() won't be called when you batch-save
your entities with db.put() function.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: calculated properties

2009-02-10 Thread Andy Freeman

Look at db.DateTimeProperty, specifically its definition of
get_value_for_datastore.

get_value_for_datastore has access to model_instance so it can look at
other properties.

You should think about how your property knows the name of the other
property that it looks at.

On Feb 9, 2:45 pm, Jason DeFontes  wrote:
> Is there a simple way to have a calculated property that automatically
> updates itself any time an entity is saved? For example:
>
> class Article(db.Model):
>   body = db.StringProperty()
>   word_count = db.IntegerProperty()
>
> I'd like to put some code in the Article class such that word_count is
> automatically updated any time body is modified.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: calculated properties

2009-02-10 Thread Jason DeFontes

Thanks for the suggestions. Neither option seems ideal to me:
overriding Model.put() has the db.put() hole; creating a subclass of
Property seems like a lot of leakage for logic that should really
belong in the Model class. Oh well, I will deal if that's all there is.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: calculated properties

2009-02-10 Thread Andy Freeman

Since you're creating a property whose value really does depend on the
value of another property, it's unclear (to me) why you think that
that dependence shouldn't be an aspect of the dependent property, that
is, something that is part of its descriptor.

That said, it's more than a little annoying that you have to type the
name of the independent property twice, once in its definition and a
second time in the dependent property's descriptor.

On Feb 10, 9:27 am, Jason DeFontes  wrote:
> Thanks for the suggestions. Neither option seems ideal to me:
> overriding Model.put() has the db.put() hole; creating a subclass of
> Property seems like a lot of leakage for logic that should really
> belong in the Model class. Oh well, I will deal if that's all there is.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: calculated properties

2009-02-11 Thread Jason DeFontes

Because, if I just had a plain-old python class, I'd be able to
implement the dependency in the class itself, I wouldn't have to write
a separate property class to get that behavior. I'm not going to lose
any sleep over it though.

On Feb 10, 6:05 pm, Andy Freeman  wrote:
> Since you're creating a property whose value really does depend on the
> value of another property, it's unclear (to me) why you think that
> that dependence shouldn't be an aspect of the dependent property, that
> is, something that is part of its descriptor.
>
> That said, it's more than a little annoying that you have to type the
> name of the independent property twice, once in its definition and a
> second time in the dependent property's descriptor.
>
> On Feb 10, 9:27 am, Jason DeFontes  wrote:
>
> > Thanks for the suggestions. Neither option seems ideal to me:
> > overriding Model.put() has the db.put() hole; creating a subclass of
> > Property seems like a lot of leakage for logic that should really
> > belong in the Model class. Oh well, I will deal if that's all there is.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---