Commit ad4b3de1 authored by Andreas Jung's avatar Andreas Jung

merged Extended DTML Sorting implementation

parent 3fae5f04
......@@ -84,11 +84,12 @@
##############################################################################
'''Add security system support to Document Templates
$Id: DTML.py,v 1.3 2001/04/27 20:27:37 shane Exp $'''
__version__='$Revision: 1.3 $'[11:-2]
$Id: DTML.py,v 1.4 2001/05/23 14:42:04 andreas Exp $'''
__version__='$Revision: 1.4 $'[11:-2]
from DocumentTemplate import DT_Util
import SecurityManagement, string, math, whrandom, random
import DocumentTemplate.sequence
# Allow access to unprotected attributes
DT_Util.TemplateDict.__allow_access_to_unprotected_subobjects__=1
......@@ -97,6 +98,8 @@ math.__allow_access_to_unprotected_subobjects__=1
whrandom.__allow_access_to_unprotected_subobjects__=1
random.__allow_access_to_unprotected_subobjects__=1
DocumentTemplate.sequence.__allow_access_to_unprotected_subobjects__=1
# Add security testing capabilities
class DTMLSecurityAPI:
......
##############################################################################
#
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
#
# Copyright (c) Digital Creations. All rights reserved.
#
# This license has been certified as Open Source(tm).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions in source code must retain the above copyright
# notice, this list of conditions, and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. Digital Creations requests that attribution be given to Zope
# in any manner possible. Zope includes a "Powered by Zope"
# button that is installed by default. While it is not a license
# violation to remove this button, it is requested that the
# attribution remain. A significant investment has been put
# into Zope, and this effort will continue if the Zope community
# continues to grow. This is one way to assure that growth.
#
# 4. All advertising materials and documentation mentioning
# features derived from or use of this software must display
# the following acknowledgement:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# In the event that the product being advertised includes an
# intact Zope distribution (with copyright and license included)
# then this clause is waived.
#
# 5. Names associated with Zope or Digital Creations must not be used to
# endorse or promote products derived from this software without
# prior written permission from Digital Creations.
#
# 6. Modified redistributions of any form whatsoever must retain
# the following acknowledgment:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# Intact (re-)distributions of any official Zope release do not
# require an external acknowledgement.
#
# 7. Modifications are encouraged but must be packaged separately as
# patches to official Zope releases. Distributions that do not
# clearly separate the patches from the original work must be clearly
# labeled as unofficial distributions. Modifications which do not
# carry the name Zope may be packaged in any form, as long as they
# conform to all of the clauses above.
#
#
# Disclaimer
#
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations. Specific
# attributions are listed in the accompanying credits file.
#
##############################################################################
"""
Advanced sort support by Oleg Broytmann <phd@@phd.pp.ru> 23 Apr 2001
eg Sort(sequence, (("akey", "nocase"), ("anotherkey", "cmp", "desc")))
$Id: SortEx.py,v 1.2 2001/05/23 14:42:08 andreas Exp $
"""
from string import lower
TupleType=type(())
def sort(sequence, sort=(), _=None, mapping=0):
"""
- sequence is a sequence of objects to be sorted
- sort is a sequence of tuples (key,func,direction)
that define the sort order:
- key is the name of an attribute to sort the objects by
- func is the name of a comparison function. This parameter is
optional
allowed values:
- "cmp" -- the standard comparison function (default)
- "nocase" -- case-insensitive comparison
- "strcoll" or "locale" -- locale-aware string comparison
- "strcoll_nocase" or "locale_nocase" -- locale-aware case-insensitive
string comparison
- "xxx" -- a user-defined comparison function
- direction -- defines the sort direction for the key (optional).
(allowed values: "asc" (default) , "desc")
"""
need_sortfunc = 0
if sort:
for s in sort:
if len(s) > 1: # extended sort if there is reference to...
# ...comparison function or sort order, even if they are "cmp" and "asc"
need_sortfunc = 1
break
sortfields = sort # multi sort = key1,key2
multsort = len(sortfields) > 1 # flag: is multiple sort
if need_sortfunc:
# prepare the list of functions and sort order multipliers
sf_list = make_sortfunctions(sortfields, _)
# clean the mess a bit
if multsort: # More than one sort key.
sortfields = map(lambda x: x[0], sf_list)
else:
sort = sf_list[0][0]
elif sort:
if multsort: # More than one sort key.
sortfields = map(lambda x: x[0], sort)
else:
sort = sort[0][0]
isort=not sort
s=[]
for client in sequence:
k = None
if type(client)==TupleType and len(client)==2:
if isort: k=client[0]
v=client[1]
else:
if isort: k=client
v=client
if sort:
if multsort: # More than one sort key.
k = []
for sk in sortfields:
try:
if mapping: akey = v[sk]
else: akey = getattr(v, sk)
except AttributeError, KeyError: akey = None
if not basic_type(akey):
try: akey = akey()
except: pass
k.append(akey)
else: # One sort key.
try:
if mapping: k = v[sort]
else: k = getattr(v, sort)
except AttributeError, KeyError: k = None
if not basic_type(type(k)):
try: k = k()
except: pass
s.append((k,client))
if need_sortfunc:
by = SortBy(multsort, sf_list)
s.sort(by)
else:
s.sort()
sequence=[]
for k, client in s:
sequence.append(client)
return sequence
SortEx = sort
basic_type={type(''): 1, type(0): 1, type(0.0): 1, type(()): 1, type([]): 1,
type(None) : 1 }.has_key
def nocase(str1, str2):
return cmp(lower(str1), lower(str2))
import sys
if sys.modules.has_key("locale"): # only if locale is already imported
from locale import strcoll
def strcoll_nocase(str1, str2):
return strcoll(lower(str1), lower(str2))
def make_sortfunctions(sortfields, _):
"""Accepts a list of sort fields; splits every field, finds comparison
function. Returns a list of 3-tuples (field, cmp_function, asc_multplier)"""
sf_list = []
for field in sortfields:
f = list(field)
l = len(f)
if l == 1:
f.append("cmp")
f.append("asc")
elif l == 2:
f.append("asc")
elif l == 3:
pass
else:
raise SyntaxError, "sort option must contains no more than 2 fields"
f_name = f[1]
# predefined function?
if f_name == "cmp":
func = cmp # builtin
elif f_name == "nocase":
func = nocase
elif f_name in ("locale", "strcoll"):
func = strcoll
elif f_name in ("locale_nocase", "strcoll_nocase"):
func = strcoll_nocase
else: # no - look it up in the namespace
func = _.getitem(f_name, 0)
sort_order = lower(f[2])
if sort_order == "asc":
multiplier = +1
elif sort_order == "desc":
multiplier = -1
else:
raise SyntaxError, "sort oder must be either ASC or DESC"
sf_list.append((f[0], func, multiplier))
return sf_list
class SortBy:
def __init__(self, multsort, sf_list):
self.multsort = multsort
self.sf_list = sf_list
def __call__(self, o1, o2):
multsort = self.multsort
if multsort:
o1 = o1[0] # if multsort - take the first element (key list)
o2 = o2[0]
sf_list = self.sf_list
l = len(sf_list)
# assert that o1 and o2 are tuples of apropriate length
assert len(o1) == l + 1 - multsort, "%s, %d" % (o1, l + multsort)
assert len(o2) == l + 1 - multsort, "%s, %d" % (o2, l + multsort)
# now run through the list of functions in sf_list and
# compare every object in o1 and o2
for i in range(l):
# if multsort - we already extracted the key list
# if not multsort - i is 0, and the 0th element is the key
c1, c2 = o1[i], o2[i]
func, multiplier = sf_list[i][1:3]
n = func(c1, c2)
if n: return n*multiplier
# all functions returned 0 - identical sequences
return 0
##############################################################################
#
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
#
# Copyright (c) Digital Creations. All rights reserved.
#
# This license has been certified as Open Source(tm).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions in source code must retain the above copyright
# notice, this list of conditions, and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. Digital Creations requests that attribution be given to Zope
# in any manner possible. Zope includes a "Powered by Zope"
# button that is installed by default. While it is not a license
# violation to remove this button, it is requested that the
# attribution remain. A significant investment has been put
# into Zope, and this effort will continue if the Zope community
# continues to grow. This is one way to assure that growth.
#
# 4. All advertising materials and documentation mentioning
# features derived from or use of this software must display
# the following acknowledgement:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# In the event that the product being advertised includes an
# intact Zope distribution (with copyright and license included)
# then this clause is waived.
#
# 5. Names associated with Zope or Digital Creations must not be used to
# endorse or promote products derived from this software without
# prior written permission from Digital Creations.
#
# 6. Modified redistributions of any form whatsoever must retain
# the following acknowledgment:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# Intact (re-)distributions of any official Zope release do not
# require an external acknowledgement.
#
# 7. Modifications are encouraged but must be packaged separately as
# patches to official Zope releases. Distributions that do not
# clearly separate the patches from the original work must be clearly
# labeled as unofficial distributions. Modifications which do not
# carry the name Zope may be packaged in any form, as long as they
# conform to all of the clauses above.
#
#
# Disclaimer
#
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations. Specific
# attributions are listed in the accompanying credits file.
#
##############################################################################
from SortEx import *
res1=[{'weight': 1, 'word': 'AAA', 'key': 'aaa'}, {'weight': 0, 'word': 'BBB', 'key': 'bbb'}, {'weight': 0, 'word': 'CCC', 'key': 'ccc'}, {'weight': 0, 'word': 'DDD', 'key': 'ddd'}, {'weight': 1, 'word': 'EEE', 'key': 'eee'}, {'weight': 0, 'word': 'FFF', 'key': 'fff'}, {'weight': 0, 'word': 'GGG', 'key': 'ggg'}, {'weight': 0, 'word': 'HHH', 'key': 'hhh'}, {'weight': 1, 'word': 'III', 'key': 'iii'}, {'weight': -1, 'word': 'JJJ', 'key': 'jjj'}, {'weight': 0, 'word': 'KKK', 'key': 'kkk'}, {'weight': 0, 'word': 'LLL', 'key': 'lll'}, {'weight': 0, 'word': 'MMM', 'key': 'mmm'}, {'weight': 0, 'word': 'NNN', 'key': 'nnn'}, {'weight': 1, 'word': 'OOO', 'key': 'ooo'}, {'weight': 0, 'word': 'PPP', 'key': 'ppp'}, {'weight': -1, 'word': 'QQQ', 'key': 'qqq'}, {'weight': 0, 'word': 'RRR', 'key': 'rrr'}, {'weight': 0, 'word': 'SSS', 'key': 'sss'}, {'weight': 0, 'word': 'TTT', 'key': 'ttt'}, {'weight': 1, 'word': 'UUU', 'key': 'uuu'}, {'weight': 0, 'word': 'VVV', 'key': 'vvv'}, {'weight': 0, 'word': 'WWW', 'key': 'www'}, {'weight': 0, 'word': 'XXX', 'key': 'xxx'}, {'weight': -1, 'word': 'YYY', 'key': 'yyy'}, {'weight': 0, 'word': 'ZZZ', 'key': 'zzz'}]
res2=[{'weight': 1, 'word': 'AAA', 'key': 'aaa'}, {'weight': 0, 'word': 'BBB', 'key': 'bbb'}, {'weight': 0, 'word': 'CCC', 'key': 'ccc'}, {'weight': 0, 'word': 'DDD', 'key': 'ddd'}, {'weight': 1, 'word': 'EEE', 'key': 'eee'}, {'weight': 0, 'word': 'FFF', 'key': 'fff'}, {'weight': 0, 'word': 'GGG', 'key': 'ggg'}, {'weight': 0, 'word': 'HHH', 'key': 'hhh'}, {'weight': 1, 'word': 'III', 'key': 'iii'}, {'weight': -1, 'word': 'JJJ', 'key': 'jjj'}, {'weight': 0, 'word': 'KKK', 'key': 'kkk'}, {'weight': 0, 'word': 'LLL', 'key': 'lll'}, {'weight': 0, 'word': 'MMM', 'key': 'mmm'}, {'weight': 0, 'word': 'NNN', 'key': 'nnn'}, {'weight': 1, 'word': 'OOO', 'key': 'ooo'}, {'weight': 0, 'word': 'PPP', 'key': 'ppp'}, {'weight': -1, 'word': 'QQQ', 'key': 'qqq'}, {'weight': 0, 'word': 'RRR', 'key': 'rrr'}, {'weight': 0, 'word': 'SSS', 'key': 'sss'}, {'weight': 0, 'word': 'TTT', 'key': 'ttt'}, {'weight': 1, 'word': 'UUU', 'key': 'uuu'}, {'weight': 0, 'word': 'VVV', 'key': 'vvv'}, {'weight': 0, 'word': 'WWW', 'key': 'www'}, {'weight': 0, 'word': 'XXX', 'key': 'xxx'}, {'weight': -1, 'word': 'YYY', 'key': 'yyy'}, {'weight': 0, 'word': 'ZZZ', 'key': 'zzz'}]
res3=[{'weight': 1, 'word': 'AAA', 'key': 'aaa'}, {'weight': 0, 'word': 'BBB', 'key': 'bbb'}, {'weight': 0, 'word': 'CCC', 'key': 'ccc'}, {'weight': 0, 'word': 'DDD', 'key': 'ddd'}, {'weight': 1, 'word': 'EEE', 'key': 'eee'}, {'weight': 0, 'word': 'FFF', 'key': 'fff'}, {'weight': 0, 'word': 'GGG', 'key': 'ggg'}, {'weight': 0, 'word': 'HHH', 'key': 'hhh'}, {'weight': 1, 'word': 'III', 'key': 'iii'}, {'weight': -1, 'word': 'JJJ', 'key': 'jjj'}, {'weight': 0, 'word': 'KKK', 'key': 'kkk'}, {'weight': 0, 'word': 'LLL', 'key': 'lll'}, {'weight': 0, 'word': 'MMM', 'key': 'mmm'}, {'weight': 0, 'word': 'NNN', 'key': 'nnn'}, {'weight': 1, 'word': 'OOO', 'key': 'ooo'}, {'weight': 0, 'word': 'PPP', 'key': 'ppp'}, {'weight': -1, 'word': 'QQQ', 'key': 'qqq'}, {'weight': 0, 'word': 'RRR', 'key': 'rrr'}, {'weight': 0, 'word': 'SSS', 'key': 'sss'}, {'weight': 0, 'word': 'TTT', 'key': 'ttt'}, {'weight': 1, 'word': 'UUU', 'key': 'uuu'}, {'weight': 0, 'word': 'VVV', 'key': 'vvv'}, {'weight': 0, 'word': 'WWW', 'key': 'www'}, {'weight': 0, 'word': 'XXX', 'key': 'xxx'}, {'weight': -1, 'word': 'YYY', 'key': 'yyy'}, {'weight': 0, 'word': 'ZZZ', 'key': 'zzz'}]
res4=[{'weight': 0, 'word': 'ZZZ', 'key': 'zzz'}, {'weight': -1, 'word': 'YYY', 'key': 'yyy'}, {'weight': 0, 'word': 'XXX', 'key': 'xxx'}, {'weight': 0, 'word': 'WWW', 'key': 'www'}, {'weight': 0, 'word': 'VVV', 'key': 'vvv'}, {'weight': 1, 'word': 'UUU', 'key': 'uuu'}, {'weight': 0, 'word': 'TTT', 'key': 'ttt'}, {'weight': 0, 'word': 'SSS', 'key': 'sss'}, {'weight': 0, 'word': 'RRR', 'key': 'rrr'}, {'weight': -1, 'word': 'QQQ', 'key': 'qqq'}, {'weight': 0, 'word': 'PPP', 'key': 'ppp'}, {'weight': 1, 'word': 'OOO', 'key': 'ooo'}, {'weight': 0, 'word': 'NNN', 'key': 'nnn'}, {'weight': 0, 'word': 'MMM', 'key': 'mmm'}, {'weight': 0, 'word': 'LLL', 'key': 'lll'}, {'weight': 0, 'word': 'KKK', 'key': 'kkk'}, {'weight': -1, 'word': 'JJJ', 'key': 'jjj'}, {'weight': 1, 'word': 'III', 'key': 'iii'}, {'weight': 0, 'word': 'HHH', 'key': 'hhh'}, {'weight': 0, 'word': 'GGG', 'key': 'ggg'}, {'weight': 0, 'word': 'FFF', 'key': 'fff'}, {'weight': 1, 'word': 'EEE', 'key': 'eee'}, {'weight': 0, 'word': 'DDD', 'key': 'ddd'}, {'weight': 0, 'word': 'CCC', 'key': 'ccc'}, {'weight': 0, 'word': 'BBB', 'key': 'bbb'}, {'weight': 1, 'word': 'AAA', 'key': 'aaa'}]
res5=[{'weight': -1, 'word': 'JJJ', 'key': 'jjj'}, {'weight': -1, 'word': 'QQQ', 'key': 'qqq'}, {'weight': -1, 'word': 'YYY', 'key': 'yyy'}, {'weight': 0, 'word': 'BBB', 'key': 'bbb'}, {'weight': 0, 'word': 'CCC', 'key': 'ccc'}, {'weight': 0, 'word': 'DDD', 'key': 'ddd'}, {'weight': 0, 'word': 'FFF', 'key': 'fff'}, {'weight': 0, 'word': 'GGG', 'key': 'ggg'}, {'weight': 0, 'word': 'HHH', 'key': 'hhh'}, {'weight': 0, 'word': 'KKK', 'key': 'kkk'}, {'weight': 0, 'word': 'LLL', 'key': 'lll'}, {'weight': 0, 'word': 'MMM', 'key': 'mmm'}, {'weight': 0, 'word': 'NNN', 'key': 'nnn'}, {'weight': 0, 'word': 'PPP', 'key': 'ppp'}, {'weight': 0, 'word': 'RRR', 'key': 'rrr'}, {'weight': 0, 'word': 'SSS', 'key': 'sss'}, {'weight': 0, 'word': 'TTT', 'key': 'ttt'}, {'weight': 0, 'word': 'VVV', 'key': 'vvv'}, {'weight': 0, 'word': 'WWW', 'key': 'www'}, {'weight': 0, 'word': 'XXX', 'key': 'xxx'}, {'weight': 0, 'word': 'ZZZ', 'key': 'zzz'}, {'weight': 1, 'word': 'AAA', 'key': 'aaa'}, {'weight': 1, 'word': 'EEE', 'key': 'eee'}, {'weight': 1, 'word': 'III', 'key': 'iii'}, {'weight': 1, 'word': 'OOO', 'key': 'ooo'}, {'weight': 1, 'word': 'UUU', 'key': 'uuu'}]
res6=[{'weight': -1, 'word': 'YYY', 'key': 'yyy'}, {'weight': -1, 'word': 'QQQ', 'key': 'qqq'}, {'weight': -1, 'word': 'JJJ', 'key': 'jjj'}, {'weight': 0, 'word': 'ZZZ', 'key': 'zzz'}, {'weight': 0, 'word': 'XXX', 'key': 'xxx'}, {'weight': 0, 'word': 'WWW', 'key': 'www'}, {'weight': 0, 'word': 'VVV', 'key': 'vvv'}, {'weight': 0, 'word': 'TTT', 'key': 'ttt'}, {'weight': 0, 'word': 'SSS', 'key': 'sss'}, {'weight': 0, 'word': 'RRR', 'key': 'rrr'}, {'weight': 0, 'word': 'PPP', 'key': 'ppp'}, {'weight': 0, 'word': 'NNN', 'key': 'nnn'}, {'weight': 0, 'word': 'MMM', 'key': 'mmm'}, {'weight': 0, 'word': 'LLL', 'key': 'lll'}, {'weight': 0, 'word': 'KKK', 'key': 'kkk'}, {'weight': 0, 'word': 'HHH', 'key': 'hhh'}, {'weight': 0, 'word': 'GGG', 'key': 'ggg'}, {'weight': 0, 'word': 'FFF', 'key': 'fff'}, {'weight': 0, 'word': 'DDD', 'key': 'ddd'}, {'weight': 0, 'word': 'CCC', 'key': 'ccc'}, {'weight': 0, 'word': 'BBB', 'key': 'bbb'}, {'weight': 1, 'word': 'UUU', 'key': 'uuu'}, {'weight': 1, 'word': 'OOO', 'key': 'ooo'}, {'weight': 1, 'word': 'III', 'key': 'iii'}, {'weight': 1, 'word': 'EEE', 'key': 'eee'}, {'weight': 1, 'word': 'AAA', 'key': 'aaa'}]
res7=[{'weight': -1, 'word': 'JJJ', 'key': 'jjj'}, {'weight': -1, 'word': 'QQQ', 'key': 'qqq'}, {'weight': -1, 'word': 'YYY', 'key': 'yyy'}, {'weight': 0, 'word': 'BBB', 'key': 'bbb'}, {'weight': 0, 'word': 'CCC', 'key': 'ccc'}, {'weight': 0, 'word': 'DDD', 'key': 'ddd'}, {'weight': 0, 'word': 'FFF', 'key': 'fff'}, {'weight': 0, 'word': 'GGG', 'key': 'ggg'}, {'weight': 0, 'word': 'HHH', 'key': 'hhh'}, {'weight': 0, 'word': 'KKK', 'key': 'kkk'}, {'weight': 0, 'word': 'LLL', 'key': 'lll'}, {'weight': 0, 'word': 'MMM', 'key': 'mmm'}, {'weight': 0, 'word': 'NNN', 'key': 'nnn'}, {'weight': 0, 'word': 'PPP', 'key': 'ppp'}, {'weight': 0, 'word': 'RRR', 'key': 'rrr'}, {'weight': 0, 'word': 'SSS', 'key': 'sss'}, {'weight': 0, 'word': 'TTT', 'key': 'ttt'}, {'weight': 0, 'word': 'VVV', 'key': 'vvv'}, {'weight': 0, 'word': 'WWW', 'key': 'www'}, {'weight': 0, 'word': 'XXX', 'key': 'xxx'}, {'weight': 0, 'word': 'ZZZ', 'key': 'zzz'}, {'weight': 1, 'word': 'AAA', 'key': 'aaa'}, {'weight': 1, 'word': 'EEE', 'key': 'eee'}, {'weight': 1, 'word': 'III', 'key': 'iii'}, {'weight': 1, 'word': 'OOO', 'key': 'ooo'}, {'weight': 1, 'word': 'UUU', 'key': 'uuu'}]
##############################################################################
#
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
#
# Copyright (c) Digital Creations. All rights reserved.
#
# This license has been certified as Open Source(tm).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions in source code must retain the above copyright
# notice, this list of conditions, and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. Digital Creations requests that attribution be given to Zope
# in any manner possible. Zope includes a "Powered by Zope"
# button that is installed by default. While it is not a license
# violation to remove this button, it is requested that the
# attribution remain. A significant investment has been put
# into Zope, and this effort will continue if the Zope community
# continues to grow. This is one way to assure that growth.
#
# 4. All advertising materials and documentation mentioning
# features derived from or use of this software must display
# the following acknowledgement:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# In the event that the product being advertised includes an
# intact Zope distribution (with copyright and license included)
# then this clause is waived.
#
# 5. Names associated with Zope or Digital Creations must not be used to
# endorse or promote products derived from this software without
# prior written permission from Digital Creations.
#
# 6. Modified redistributions of any form whatsoever must retain
# the following acknowledgment:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# Intact (re-)distributions of any official Zope release do not
# require an external acknowledgement.
#
# 7. Modifications are encouraged but must be packaged separately as
# patches to official Zope releases. Distributions that do not
# clearly separate the patches from the original work must be clearly
# labeled as unofficial distributions. Modifications which do not
# carry the name Zope may be packaged in any form, as long as they
# conform to all of the clauses above.
#
#
# Disclaimer
#
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations. Specific
# attributions are listed in the accompanying credits file.
#
##############################################################################
import sys
sys.path.insert(0, '.')
try:
import Testing
except ImportError:
sys.path[0] = '../../'
import Testing
import unittest
from SortEx import *
from ztestlib import *
from results import *
class TestCase( unittest.TestCase ):
"""
Test SortEx .
"""
def setUp( self ):
"""
"""
def tearDown( self ):
"""
"""
def test1( self ):
"test1"
assert res1==SortEx(wordlist)
def test2( self ):
"test2"
assert res2==SortEx(wordlist, (("key",),), mapping=1)
def test3( self ):
"test3"
assert res3==SortEx(wordlist, (("key", "cmp"),), mapping=1)
def test4( self ):
"test4"
assert res4==SortEx(wordlist, (("key", "cmp", "desc"),), mapping=1)
def test5( self ):
"test5"
assert res5==SortEx(wordlist, (("weight",), ("key",)), mapping=1)
def test6( self ):
"test6"
assert res6==SortEx(wordlist, (("weight",), ("key", "nocase", "desc")), mapping=1)
def test7(self):
"test7"
def myCmp(s1, s2):
return -cmp(s1, s2)
# Create namespace...
from DocumentTemplate.DT_Util import TemplateDict
md = TemplateDict()
#... and push out function onto the namespace
md._push({"myCmp" : myCmp})
assert res7==SortEx(wordlist, (("weight",), ("key", "myCmp", "desc")), md, mapping=1)
def test_suite():
return unittest.makeSuite( TestCase )
def debug():
return test_suite().debug()
def pdebug():
import pdb
pdb.run('debug()')
def main():
unittest.TextTestRunner().run( test_suite() )
if __name__ == '__main__':
if len(sys.argv) > 1:
globals()[sys.argv[1]]()
else:
main()
class standard_html: # Base class for using with ZTemplates
def __init__(self, title):
self.standard_html_header = """<HTML>
<HEAD>
<TITLE>
%s
</TITLE>
</HEAD>
<BODY>""" % title
self.standard_html_footer = """</BODY>
</HTML>"""
self.title = title
def test(s):
outfile = open("test.out", 'w')
outfile.write(s)
outfile.close()
def exception():
import sys, traceback
exc_type, exc_value, exc_tb = sys.exc_info()
outfile = open("test.err", 'w')
traceback.print_exception(exc_type, exc_value, exc_tb, None, outfile)
outfile.close()
wordlist = [
{"key": "aaa", "word": "AAA", "weight": 1},
{"key": "bbb", "word": "BBB", "weight": 0},
{"key": "ccc", "word": "CCC", "weight": 0},
{"key": "ddd", "word": "DDD", "weight": 0},
{"key": "eee", "word": "EEE", "weight": 1},
{"key": "fff", "word": "FFF", "weight": 0},
{"key": "ggg", "word": "GGG", "weight": 0},
{"key": "hhh", "word": "HHH", "weight": 0},
{"key": "iii", "word": "III", "weight": 1},
{"key": "jjj", "word": "JJJ", "weight": -1},
{"key": "kkk", "word": "KKK", "weight": 0},
{"key": "lll", "word": "LLL", "weight": 0},
{"key": "mmm", "word": "MMM", "weight": 0},
{"key": "nnn", "word": "NNN", "weight": 0},
{"key": "ooo", "word": "OOO", "weight": 1},
{"key": "ppp", "word": "PPP", "weight": 0},
{"key": "qqq", "word": "QQQ", "weight": -1},
{"key": "rrr", "word": "RRR", "weight": 0},
{"key": "sss", "word": "SSS", "weight": 0},
{"key": "ttt", "word": "TTT", "weight": 0},
{"key": "uuu", "word": "UUU", "weight": 1},
{"key": "vvv", "word": "VVV", "weight": 0},
{"key": "www", "word": "WWW", "weight": 0},
{"key": "xxx", "word": "XXX", "weight": 0},
{"key": "yyy", "word": "YYY", "weight": -1},
{"key": "zzz", "word": "ZZZ", "weight": 0}
]
......@@ -213,6 +213,8 @@ functions: DTML Functions
"'math' module":dtml-math.stx
"'sequence' module":dtml-sequence.stx
"Built-in Python Functions":http://www.python.org/doc/current/lib/built-in-funcs.html
......
sequence: DTML Sequence Functions
The 'sequence' module provides sequence sorting function.
Functions
sort(seq,(sort)) -- Sort the sequence *seq* of objects by the optional
sort schema *sort*. *sort* is a sequence of tuples (key,func,direction)
that describe the sort order.
- *key* -- attribute of the object to be sorted.
- *func* -- defines the compare function (optional). Allowed values:
- "cmp" -- the standard comparison function
- "nocase" -- case-insensitive comparison
- "strcoll" or "locale" -- locale-aware string comparison
- "strcoll_nocase" or "locale_nocase" -- locale-aware
case-insensitive string comparison
- "xxx" -- a user-defined comparison function
- *direction* -- defines the sort direction for the key (optional).
(allowed values: "asc", "desc")
......@@ -83,9 +83,10 @@
#
##############################################################################
__version__='$Revision: 1.2 $'[11:-2]
__version__='$Revision: 1.3 $'[11:-2]
import string, math, random, whrandom
import DocumentTemplate.sequence
utility_builtins = {}
......@@ -93,6 +94,7 @@ utility_builtins['string'] = string
utility_builtins['math'] = math
utility_builtins['random'] = random
utility_builtins['whrandom'] = whrandom
utility_builtins['sequence'] = DocumentTemplate.sequence
try:
import DateTime
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment