Skip to content

Commit 3f25771

Browse files
committed
Fix flake8 warnings in regular code
See issue #27.
1 parent 967a3bf commit 3f25771

File tree

6 files changed

+98
-95
lines changed

6 files changed

+98
-95
lines changed

unpythonic/amb.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def forall(*lines):
113113
# Notation used by the monad implementation for the bind and sequence
114114
# operators, with any relevant whitespace.
115115
bind = " >> "
116-
seq = ".then"
116+
seq = ".then"
117117

118118
class env:
119119
def __init__(self):
@@ -142,7 +142,7 @@ def begin(*exprs): # args eagerly evaluated by Python
142142
begin_is_open = False
143143
for j, item in enumerate(lines):
144144
is_first = (j == 0)
145-
is_last = (j == len(lines) - 1)
145+
is_last = (j == len(lines) - 1)
146146

147147
if isinstance(item, Assignment):
148148
name, body = item
@@ -251,7 +251,7 @@ def __len__(self):
251251
def __getitem__(self, i):
252252
return self.x[i]
253253

254-
def __add__(self, other): # concatenation of Lists, for convenience
254+
def __add__(self, other): # concatenation of Lists, for convenience
255255
cls = self.__class__
256256
return cls.from_iterable(self.x + other.x)
257257

@@ -263,7 +263,7 @@ def __str__(self):
263263
def from_iterable(cls, iterable): # convenience
264264
try:
265265
return cls(*iterable)
266-
except TypeError: # maybe a generator; try forcing it before giving up.
266+
except TypeError: # maybe a generator; try forcing it before giving up.
267267
return cls(*tuple(iterable))
268268

269269
def copy(self):

unpythonic/collections.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ def __eq__(self, other):
555555
return True
556556
if len(self) != len(other):
557557
return False
558-
for v, w in zip(self, other):
559-
if v != w:
558+
for v, wrap in zip(self, other):
559+
if v != wrap:
560560
return False
561561
return True
562562

@@ -647,8 +647,8 @@ def __getitem__(self, k):
647647
raise TypeError("multidimensional subscripting not supported; got '{}'".format(k))
648648
else:
649649
data, r = self._update_cache()
650-
l = len(r)
651-
if k >= l or k < -l:
650+
n = len(r)
651+
if k >= n or k < -n:
652652
raise IndexError("view index out of range")
653653
return data[r[k]]
654654

@@ -712,8 +712,8 @@ def __setitem__(self, k, v):
712712
elif isinstance(k, tuple):
713713
raise TypeError("multidimensional subscripting not supported; got '{}'".format(k))
714714
else:
715-
l = len(r)
716-
if k >= l or k < -l:
715+
n = len(r)
716+
if k >= n or k < -n:
717717
raise IndexError("view assigment index out of range")
718718
data[r[k]] = v
719719
def reverse(self):
@@ -749,10 +749,10 @@ def __init__(self, seq, ix=None, v=None):
749749
def __iter__(self):
750750
if self.ix is None: # allow no-op ShadowedSequences since the repr suggests one could do that
751751
return iter(self.seq)
752-
l = len(self)
752+
n = len(self)
753753
getone = self._getone
754754
def ShadowedSequenceIterator():
755-
for j in range(l):
755+
for j in range(n):
756756
yield getone(j)
757757
return ShadowedSequenceIterator()
758758

@@ -762,31 +762,32 @@ def __len__(self):
762762
def __getitem__(self, k):
763763
if self.ix is None: # allow no-op ShadowedSequences since the repr suggests one could do that
764764
return self.seq[k]
765-
l = len(self)
765+
n = len(self)
766766
if isinstance(k, slice):
767767
cls = type(self.seq)
768768
ctor = tuple if hasattr(cls, "_make") else cls # slice of namedtuple -> tuple
769-
return ctor(self._getone(j) for j in range(l)[k])
769+
return ctor(self._getone(j) for j in range(n)[k])
770770
elif isinstance(k, tuple):
771771
raise TypeError("multidimensional subscripting not supported; got '{}'".format(k))
772772
else:
773-
if k >= l or k < -l:
773+
if k >= n or k < -n:
774774
raise IndexError("ShadowedSequence index out of range")
775775
return self._getone(k)
776776

