Skip to content

Commit bf8b007

Browse files
committed
Style: eliminate line continuations for great justice
More pythonic to use parentheses.
1 parent 752f5e1 commit bf8b007

File tree

11 files changed

+64
-55
lines changed

11 files changed

+64
-55
lines changed

unpythonic/ec.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,18 @@ def s(loop, acc=0, i=0):
200200
else: # single tag
201201
tags = set((tags,))
202202

203+
def shouldcatch(e):
204+
return ((tags is None and e.allow_catchall) or
205+
(catch_untagged and e.tag is None) or
206+
(tags is not None and e.tag is not None and e.tag in tags))
207+
203208
def decorator(f):
204209
@wraps(f)
205210
def catchpoint(*args, **kwargs):
206211
try:
207212
return f(*args, **kwargs)
208213
except Escape as e:
209-
if (tags is None and e.allow_catchall) or \
210-
(catch_untagged and e.tag is None) or \
211-
(tags is not None and e.tag is not None and e.tag in tags):
214+
if shouldcatch(e):
212215
return e.value
213216
else: # meant for someone else, pass it on
214217
raise

unpythonic/fold.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ def unfold1(proc, init):
284284
def step2(k): # x0, x0 + 2, x0 + 4, ...
285285
return (k, k + 2)
286286
287-
assert tuple(take(10, unfold1(step2, 10))) == \\
288-
(10, 12, 14, 16, 18, 20, 22, 24, 26, 28)
287+
assert (tuple(take(10, unfold1(step2, 10))) ==
288+
(10, 12, 14, 16, 18, 20, 22, 24, 26, 28))
289289
"""
290290
state = init
291291
while True:
@@ -309,8 +309,8 @@ def unfold(proc, *inits):
309309
def fibo(a, b):
310310
return (a, b, a + b)
311311
312-
assert tuple(take(10, unfold(fibo, 1, 1))) == \\
313-
(1, 1, 2, 3, 5, 8, 13, 21, 34, 55)
312+
assert (tuple(take(10, unfold(fibo, 1, 1))) ==
313+
(1, 1, 2, 3, 5, 8, 13, 21, 34, 55))
314314
"""
315315
states = inits
316316
while True:

unpythonic/it.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,18 @@ def msqrt(x): # multivalued sqrt
203203
else:
204204
s = x**0.5
205205
return (s, -s)
206-
assert tuple(flatmap(msqrt, (0, 1, 4, 9))) == \\
207-
(0., 1., -1., 2., -2., 3., -3.)
206+
assert (tuple(flatmap(msqrt, (0, 1, 4, 9))) ==
207+
(0., 1., -1., 2., -2., 3., -3.))
208208
209209
def add_and_tuplify(a, b):
210210
return (a + b,)
211-
assert tuple(flatmap(add_and_tuplify, (10, 20, 30), (1, 2, 3))) == \\
212-
(11, 22, 33)
211+
assert (tuple(flatmap(add_and_tuplify, (10, 20, 30), (1, 2, 3))) ==
212+
(11, 22, 33))
213213
214214
def sum_and_diff(a, b):
215215
return (a + b, a - b)
216-
assert tuple(flatmap(sum_and_diff, (10, 20, 30), (1, 2, 3))) == \\
217-
(11, 9, 22, 18, 33, 27)
216+
assert (tuple(flatmap(sum_and_diff, (10, 20, 30), (1, 2, 3))) ==
217+
(11, 9, 22, 18, 33, 27))
218218
"""
219219
yield from chain.from_iterable(map(f, iterable0, *iterables))
220220
# for xs in map(f, iterable0, *iterables):
@@ -531,10 +531,10 @@ def flatten_in(iterable, pred=None):
531531
532532
is_nested = lambda e: all(isinstance(x, (list, tuple)) for x in e)
533533
data = (((1, 2), ((3, 4), (5, 6)), 7), ((8, 9), (10, 11)))
534-
assert tuple(flatten(data, is_nested)) == \\
535-
(((1, 2), ((3, 4), (5, 6)), 7), (8, 9), (10, 11))
536-
assert tuple(flatten_in(data, is_nested)) == \\
537-
(((1, 2), (3, 4), (5, 6), 7), (8, 9), (10, 11))
534+
assert (tuple(flatten(data, is_nested)) ==
535+
(((1, 2), ((3, 4), (5, 6)), 7), (8, 9), (10, 11)))
536+
assert (tuple(flatten_in(data, is_nested)) ==
537+
(((1, 2), (3, 4), (5, 6), 7), (8, 9), (10, 11)))
538538
"""
539539
pred = pred or (lambda x: True)
540540
it = iter(iterable)

