-1

I have a collection of properties. some of these properties are given review score. review is saved in AggregateReview table. I have to sort these properties on basis of their score. Property with highest review score will come first.When all properties with review score will be sorted, the i have to append those property which aren't reviewed. This is the code i have written, It's working fine....but i want to know is there anything in this code, that can be optimized, I am new to app engine and ndb, so any help will be appreciated. ( I think there are so many calls to db )...

sortedProperties = []
sortedProperties.extend(sorted([eachProperty for eachProperty in properties if AggregateReview.query(AggregateReview.property == eachProperty.key).get()],key=lambda property: AggregateReview.query(AggregateReview.property == property.key).get().rating,reverse=True))
sortedProperties.extend([eachProperty for eachProperty in properties if AggregateReview.query(AggregateReview.property == eachProperty.key).get() is None])
return sortedProperties

after bit of workaround i came to this:

return sorted(properties,key=lambda property: property.review_aggregate.get().average,reverse=True)

but it throws an error : 'NoneType' object has no attribute 'average'

because it can not find the review_aggregate for every property. I want it to accept None....

Subodh Deoli
  • 87
  • 1
  • 7

1 Answers1

0

I am not sure what's going on in your code there, to be honest, as the lines are soo long. But I know that you may want to look into Memcached. It can be used to minimize hits on a database, and is widely used indeed. It is actually built into Google app engine. Read the docs on it here

Totem
  • 7,189
  • 5
  • 39
  • 66