Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLN: replace %s syntax with .format in core.computation #17209

Merged
merged 2 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions pandas/core/computation/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,11 @@ def _align_core(terms):

ordm = np.log10(max(1, abs(reindexer_size - term_axis_size)))
if ordm >= 1 and reindexer_size >= 10000:
warnings.warn('Alignment difference on axis {0} is larger '
'than an order of magnitude on term {1!r}, '
'by more than {2:.4g}; performance may '
'suffer'.format(axis, terms[i].name, ordm),
category=PerformanceWarning,
stacklevel=6)
w = ('Alignment difference on axis {axis} is larger '
'than an order of magnitude on term {term!r}, by '
'more than {ordm:.4g}; performance may suffer'
).format(axis=axis, term=terms[i].name, ordm=ordm)
warnings.warn(w, category=PerformanceWarning, stacklevel=6)

if transpose:
f = partial(ti.reindex, index=reindexer, copy=False)
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/computation/engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def _check_ne_builtin_clash(expr):

if overlap:
s = ', '.join(map(repr, overlap))
raise NumExprClobberingError('Variables in expression "%s" '
'overlap with builtins: (%s)' % (expr, s))
raise NumExprClobberingError('Variables in expression "{expr}" '
'overlap with builtins: ({s})'
.format(expr=expr, s=s))


class AbstractEngine(object):
Expand Down
13 changes: 7 additions & 6 deletions pandas/core/computation/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ def _check_engine(engine):
engine = 'python'

if engine not in _engines:
raise KeyError('Invalid engine {0!r} passed, valid engines are'
' {1}'.format(engine, list(_engines.keys())))
valid = list(_engines.keys())
raise KeyError('Invalid engine {engine!r} passed, valid engines are'
' {valid}'.format(engine=engine, valid=valid))

# TODO: validate this in a more general way (thinking of future engines
# that won't necessarily be import-able)
Expand Down Expand Up @@ -69,17 +70,17 @@ def _check_parser(parser):
* If an invalid parser is passed
"""
if parser not in _parsers:
raise KeyError('Invalid parser {0!r} passed, valid parsers are'
' {1}'.format(parser, _parsers.keys()))
raise KeyError('Invalid parser {parser!r} passed, valid parsers are'
' {valid}'.format(parser=parser, valid=_parsers.keys()))


def _check_resolvers(resolvers):
if resolvers is not None:
for resolver in resolvers:
if not hasattr(resolver, '__getitem__'):
name = type(resolver).__name__
raise TypeError('Resolver of type %r does not implement '
'the __getitem__ method' % name)
raise TypeError('Resolver of type {name!r} does not implement '
'the __getitem__ method'.format(name=name))


def _check_expression(expr):
Expand Down
45 changes: 25 additions & 20 deletions pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ def _filter_nodes(superclass, all_nodes=_all_nodes):
# and we don't want `stmt` and friends in their so get only the class whose
# names are capitalized
_base_supported_nodes = (_all_node_names - _unsupported_nodes) | _hacked_nodes
_msg = 'cannot both support and not support {0}'.format(_unsupported_nodes &
_base_supported_nodes)
_msg = 'cannot both support and not support {intersection}'.format(
intersection=_unsupported_nodes & _base_supported_nodes)
assert not _unsupported_nodes & _base_supported_nodes, _msg


Expand All @@ -200,8 +200,8 @@ def _node_not_implemented(node_name, cls):
"""

def f(self, *args, **kwargs):
raise NotImplementedError("{0!r} nodes are not "
"implemented".format(node_name))
raise NotImplementedError("{name!r} nodes are not "
"implemented".format(name=node_name))
return f


Expand All @@ -217,7 +217,7 @@ def disallowed(cls):
cls.unsupported_nodes = ()
for node in nodes:
new_method = _node_not_implemented(node, cls)
name = 'visit_{0}'.format(node)
name = 'visit_{node}'.format(node=node)
cls.unsupported_nodes += (name,)
setattr(cls, name, new_method)
return cls
Expand Down Expand Up @@ -251,13 +251,14 @@ def add_ops(op_classes):
"""Decorator to add default implementation of ops."""
def f(cls):
for op_attr_name, op_class in compat.iteritems(op_classes):
ops = getattr(cls, '{0}_ops'.format(op_attr_name))
ops_map = getattr(cls, '{0}_op_nodes_map'.format(op_attr_name))
ops = getattr(cls, '{name}_ops'.format(name=op_attr_name))
ops_map = getattr(cls, '{name}_op_nodes_map'.format(
name=op_attr_name))
for op in ops:
op_node = ops_map[op]
if op_node is not None:
made_op = _op_maker(op_class, op)
setattr(cls, 'visit_{0}'.format(op_node), made_op)
setattr(cls, 'visit_{node}'.format(node=op_node), made_op)
return cls
return f

Expand Down Expand Up @@ -388,9 +389,10 @@ def _maybe_evaluate_binop(self, op, op_class, lhs, rhs,
res = op(lhs, rhs)

if res.has_invalid_return_type:
raise TypeError("unsupported operand type(s) for {0}:"
" '{1}' and '{2}'".format(res.op, lhs.type,
rhs.type))
raise TypeError("unsupported operand type(s) for {op}:"
" '{lhs}' and '{rhs}'".format(op=res.op,
lhs=lhs.type,
rhs=rhs.type))

if self.engine != 'pytables':
if (res.op in _cmp_ops_syms and
Expand Down Expand Up @@ -527,7 +529,8 @@ def visit_Attribute(self, node, **kwargs):
if isinstance(value, ast.Name) and value.id == attr:
return resolved

raise ValueError("Invalid Attribute context {0}".format(ctx.__name__))
raise ValueError("Invalid Attribute context {name}"
.format(name=ctx.__name__))

def visit_Call_35(self, node, side=None, **kwargs):
""" in 3.5 the starargs attribute was changed to be more flexible,
Expand All @@ -549,7 +552,8 @@ def visit_Call_35(self, node, side=None, **kwargs):
raise

if res is None:
raise ValueError("Invalid function call {0}".format(node.func.id))
raise ValueError("Invalid function call {func}"
.format(func=node.func.id))
if hasattr(res, 'value'):
res = res.value

Expand All @@ -558,8 +562,8 @@ def visit_Call_35(self, node, side=None, **kwargs):
new_args = [self.visit(arg) for arg in node.args]

if node.keywords:
raise TypeError("Function \"{0}\" does not support keyword "
"arguments".format(res.name))
raise TypeError("Function \"{name}\" does not support keyword "
"arguments".format(name=res.name))

return res(*new_args, **kwargs)

Expand All @@ -570,7 +574,7 @@ def visit_Call_35(self, node, side=None, **kwargs):
for key in node.keywords:
if not isinstance(key, ast.keyword):
raise ValueError("keyword error in function call "
"'{0}'".format(node.func.id))
"'{func}'".format(func=node.func.id))

if key.arg:
# TODO: bug?
Expand Down Expand Up @@ -598,7 +602,8 @@ def visit_Call_legacy(self, node, side=None, **kwargs):
raise

if res is None:
raise ValueError("Invalid function call {0}".format(node.func.id))
raise ValueError("Invalid function call {func}"
.format(func=node.func.id))
if hasattr(res, 'value'):
res = res.value

Expand All @@ -609,8 +614,8 @@ def visit_Call_legacy(self, node, side=None, **kwargs):
args += self.visit(node.starargs)

if node.keywords or node.kwargs:
raise TypeError("Function \"{0}\" does not support keyword "
"arguments".format(res.name))
raise TypeError("Function \"{name}\" does not support keyword "
"arguments".format(name=res.name))

return res(*args, **kwargs)

Expand All @@ -623,7 +628,7 @@ def visit_Call_legacy(self, node, side=None, **kwargs):
for key in node.keywords:
if not isinstance(key, ast.keyword):
raise ValueError("keyword error in function call "
"'{0}'".format(node.func.id))
"'{func}'".format(func=node.func.id))
keywords[key.arg] = self.visit(key.value).value
if node.kwargs is not None:
keywords.update(self.visit(node.kwargs).value)
Expand Down
14 changes: 7 additions & 7 deletions pandas/core/computation/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _evaluate_numexpr(op, op_str, a, b, raise_on_error=False, truediv=True,

a_value = getattr(a, "values", a)
b_value = getattr(b, "values", b)
result = ne.evaluate('a_value %s b_value' % op_str,
result = ne.evaluate('a_value {op} b_value'.format(op=op_str),
local_dict={'a_value': a_value,
'b_value': b_value},
casting='safe', truediv=truediv,
Expand Down Expand Up @@ -177,15 +177,15 @@ def _bool_arith_check(op_str, a, b, not_allowed=frozenset(('/', '//', '**')),

if _has_bool_dtype(a) and _has_bool_dtype(b):
if op_str in unsupported:
warnings.warn("evaluating in Python space because the %r operator"
" is not supported by numexpr for the bool "
"dtype, use %r instead" % (op_str,
unsupported[op_str]))
warnings.warn("evaluating in Python space because the {op!r} "
"operator is not supported by numexpr for "
"the bool dtype, use {alt_op!r} instead"
.format(op=op_str, alt_op=unsupported[op_str]))
return False

if op_str in not_allowed:
raise NotImplementedError("operator %r not implemented for bool "
"dtypes" % op_str)
raise NotImplementedError("operator {op!r} not implemented for "
"bool dtypes".format(op=op_str))
return True


Expand Down
Loading