unpythonic/syntax/autoref.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,19 @@ def isexpandedautorefblock(tree):
8989
if not (type(tree) is With and len(tree.items) == 1):
9090
return False
9191
ctxmanager = tree.items[0].context_expr
92-
return type(ctxmanager) is Call and \
93-
type(ctxmanager.func) is Name and ctxmanager.func.id == "AutorefMarker" and \
94-
len(ctxmanager.args) == 1 and type(ctxmanager.args[0]) is Str
92+
return (type(ctxmanager) is Call and
93+
type(ctxmanager.func) is Name and ctxmanager.func.id == "AutorefMarker" and
94+
len(ctxmanager.args) == 1 and type(ctxmanager.args[0]) is Str)
9595
def getreferent(tree):
9696
return tree.items[0].context_expr.args[0].s
9797

9898
# (lambda _ar314: _ar314[1] if _ar314[0] else x)(_autoref_resolve((p, o, "x")))
9999
def isautoreference(tree):
100-
return type(tree) is Call and \
101-
len(tree.args) == 1 and type(tree.args[0]) is Call and \
102-
type(tree.args[0].func) is Name and tree.args[0].func.id == "_autoref_resolve" and \
103-
type(tree.func) is Lambda and len(tree.func.args.args) == 1 and \
104-
tree.func.args.args[0].arg.startswith("_ar")
100+
return (type(tree) is Call and
101+
len(tree.args) == 1 and type(tree.args[0]) is Call and
102+
type(tree.args[0].func) is Name and tree.args[0].func.id == "_autoref_resolve" and
103+
type(tree.func) is Lambda and len(tree.func.args.args) == 1 and
104+
tree.func.args.args[0].arg.startswith("_ar"))
105105
def get_resolver_list(tree): # (p, o, "x")
106106
return tree.args[0].args[0].elts
107107
def add_to_resolver_list(tree, objnode):

unpythonic/syntax/lambdatools.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ def getargs(tree): # tree: FunctionDef, AsyncFunctionDef, Lambda
186186
argnames.append(a.kwarg.arg)
187187
return argnames
188188

189+
def isfunctionoruserlambda(tree):
190+
return ((type(tree) in (FunctionDef, AsyncFunctionDef)) or
191+
(type(tree) is Lambda and id(tree) in userlambdas))
192+
189193
# Create a renamed reference to the env() constructor to be sure the Call
190194
# nodes added by us have a unique .func (not used by other macros or user code)
191195
_ismakeenv = make_isxpred("_envify")
@@ -199,8 +203,7 @@ def isourupdate(thecall):
199203
return False
200204
return thecall.func.attr == "update" and any(isx(thecall.func.value, x) for x in enames)
201205

202-
if type(tree) in (FunctionDef, AsyncFunctionDef) or \
203-
(type(tree) is Lambda and id(tree) in userlambdas):
206+
if isfunctionoruserlambda(tree):
204207
argnames = getargs(tree)
205208
if argnames:
206209
# prepend env init to function body, update bindings

unpythonic/syntax/lazify.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ def transform_starred(tree, dstarred=False):
267267
# namelambda() is used by let[] and do[]
268268
# Lazy() is a strict function, takes a lambda, constructs a Lazy object
269269
# _autoref_resolve doesn't need any special handling
270-
elif isdo(tree) or is_decorator(tree.func, "namelambda") or \
271-
any(isx(tree.func, s) for s in _ctorcalls_all) or isx(tree.func, isLazy) or \
272-
any(isx(tree.func, s) for s in ("_autoref_resolve", "AutorefMarker")):
270+
elif (isdo(tree) or is_decorator(tree.func, "namelambda") or
271+
any(isx(tree.func, s) for s in _ctorcalls_all) or isx(tree.func, isLazy) or
272+
any(isx(tree.func, s) for s in ("_autoref_resolve", "AutorefMarker"))):
273273
# here we know the operator (.func) to be one of specific names;
274274
# don't transform it to avoid confusing lazyrec[] (important if this
275275
# is an inner call in the arglist of an outer, lazy call, since it

unpythonic/syntax/letdoutil.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def where(*bindings):
1818
# avoid circular dependency; can't import from .util, so implement a minimal isx() for what we need
1919
def _isx(tree, x):
2020
ismatch = x if callable(x) else lambda s: s == x
21-
return (type(tree) is Name and ismatch(tree.id)) or \
22-
(type(tree) is Captured and ismatch(tree.name))
21+
return ((type(tree) is Name and ismatch(tree.id)) or
22+
(type(tree) is Captured and ismatch(tree.name)))
2323
def _pred(x):
2424
rematch = re.match
2525
pat = re.compile(r"^{}\d*$".format(x))
@@ -46,8 +46,8 @@ def _canonize_bindings(elts, locref, allow_call_in_name_position=False):
4646
allow_call_in_name_position: used by let_syntax to allow template definitions.
4747
"""
4848
def iskey(x):
49-
return type(x) is Name or \
50-
allow_call_in_name_position and type(x) is Call and type(x.func) is Name
49+
return ((type(x) is Name) or
50+
(allow_call_in_name_position and type(x) is Call and type(x.func) is Name))
5151
if len(elts) == 2 and iskey(elts[0]):
5252
return [Tuple(elts=elts, lineno=locref.lineno, col_offset=locref.col_offset)]
5353
if all((type(b) is Tuple and len(b.elts) == 2 and iskey(b.elts[0])) for b in elts):
@@ -150,9 +150,15 @@ def _ishaskellylet(tree):
150150
To detect the full expression including the ``let[]``, use ``islet`` instead.
151151
"""
152152
# let[((k0, v0), ...) in body]
153-
if type(tree) is Compare and \
154-
len(tree.ops) == 1 and type(tree.ops[0]) is In and \
155-
type(tree.left) is Tuple:
153+
def maybeiscontentofletin(tree):
154+
return (type(tree) is Compare and
155+
len(tree.ops) == 1 and type(tree.ops[0]) is In and
156+
type(tree.left) is Tuple)
157+
# let[body, where((k0, v0), ...)]
158+
def maybeiscontentofletwhere(tree):
159+
return type(tree) is Tuple and len(tree.elts) == 2 and type(tree.elts[1]) is Call
160+
161+
if maybeiscontentofletin(tree):
156162
bindings = tree.left
157163
if all((type(b) is Tuple and len(b.elts) == 2 and type(b.elts[0]) is Name)
158164
for b in bindings.elts):
@@ -163,8 +169,7 @@ def _ishaskellylet(tree):
163169
# require it, because they look like function calls in the AST.)
164170
if len(bindings.elts) == 2 and type(bindings.elts[0]) is Name:
165171
return "in_expr"
166-
# let[body, where((k0, v0), ...)]
167-
elif type(tree) is Tuple and len(tree.elts) == 2 and type(tree.elts[1]) is Call:
172+
elif maybeiscontentofletwhere(tree):
168173
thecall = tree.elts[1]
169174
if type(thecall.func) is Name and thecall.func.id == "where":
170175
return "where_expr"
@@ -197,9 +202,9 @@ def isdo(tree, expanded=True):
197202
return False
198203
return kind
199204
# TODO: detect also do[] with a single expression inside? (now requires a comma)
200-
return type(tree) is Subscript and \
201-
type(tree.value) is Name and any(tree.value.id == x for x in ("do", "do0")) and \
202-
type(tree.slice) is Index and type(tree.slice.value) is Tuple
205+
return (type(tree) is Subscript and
206+
type(tree.value) is Name and any(tree.value.id == x for x in ("do", "do0")) and
207+
type(tree.slice) is Index and type(tree.slice.value) is Tuple)
203208

