[SQL-CVS] r657 - in trunk/SQLObject/sqlobject: . tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2005-03-01 13:05:08
|
Author: phd Date: 2005-03-01 13:04:43 +0000 (Tue, 01 Mar 2005) New Revision: 657 Modified: trunk/SQLObject/sqlobject/sresults.py trunk/SQLObject/sqlobject/tests/test_minmax.py Log: accumulateMany() usage is now even simpler: select.accumulateMany(*atributes). Modified: trunk/SQLObject/sqlobject/sresults.py =================================================================== --- trunk/SQLObject/sqlobject/sresults.py 2005-03-01 09:40:03 UTC (rev 656) +++ trunk/SQLObject/sqlobject/sresults.py 2005-03-01 13:04:43 UTC (rev 657) @@ -1,6 +1,8 @@ import sqlbuilder import dbconnection +StringType = type('') + class SelectResults(object): IterationClass = dbconnection.Iteration @@ -147,41 +149,39 @@ count = min(self.ops['end'] - self.ops.get('start', 0), count) return count - def _accumulateOne(self, func_name, attribute): + def accumulateMany(self, *attributes): + """ Making the expressions for count/sum/min/max/avg + of a given select result attributes. + `attributes` must be a list/tuple of pairs (func_name, attribute); + `attribute` can be a column name (like 'a_column') + or a dot-q attribute (like Table.q.aColumn) + """ + expressions = [] + for func_name, attribute in attributes: + if type(attribute) == StringType: + expression = '%s(%s)' % (func_name, attribute) + else: + expression = getattr(sqlbuilder.func, func_name)(attribute) + expressions.append(expression) + return self.accumulate(*expressions) + + def accumulateOne(self, func_name, attribute): """ Making the sum/min/max/avg of a given select result attribute. `attribute` can be a column name (like 'a_column') or a dot-q attribute (like Table.q.aColumn) """ - return self.accumulate(*accumulateMany((func_name, attribute))) + return self.accumulateMany((func_name, attribute)) def sum(self, attribute): - return self._accumulateOne("SUM", attribute) + return self.accumulateOne("SUM", attribute) def min(self, attribute): - return self._accumulateOne("MIN", attribute) + return self.accumulateOne("MIN", attribute) def avg(self, attribute): - return self._accumulateOne("AVG", attribute) + return self.accumulateOne("AVG", attribute) def max(self, attribute): - return self._accumulateOne("MAX", attribute) + return self.accumulateOne("MAX", attribute) -StringType = type('') - -def accumulateMany(*attributes): - """ Making the expressions for sum/min/max/avg - of a given select result attributes. - `attributes` must be a list/tuple of pairs (func_name, attribute); - `attribute` can be a column name (like 'a_column') - or a dot-q attribute (like Table.q.aColumn) - """ - expressions = [] - for func_name, attribute in attributes: - if type(attribute) == StringType: - expression = '%s(%s)' % (func_name, attribute) - else: - expression = getattr(sqlbuilder.func, func_name)(attribute) - expressions.append(expression) - return expressions - -__all__ = ['SelectResults', 'accumulateMany'] +__all__ = ['SelectResults'] Modified: trunk/SQLObject/sqlobject/tests/test_minmax.py =================================================================== --- trunk/SQLObject/sqlobject/tests/test_minmax.py 2005-03-01 09:40:03 UTC (rev 656) +++ trunk/SQLObject/sqlobject/tests/test_minmax.py 2005-03-01 13:04:43 UTC (rev 657) @@ -1,5 +1,4 @@ from sqlobject import * -from sqlobject.sresults import * from sqlobject.tests.dbtest import * # Test MIN, AVG, MAX, SUM @@ -55,5 +54,6 @@ select = IntAccumulator.select() attribute = IntAccumulator.q.value - expression = accumulateMany(("MIN", attribute), ("AVG", attribute), ("MAX", attribute)) - assert select.accumulate(*expression) == (1, 2, 3) + assert select.accumulateMany( + ("MIN", attribute), ("AVG", attribute), ("MAX", attribute) + ) == (1, 2, 3) |