777777
def _getone(self, k):
778778
ix = self.ix
779-
l = len(self)
780-
if in_slice(k, ix, l):
779+
n = len(self)
780+
if in_slice(k, ix, n):
781781
if isinstance(ix, int):
782782
return self.v # just one item
783783
# we already know k is in ix, so skip validation for speed.
784-
i = _index_in_slice(k, ix, l, _validate=False)
784+
i = _index_in_slice(k, ix, n, _validate=False)
785785
if i >= len(self.v):
786786
raise IndexError("Replacement sequence too short; attempted to access index {} with len {} (items: {})".format(i, len(self.v), self.v))
787787
return self.v[i]
788788
return self.seq[k] # not in slice
789789

790+
# TODO: fix flake8 E741 ambiguous variable name "l". Here it's part of the public API, so we'll have to wait until 15.0 to change the name.
790791
def in_slice(i, s, l=None):
791792
"""Return whether the int i is in the slice s.
792793
@@ -816,6 +817,7 @@ def in_slice(i, s, l=None):
816817
on_grid = (i - start) % step == 0
817818
return at_or_after_start and on_grid and before_stop
818819

820+
# TODO: fix flake8 E741 ambiguous variable name "l". Here it's part of the public API, so we'll have to wait until 15.0 to change the name.
819821
def index_in_slice(i, s, l=None):
820822
"""Return the index of the int i in the slice s, or None if i is not in s.
821823
@@ -827,60 +829,60 @@ def index_in_slice(i, s, l=None):
827829

828830
# efficiency: allow skipping the validation check for call sites
829831
# that have already checked with in_slice().
830-
def _index_in_slice(i, s, l=None, _validate=True):
831-
if (not _validate) or in_slice(i, s, l):
832-
wrap = _make_negidx_converter(l)
833-
start, _, step = _canonize_slice(s, l, wrap)
832+
def _index_in_slice(i, s, n=None, _validate=True): # n: length of sequence being indexed
833+
if (not _validate) or in_slice(i, s, n):
834+
wrap = _make_negidx_converter(n)
835+
start, _, step = _canonize_slice(s, n, wrap)
834836
return (wrap(i) - start) // step
835837

836-
def _make_negidx_converter(l): # l: length of sequence being indexed
837-
if l is not None:
838-
if not isinstance(l, int):
839-
raise TypeError("l must be int, got {} with value {}".format(type(l), l))
840-
if l <= 0:
841-
raise ValueError("l must be an int >= 1, got {}".format(l))
838+
def _make_negidx_converter(n): # n: length of sequence being indexed
839+
if n is not None:
840+
if not isinstance(n, int):
841+
raise TypeError("n must be int, got {} with value {}".format(type(n), n))
842+
if n <= 0:
843+
raise ValueError("n must be an int >= 1, got {}".format(n))
842844
def apply_conversion(k):
843-
return k % l
845+
return k % n
844846
else:
845847
def apply_conversion(k):
846-
raise ValueError("Need l to interpret negative indices")
848+
raise ValueError("Need n to interpret negative indices")
847849
def convert(k):
848850
if k is not None:
849851
if not isinstance(k, int):
850852
raise TypeError("k must be int, got {} with value {}".format(type(k), k))
851-
# Almost standard semantics for negative indices. Usually -l < k < l,
853+
# Almost standard semantics for negative indices. Usually -n < k < n,
852854
# but here we must allow for conversion of the end position, for
853855
# which the last valid value is one past the end.
854-
if not -l <= k <= l:
855-
raise IndexError("Should have -n <= k <= n, but n = len(args) = {}, and k = {}".format(l, k))
856+
if not -n <= k <= n:
857+
raise IndexError("Should have -n <= k <= n, but n = len(args) = {}, and k = {}".format(n, k))
856858
return apply_conversion(k) if k < 0 else k
857859
return convert
858860

859-
def _canonize_slice(s, l=None, w=None): # convert negatives, inject defaults.
861+
def _canonize_slice(s, n=None, wrap=None): # convert negatives, inject defaults.
860862
if not isinstance(s, slice):
861863
raise TypeError("s must be slice, got {} with value {}".format(type(s), s))
862864

863865
step = s.step if s.step is not None else +1 # no "s.step or +1"; someone may try step=0
864866
if step == 0:
865867
raise ValueError("slice step cannot be zero") # message copied from range(5)[0:4:0]
866868

867-
wrap = w or _make_negidx_converter(l)
869+
wrap = wrap or _make_negidx_converter(n)
868870

869871
start = wrap(s.start)
870872
if start is None:
871873
if step > 0:
872874
start = 0
873875
else:
874-
if l is None:
875-
raise ValueError("Need l to determine default start for step < 0")
876+
if n is None:
877+
raise ValueError("Need n to determine default start for step < 0")
876878
start = wrap(-1)
877879

878880
stop = wrap(s.stop)
879881
if stop is None:
880882
if step > 0:
881-
if l is None:
882-
raise ValueError("Need l to determine default stop for step > 0")
883-
stop = l
883+
if n is None:
884+
raise ValueError("Need n to determine default stop for step > 0")
885+
stop = n
884886
else:
885887
stop = -1 # yes, really -1 to have index 0 inside the slice
886888

unpythonic/env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def __exit__(self, exctype, excvalue, traceback):
167167

168168
# pretty-printing
169169
def __repr__(self):
170-
bindings = ["{:s}={}".format(name,repr(value)) for name,value in self._env.items()]
170+
bindings = ["{:s}={}".format(name, repr(value)) for name, value in self._env.items()]
171171
return "<env object at 0x{:x}: {{{:s}}}>".format(id(self), ", ".join(bindings))
172172

173173
# other

unpythonic/fold.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -320,35 +320,36 @@ def fibo(a, b):
320320
value, *states = result
321321
yield value
322322

323-
## This is **not** how to make a right map; the result is exactly the same
324-
## as for the ordinary (left) map and zip, but unnecessarily using a
325-
## recursive process for something that can be done using a linear one.
326-
## For documentation only. For working mapr, zipr, see unpythonic.it.
327-
## The trick is in the order in which the recurser yields its results.
328-
#def testme():
329-
# squaretwo = lambda a, b: (a**2, b**2)
330-
# print(tuple(mapr(squaretwo, (1, 2, 3), (4, 5))))
331-
# print(tuple(map(squaretwo, (1, 2, 3), (4, 5))))
323+
# This is **not** how to make a right map; the result is exactly the same
324+
# as for the ordinary (left) map and zip, but unnecessarily using a
325+
# recursive process for something that can be done using a linear one.
326+
# For documentation only. For working mapr, zipr, see unpythonic.it.
327+
# The trick is in the order in which the recurser yields its results.
332328
#
333-
#def mapr(proc, *iterables):
334-
# """Like map, but starting from the right. Recursive process.
329+
# def testme():
330+
# squaretwo = lambda a, b: (a**2, b**2)
331+
# print(tuple(mapr(squaretwo, (1, 2, 3), (4, 5))))
332+
# print(tuple(map(squaretwo, (1, 2, 3), (4, 5))))
335333
#
336-
# See ``rmap`` for the linear process that works by reversing each input.
337-
# """
338-
# def scanproc(*args):
339-
# *elts, _ = args # discard acc
340-
# return proc(*elts)
341-
# # discard the init value with butlast
342-
# return butlast(scanr(scanproc, None, *iterables))
334+
# def mapr(proc, *iterables):
335+
# """Like map, but starting from the right. Recursive process.
343336
#
344-
#def zipr(*iterables):
345-
# """Like zip, but starting from the right. Recursive process.
337+
# See ``rmap`` for the linear process that works by reversing each input.
338+
# """
339+
# def scanproc(*args):
340+
# *elts, _ = args # discard acc
341+
# return proc(*elts)
342+
# # discard the init value with butlast
343+
# return butlast(scanr(scanproc, None, *iterables))
346344
#
347-
# See ``rzip`` for the linear process that works by reversing each input.
348-
# """
349-
# def identity(*args): # unpythonic.fun.identity, but dependency loop
350-
# return args
351-
# return mapr(identity, *iterables)
345+
# def zipr(*iterables):
346+
# """Like zip, but starting from the right. Recursive process.
347+
#
348+
# See ``rzip`` for the linear process that works by reversing each input.
349+
# """
350+
# def identity(*args): # unpythonic.fun.identity, but dependency loop
351+
# return args
352+
# return mapr(identity, *iterables)
352353

353354
def prod(iterable, start=1):
354355
"""Like the builtin sum, but compute the product.

0 commit comments

Comments
 (0)