Mercurial > p > roundup > code
annotate roundup/cgi/ZTUtils/Iterator.py @ 7752:b2dbab2b34bc
fix(refactor): multiple fixups using ruff linter; more testing.
Converting to using the ruff linter and its rulesets. Fixed a number
of issues.
admin.py:
sort imports
use immutable tuples as default value markers for parameters where a
None value is valid.
reduced some loops to list comprehensions for performance
used ternary to simplify some if statements
named some variables to make them less magic
(e.g. _default_savepoint_setting = 1000)
fixed some tests for argument counts < 2 becomes != 2 so 3 is an
error.
moved exception handlers outside of loops for performance where
exception handler will abort loop anyway.
renamed variables called 'id' or 'dir' as they shadow builtin
commands.
fix translations of form _("string %s" % value) -> _("string %s") %
value so translation will be looked up with the key before
substitution.
end dicts, tuples with a trailing comma to reduce missing comma
errors if modified
simplified sorted(list(self.setting.keys())) to
sorted(self.setting.keys()) as sorted consumes whole list.
in if conditions put compared variable on left and threshold condition
on right. (no yoda conditions)
multiple noqa: suppression
removed unneeded noqa as lint rulesets are a bit different
do_get - refactor output printing logic: Use fast return if not
special formatting is requested; use isinstance with a tuple
rather than two isinstance calls; cleaned up flow and removed
comments on algorithm as it can be easily read from the code.
do_filter, do_find - refactor output printing logic. Reduce
duplicate code.
do_find - renamed variable 'value' that was set inside a loop. The
loop index variable was also named 'value'.
do_pragma - added hint to use list subcommand if setting was not
found. Replaced condition 'type(x) is bool' with 'isinstance(x,
bool)' for various types.
test_admin.py
added testing for do_list
better test coverage for do_get includes: -S and -d for multilinks,
error case for -d with non-link.
better testing for do_find including all output modes
better testing for do_filter including all output modes
fixed expected output for do_pragma that now includes hint to use
pragma list if setting not found.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Fri, 01 Mar 2024 14:53:18 -0500 |
| parents | ef6631409171 |
| children | 9bbc1d951677 |
| rev | line source |
|---|---|
| 1049 | 1 ############################################################################## |
| 2 # | |
| 3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. | |
| 4 # | |
| 5 # This software is subject to the provisions of the Zope Public License, | |
| 6 # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. | |
| 7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | |
| 8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | |
| 10 # FOR A PARTICULAR PURPOSE | |
| 11 # | |
| 12 ############################################################################## | |
| 13 __doc__='''Iterator class | |
| 14 | |
| 15 Unlike the builtin iterators of Python 2.2+, these classes are | |
| 16 designed to maintain information about the state of an iteration. | |
| 17 The Iterator() function accepts either a sequence or a Python | |
| 18 iterator. The next() method fetches the next item, and returns | |
| 19 true if it succeeds. | |
| 20 | |
|
4570
6e3e4f24c753
Remove keyword expansions from CVS. All regression tests passed afterwards.
Eric S. Raymond <esr@thyrsus.com>
parents:
3200
diff
changeset
|
21 ''' |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1233
diff
changeset
|
22 __docformat__ = 'restructuredtext' |
| 1049 | 23 |
| 24 import string | |
| 25 | |
| 26 class Iterator: | |
| 27 '''Simple Iterator class''' | |
| 28 | |
| 29 __allow_access_to_unprotected_subobjects__ = 1 | |
| 30 | |
| 31 nextIndex = 0 | |
| 32 def __init__(self, seq): | |
|
3200
d2b1a946fdf4
change ZTUtils Iterator to always iter() its sequence argument
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
33 self.seq = iter(seq) # force seq to be an iterator |
|
d2b1a946fdf4
change ZTUtils Iterator to always iter() its sequence argument
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
34 self._inner = iterInner |
|
d2b1a946fdf4
change ZTUtils Iterator to always iter() its sequence argument
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
35 self._prep_next = iterInner.prep_next |
| 1049 | 36 |
| 37 def __getattr__(self, name): | |
| 38 try: | |
| 39 inner = getattr(self._inner, 'it_' + name) | |
| 40 except AttributeError: | |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
4570
diff
changeset
|
41 raise AttributeError(name) |
| 1049 | 42 return inner(self) |
| 43 | |
| 44 def next(self): | |
| 45 if not (hasattr(self, '_next') or self._prep_next(self)): | |
| 46 return 0 | |
| 47 self.index = i = self.nextIndex | |
| 48 self.nextIndex = i+1 | |
| 49 self._advance(self) | |
| 50 return 1 | |
| 51 | |
| 52 def _advance(self, it): | |
| 53 self.item = self._next | |
| 54 del self._next | |
| 55 del self.end | |
| 56 self._advance = self._inner.advance | |
| 57 self.start = 1 | |
| 58 | |
| 59 def number(self): return self.nextIndex | |
| 60 | |
| 61 def even(self): return not self.index % 2 | |
| 62 | |
| 63 def odd(self): return self.index % 2 | |
| 64 | |
| 65 def letter(self, base=ord('a'), radix=26): | |
| 66 index = self.index | |
| 67 s = '' | |
| 68 while 1: | |
| 69 index, off = divmod(index, radix) | |
| 70 s = chr(base + off) + s | |
| 71 if not index: return s | |
| 72 | |
| 73 def Letter(self): | |
| 74 return self.letter(base=ord('A')) | |
| 75 | |
| 76 def Roman(self, rnvalues=( | |
| 77 (1000,'M'),(900,'CM'),(500,'D'),(400,'CD'), | |
| 78 (100,'C'),(90,'XC'),(50,'L'),(40,'XL'), | |
| 79 (10,'X'),(9,'IX'),(5,'V'),(4,'IV'),(1,'I')) ): | |
| 80 n = self.index + 1 | |
| 81 s = '' | |
| 82 for v, r in rnvalues: | |
| 83 rct, n = divmod(n, v) | |
| 84 s = s + r * rct | |
| 85 return s | |
| 86 | |
|
5419
ef6631409171
Python 3 preparation: avoid string.lower.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5396
diff
changeset
|
87 def roman(self, lower=lambda x:x.lower): |
| 1049 | 88 return lower(self.Roman()) |
| 89 | |
| 90 def first(self, name=None): | |
| 91 if self.start: return 1 | |
| 92 return not self.same_part(name, self._last, self.item) | |
| 93 | |
| 94 def last(self, name=None): | |
| 95 if self.end: return 1 | |
| 96 return not self.same_part(name, self.item, self._next) | |
| 97 | |
| 98 def same_part(self, name, ob1, ob2): | |
| 99 if name is None: | |
| 100 return ob1 == ob2 | |
| 101 no = [] | |
| 102 return getattr(ob1, name, no) == getattr(ob2, name, no) is not no | |
| 103 | |
| 104 def __iter__(self): | |
| 105 return IterIter(self) | |
| 106 | |
| 107 class InnerBase: | |
| 108 '''Base Inner class for Iterators''' | |
| 109 # Prep sets up ._next and .end | |
| 110 def prep_next(self, it): | |
| 111 it.next = self.no_next | |
| 112 it.end = 1 | |
| 113 return 0 | |
| 114 | |
| 115 # Advance knocks them down | |
| 116 def advance(self, it): | |
| 117 it._last = it.item | |
| 118 it.item = it._next | |
| 119 del it._next | |
| 120 del it.end | |
| 121 it.start = 0 | |
| 122 | |
| 123 def no_next(self, it): | |
| 124 return 0 | |
| 125 | |
| 126 def it_end(self, it): | |
| 127 if hasattr(it, '_next'): | |
| 128 return 0 | |
| 129 return not self.prep_next(it) | |
| 130 | |
| 131 class SeqInner(InnerBase): | |
| 132 '''Inner class for sequence Iterators''' | |
| 133 | |
| 134 def _supports(self, ob): | |
| 135 try: ob[0] | |
|
1233
69bf0d381fd7
Zope Collector fixes.
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
136 except (TypeError, AttributeError): return 0 |
| 1049 | 137 except: pass |
| 138 return 1 | |
| 139 | |
| 140 def prep_next(self, it): | |
| 141 i = it.nextIndex | |
| 142 try: | |
| 143 it._next = it.seq[i] | |
| 144 except IndexError: | |
| 145 it._prep_next = self.no_next | |
| 146 it.end = 1 | |
| 147 return 0 | |
| 148 it.end = 0 | |
| 149 return 1 | |
| 150 | |
| 151 def it_length(self, it): | |
| 152 it.length = l = len(it.seq) | |
| 153 return l | |
| 154 | |
| 155 try: | |
| 156 StopIteration=StopIteration | |
| 157 except NameError: | |
| 158 StopIteration="StopIteration" | |
| 159 | |
| 160 class IterInner(InnerBase): | |
| 161 '''Iterator inner class for Python iterators''' | |
| 162 | |
| 163 def _supports(self, ob): | |
| 164 try: | |
| 165 if hasattr(ob, 'next') and (ob is iter(ob)): | |
| 166 return 1 | |
| 167 except: | |
| 168 return 0 | |
| 169 | |
| 170 def prep_next(self, it): | |
| 171 try: | |
|
5396
831787cf6694
Python 3 preparation: update next() usage for iterators.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5378
diff
changeset
|
172 it._next = next(it.seq) |
| 1049 | 173 except StopIteration: |
| 174 it._prep_next = self.no_next | |
| 175 it.end = 1 | |
| 176 return 0 | |
| 177 it.end = 0 | |
| 178 return 1 | |
| 179 | |
| 180 class IterIter: | |
| 181 def __init__(self, it): | |
| 182 self.it = it | |
| 183 self.skip = it.nextIndex > 0 and not it.end | |
|
5396
831787cf6694
Python 3 preparation: update next() usage for iterators.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5378
diff
changeset
|
184 def __next__(self): |
| 1049 | 185 it = self.it |
| 186 if self.skip: | |
| 187 self.skip = 0 | |
| 188 return it.item | |
| 189 if it.next(): | |
| 190 return it.item | |
| 191 raise StopIteration | |
|
5396
831787cf6694
Python 3 preparation: update next() usage for iterators.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5378
diff
changeset
|
192 # Python 2 compatibility: |
|
831787cf6694
Python 3 preparation: update next() usage for iterators.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5378
diff
changeset
|
193 next = __next__ |
| 1049 | 194 |
| 195 seqInner = SeqInner() | |
| 196 iterInner = IterInner() |