204209
def isenvassign(tree):
205210
"""Detect whether tree is an unpythonic ``env`` assignment, ``name << value``.

unpythonic/syntax/tailtools.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,7 @@ def iscallcc(tree):
255255
if type(tree) not in (Assign, Expr):
256256
return False
257257
tree = tree.value
258-
if type(tree) is Subscript and type(tree.value) is Name \
259-
and tree.value.id == "call_cc":
258+
if type(tree) is Subscript and type(tree.value) is Name and tree.value.id == "call_cc":
260259
if type(tree.slice) is Index:
261260
return True
262261
assert False, "expected single expr, not slice in call_cc[...]"

unpythonic/syntax/util.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ def isx(tree, x, accept_attr=True):
4040
# and bare names for anything hq[]'d; but any references that appear
4141
# explicitly in the user code may use either bare names or somemodule.f.
4242
ismatch = x if callable(x) else lambda s: s == x
43-
return (type(tree) is Name and ismatch(tree.id)) or \
44-
(type(tree) is Captured and ismatch(tree.name)) or \
45-
(accept_attr and type(tree) is Attribute and ismatch(tree.attr))
43+
return ((type(tree) is Name and ismatch(tree.id)) or
44+
(type(tree) is Captured and ismatch(tree.name)) or
45+
(accept_attr and type(tree) is Attribute and ismatch(tree.attr)))
4646

4747
def make_isxpred(x):
4848
"""Make a predicate for isx.
@@ -160,8 +160,8 @@ def is_decorator(tree, fname):
160160
161161
- ``Call`` whose ``.func`` matches the above rule (parametric decorator).
162162
"""
163-
return isx(tree, fname) or \
164-
(type(tree) is Call and isx(tree.func, fname))
163+
return ((isx(tree, fname)) or
164+
(type(tree) is Call and isx(tree.func, fname)))
165165

166166
def is_lambda_decorator(tree, fname=None):
167167
"""Test tree whether it decorates a lambda with ``fname``.
@@ -180,8 +180,8 @@ def is_lambda_decorator(tree, fname=None):
180180
trampolined(arg) # --> non-parametric decorator
181181
looped_over(range(10), acc=0)(arg) # --> parametric decorator
182182
"""
183-
return (type(tree) is Call and len(tree.args) == 1) and \
184-
(fname is None or is_decorator(tree.func, fname))
183+
return ((type(tree) is Call and len(tree.args) == 1) and
184+
(fname is None or is_decorator(tree.func, fname)))
185185

186186
def is_decorated_lambda(tree, mode):
187187
"""Detect a tree of the form f(g(h(lambda ...: ...)))

unpythonic/test/test_ec.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ def erroneous(ec):
6060

6161
# def catching_truth_table():
6262
# def check(tags, catch_untagged, e):
63-
# if (tags is None and e.allow_catchall) or \
64-
# (catch_untagged and e.tag is None):
63+
# if (tags is None and e.allow_catchall) or (catch_untagged and e.tag is None):
6564
# return 2 # unconditional catch
6665
# if (tags is not None and e.tag is not None): # and e.tag in tags):
6766
# return 1 # catch if tags match

0 commit comments

Comments
 (0)