Package teamwork :: Package widgets :: Package htmlViewer :: Module mfxtools
[hide private]
[frames] | no frames]

Source Code for Module teamwork.widgets.htmlViewer.mfxtools

  1  ## vim:ts=4:et:nowrap 
  2  ## 
  3  ##---------------------------------------------------------------------------## 
  4  ## 
  5  ## PySol -- a Python Solitaire game 
  6  ## 
  7  ## Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer 
  8  ## Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer 
  9  ## Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer 
 10  ## Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer 
 11  ## Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer 
 12  ## Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer 
 13  ## All Rights Reserved. 
 14  ## 
 15  ## This program is free software; you can redistribute it and/or modify 
 16  ## it under the terms of the GNU General Public License as published by 
 17  ## the Free Software Foundation; either version 2 of the License, or 
 18  ## (at your option) any later version. 
 19  ## 
 20  ## This program is distributed in the hope that it will be useful, 
 21  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 22  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 23  ## GNU General Public License for more details. 
 24  ## 
 25  ## You should have received a copy of the GNU General Public License 
 26  ## along with this program; see the file COPYING. 
 27  ## If not, write to the Free Software Foundation, Inc., 
 28  ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 29  ## 
 30  ## Markus F.X.J. Oberhumer 
 31  ## <markus@oberhumer.com> 
 32  ## http://www.oberhumer.com/pysol 
 33  ## 
 34  ##---------------------------------------------------------------------------## 
 35   
 36   
 37  # imports 
 38  import sys, types, operator 
 39   
 40   
 41  # /*********************************************************************** 
 42  # // unicode 
 43  # ************************************************************************/ 
 44   
 45  try: 
 46      types.StringTypes 
 47  except: 
 48      try: 
 49          types.StringTypes = (types.StringType, types.UnicodeType) 
 50      except: 
 51          types.StringTypes = (types.StringType,) 
 52   
 53  try: 
 54      types.UnicodeType 
 55  except: 
 56      types.UnicodeType = None 
 57   
 58   
59 -def ustr(s):
60 if type(s) is types.UnicodeType: 61 try: 62 x = str(s) 63 except: 64 x = "" 65 for c in x: 66 if ord(c) >= 256: 67 c = "" 68 x = x + c 69 return s 70 else: 71 x = str(s) 72 return x
73 74 75 # /*********************************************************************** 76 # // emulation of some of Marc-Andre Lemburg's mxTools 77 # ************************************************************************/ 78
79 -def indices(object):
80 return tuple(range(len(object)))
81
82 -def trange(start, stop=None, step=None):
83 if stop is None: 84 return tuple(range(start)) 85 elif step is None: 86 return tuple(range(start, stop)) 87 else: 88 return tuple(range(start, stop, step))
89
90 -def range_len(object):
91 return range(len(object))
92
93 -def reverse(sequence):
94 if type(sequence) is types.TupleType: 95 l = list(sequence) 96 l.reverse() 97 l = tuple(l) 98 elif type(sequence) is types.ListType: 99 l = sequence[:] 100 l.reverse() 101 else: 102 l = list(sequence) 103 l.reverse() 104 return l
105
106 -def irange(object, indices=None):
107 if indices is None: 108 return tuple(map(None, range(len(object)), object)) 109 else: 110 # this is slow... 111 l = [] 112 for i in indices: 113 l.append((i, object[i])) 114 return tuple(l)
115
116 -def count(condition, sequence):
117 if condition is None: 118 return len(filter(None, sequence)) 119 else: 120 ##return len(filter(condition, sequence)) 121 # why is this faster ??? 122 return len(filter(None, map(condition, sequence)))
123
124 -def exists(condition, sequence):
125 if condition is None: 126 condition = operator.truth 127 for obj in sequence: 128 if condition(obj): 129 return 1 130 return 0
131
132 -def forall(condition, sequence):
133 if condition is None: 134 condition = operator.truth 135 for obj in sequence: 136 if not condition(obj): 137 return 0 138 return 1
139 140 141 # /*********************************************************************** 142 # // 143 # ************************************************************************/ 144 145 # def bool(expr): if expr: return 1 else: return 0 146 bool = operator.truth 147
148 -def sgn(expr):
149 if expr < 0: return -1 150 if expr > 0: return 1 151 return 0
152 153 154 # /*********************************************************************** 155 # // 156 # ************************************************************************/ 157 158
159 -def mfxtools_main(args=[]):
160 print types.StringTypes 161 # 162 m = None 163 try: 164 import NewBuiltins.mxTools 165 m = NewBuiltins.mxTools 166 except: 167 print "mxTools not found !" 168 return 0 169 # 170 b = sys.modules[__name__] 171 t1 = (0, 1, 2, 3, 4) 172 t2 = () 173 l1 = [0, 1, 2, 3, 4] 174 l2 = [] 175 cond1 = lambda x: x == 1 176 cond2 = lambda x: x < 3 177 # 178 for x in (t1, t2, l1, l2): 179 assert b.indices(x) == m.indices(x) 180 assert b.range_len(x) == m.range_len(x) 181 assert b.reverse(x) == m.reverse(x) 182 assert b.irange(x) == m.irange(x) 183 for cond in (cond1, cond2): 184 assert b.count(cond, x) == m.count(cond, x) 185 assert b.exists(cond, x) == m.exists(cond, x) 186 assert b.forall(cond, x) == m.forall(cond, x) 187 # 188 assert b.irange(t1, t1) == m.irange(t1, t1) 189 assert b.irange(t1, reverse(t1)) == m.irange(t1, reverse(t1)) 190 assert b.irange(t1, t2) == m.irange(t1, t2) 191 # 192 assert b.trange(10) == m.trange(10) 193 assert b.trange(0, 10) == m.trange(0, 10) 194 assert b.trange(0, 10, 2) == m.trange(0, 10, 2) 195 # 196 print "All tests passed." 197 return 0
198 199 if __name__ == "__main__": 200 sys.exit(mfxtools_main(sys.argv)) 201