[Modeling-cvs] ProjectModeling/Modeling SortOrdering.py,1.5,1.6 __init__.py,1.4,1.5
Status: Abandoned
Brought to you by:
sbigaret
From: <sbi...@us...> - 2004-02-09 21:09:22
|
Update of /cvsroot/modeling/ProjectModeling/Modeling In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13019/Modeling Modified Files: SortOrdering.py __init__.py Log Message: Fixed SortOrdering.sortOrderingsWithString(). Updated documentation Index: SortOrdering.py =================================================================== RCS file: /cvsroot/modeling/ProjectModeling/Modeling/SortOrdering.py,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** SortOrdering.py 4 Jul 2003 16:05:07 -0000 1.5 --- SortOrdering.py 9 Feb 2004 21:06:05 -0000 1.6 *************** *** 21,33 **** # #----------------------------------------------------------------------------- ! """ ! SortOrdering ! CVS Information ! $Id$ """ --- 21,51 ---- # #----------------------------------------------------------------------------- + """ + Module SortOrdering holds a "command" pattern for sorting objects, and the + appropriate methods for building and using the commands. + A command for sorting is an instance of L{SortOrdering}; it can be built + either directly or with the method L{sortOrderingsWithString}. A command can + be used alone or inserted in a sequence of sorting command; in the later case, + the resulting sorting method simply consists in sorting objects with the first + method in the sequence, than sorting the objects which are equal wrt the first + method using the second one in the sequence, etc. ! A command consists in a key and an operator (see below: L{compareAscending}, ! ...); the sorting mechanism uses ! L{KeyValueCoding.valueForKeyPath<KeyValueCoding.KeyValueCoding.valueForKeyPath>} ! with the command's key to access the values for objects' attributes. ! Sorted objects must implement the KeyValueCoding interface's method ! L{KeyValueCoding.valueForKeyPath<KeyValueCoding.KeyValueCoding.valueForKeyPath>}): ! it is used to access objects' values. ! Example of use: ! >>> from Modeling.SortOrdering import sortOrderingsWithString, sortedArrayUsingKeyOrderArray ! >>> name_age=sortOrderingsWithString('lastName, age desc') ! >>> sortedArrayUsingKeyOrderArray(self.authors, name_age) ! ! CVS Information: $Id$ """ *************** *** 35,39 **** from interfaces.SortOrdering import SortOrderingInterface - from string import upper from utils import staticmethod --- 53,56 ---- *************** *** 41,44 **** --- 58,62 ---- # Predefined Operators def compareAscending(value1, value2): + "This operator allows to sort values in ascending order" if value1 < value2: return -1 elif value1 == value2: return 0 *************** *** 46,49 **** --- 64,68 ---- def compareDescending(value1, value2): + "This operator allows to sort values in descending order" if value1 > value2: return -1 elif value1 == value2: return 0 *************** *** 51,55 **** def compareCaseInsensitiveAscending(value1, value2): ! up1=upper(value1); up2=upper(value2) if up1 < up2: return -1 elif up1 == up2: return 0 --- 70,75 ---- def compareCaseInsensitiveAscending(value1, value2): ! "This operator allows to sort strings in ascending order, case-insensitively" ! up1=value1.upper(); up2=value2.upper() if up1 < up2: return -1 elif up1 == up2: return 0 *************** *** 57,61 **** def compareCaseInsensitiveDescending(value1, value2): ! up1=upper(value1); up2=upper(value2) if up1 > up2: return -1 elif up1 == up2: return 0 --- 77,82 ---- def compareCaseInsensitiveDescending(value1, value2): ! "This operator allows to sort strings in descending order, case-insensitively" ! up1=value1.upper(); up2=value2.upper() if up1 > up2: return -1 elif up1 == up2: return 0 *************** *** 63,67 **** def operatorFromString(str): ! op=strip(str.lower()) if op == 'asc': return compareAscending if op == 'iasc': return compareCaseInsensitiveAscending --- 84,92 ---- def operatorFromString(str): ! """ ! Converts a string to the corresponding operator. Recognized strings are: ! 'asc', 'desc' and their case-insensitive counterparts: 'iasc' and 'idesc' ! """ ! op=str.lower().strip() if op == 'asc': return compareAscending if op == 'iasc': return compareCaseInsensitiveAscending *************** *** 71,75 **** # Module functions ! def __compareUsingKeyOrderArray__(object1, object2, sortOrderings): for ordering in sortOrderings: key=ordering.key() --- 96,100 ---- # Module functions ! def _compareUsingKeyOrderArray(object1, object2, sortOrderings): for ordering in sortOrderings: key=ordering.key() *************** *** 80,84 **** def sortedArrayUsingKeyOrderArray(array, sortOrderings): ! comp=lambda o1, o2, orderings=sortOrderings:__compareUsingKeyOrderArray__(o1,o2,orderings) result=list(array) result.sort(comp) --- 105,122 ---- def sortedArrayUsingKeyOrderArray(array, sortOrderings): ! """ ! Sorts array with respect to the list of sort commands supplied in ! sortOrderings. Object's values are accessed using L{SortOrdering}.key ! and L{KeyValueCoding.valueForKeyPath}. ! ! @parameter array: a sequence of objects implementing ! L{KeyValueCoding.KeyValueCoding.valueForKeyPath} ! ! @parameter sortOrderings: a sequence of L{SortOrdering} commands. ! ! @return: the sorted array ! ! """ ! comp=lambda o1, o2, orderings=sortOrderings:_compareUsingKeyOrderArray(o1,o2,orderings) result=list(array) result.sort(comp) *************** *** 86,114 **** def sortOrderingWithKey(key, operator): return SortOrdering(key, operator) def sortOrderingsWithString(str): ! import string sorts=[] ! parts=string.split(str,',') for part in parts: ! part=string.strip(part) ! key_order=string.split(part) ! if len(key_order==1): ! key, order=key_order, 'asc' ! elif len(key_order)==2: ! key, order=key_order[0], operatorFromString(key_order[1]) else: raise ValueError, "Unrecognized pattern '%s' in '%s'"%(part, str) ! sorts.append(SortOrdering(key,operator)) return sorts # class SortOrdering: __implements__ = (SortOrderingInterface,) def __init__(self, key, operator): self._key=key self._operator=operator --- 124,173 ---- def sortOrderingWithKey(key, operator): + "Alternate constructor for a L{SortOrdering}" return SortOrdering(key, operator) def sortOrderingsWithString(str): ! """ ! Builds a sequence of SortOrdering from the supplied string. ! A valid string is: C{<key1> [<operator1>], ..., <key_n> [<operator_n>]} ! ! Valid operators are the one accepted by L{operatorFromString}. When ! C{<operator>} is omitted, it defaults to C{'asc'}. ! ! Example of a valid string: C{"name, age desc"} meaning; sort by name ! (ascending), then by age (descending). ! ! """ sorts=[] ! parts=str.split(',') for part in parts: ! part=part.strip() ! key_op=part.split() ! if len(key_op)==1: ! key, op=key_op[0], operatorFromString('asc') ! elif len(key_op)==2: ! key, op=key_op[0], operatorFromString(key_op[1]) else: raise ValueError, "Unrecognized pattern '%s' in '%s'"%(part, str) ! sorts.append(SortOrdering(key,op)) return sorts # class SortOrdering: + """ + The canonical sorting command has a key and an operator. The key is the name + of the attribute to use for sorting objects, the operator is the sorting + order (e.g. ascending, descending). Note: the key is in fact more general + than simply the name of an, attribute, since objects' values are accessed + through L{KeyValueCoding} --see L{sortedArrayUsingKeyOrderArray}. + """ + __implements__ = (SortOrderingInterface,) def __init__(self, key, operator): self._key=key + if type(operator) is type(''): + operator=operatorFromString(operator) self._operator=operator Index: __init__.py =================================================================== RCS file: /cvsroot/modeling/ProjectModeling/Modeling/__init__.py,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** __init__.py 6 Feb 2004 13:09:52 -0000 1.4 --- __init__.py 9 Feb 2004 21:06:05 -0000 1.5 *************** *** 21,26 **** # #----------------------------------------------------------------------------- ! """Empty file to make this directory a Python package.""" __version__="0.9-pre-16" --- 21,38 ---- # #----------------------------------------------------------------------------- + """The Modeling Framework is an Object-Relational Bridge for python ! The Modeling Framework fills the gap between the Python object world and ! relational databases in that it allows users to transparently create, ! retrieve, update, or delete Python objects from a database without having to ! write a single line of SQL. Its main features include generation of database ! schema, generation of Python code templates ready to be used, support for ! transparent mapping of (class) inheritance in relational databases, ! object-oriented query language, use of standard Python getters to traverse ! relationships (the related objects are automatically fetched when needed and ! when appropriate), and automatic checking for referential-integrity ! constraints, etc. Supported databases are MySQL, Oracle, PostgreSQL, and ! SQLite. ! """ __version__="0.9-pre-16" |