#NEWLINE# @ classmethod #NEWLINE# def query( cls , expression = None , order_by = None ) : #NEWLINE# #INDENT# "\n        Return model instances matching the given expression (if\n        specified). Additionally, matching instances can be returned\n        sorted by field value.\n\n        Example::\n\n            # Get administrators sorted by username.\n            admin_users = User.query(\n                (User.admin == True),\n                order_by=User.username)\n\n            # List blog entries newest to oldest.\n            entries = Entry.query(order_by=Entry.timestamp.desc())\n\n            # Perform a complex filter.\n            values = StatData.query(\n                (StatData.timestamp < datetime.date.today()) &\n                ((StatData.type == 'pv') | (StatData.type == 'cv')))\n\n        :param expression: A boolean expression to filter by.\n        :param order_by: A field whose value should be used to\n            sort returned instances.\n        " #NEWLINE# if ( expression is not None ) : #NEWLINE# #INDENT# executor = Executor ( cls . database ) #NEWLINE# result = executor . execute ( expression ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# result = cls . _query . all_index ( ) #NEWLINE# #UNINDENT# if ( order_by is not None ) : #NEWLINE# #INDENT# desc = False #NEWLINE# if isinstance ( order_by , Desc ) : #NEWLINE# #INDENT# desc = True #NEWLINE# order_by = order_by . node #NEWLINE# #UNINDENT# alpha = ( not isinstance ( order_by , _ScalarField ) ) #NEWLINE# result = cls . database . sort ( result . key , by = ( '*->%s' % order_by . name ) , alpha = alpha , desc = desc ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( result , ZSet ) : #NEWLINE# #INDENT# result = result . iterator ( reverse = True ) #NEWLINE# #UNINDENT# #UNINDENT# for hash_id in result : #NEWLINE# #INDENT# ( yield cls . load ( hash_id , convert_key = False ) )