Skip to content

Commit 9a29eb6

Browse files
committed
CLN: generlize operator/expression printing
1 parent 4f8055a commit 9a29eb6

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

pandas/computation/ops.py

+11-18
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,7 @@ def _update_name(env, key, value):
4949
raise NameError('{0!r} is undefined'.format(key))
5050

5151

52-
class NamedObjectMixin(object):
53-
@property
54-
def typename(self):
55-
return com.pprint_thing(self.__class__.__name__)
56-
57-
58-
class Term(StringMixin, NamedObjectMixin):
52+
class Term(StringMixin):
5953
def __init__(self, name, env):
6054
self.name = name
6155
self.value = _resolve_name(env, name)
@@ -79,7 +73,11 @@ def __init__(self, value, env):
7973
super(Constant, self).__init__(value, env)
8074

8175

82-
class Op(NamedObjectMixin, StringMixin):
76+
def _print_operand(opr):
77+
return opr.name if is_term(opr) else unicode(opr)
78+
79+
80+
class Op(StringMixin):
8381
"""Hold an operator of unknown arity
8482
"""
8583
def __init__(self, op, operands):
@@ -90,12 +88,11 @@ def __iter__(self):
9088
return iter(self.operands)
9189

9290
def __unicode__(self):
93-
op = 'op={1!r}'.format(self.op)
94-
operands = ', '.join('opr_{i}={opr}'.format(i=i, opr=opr)
95-
for i, opr in enumerate(self.operands))
96-
return com.pprint_thing('{0}({op}, '
97-
'{operands})'.format(self.name, op=op,
98-
operands=operands))
91+
"""Print a generic n-ary operator and its operands"""
92+
# recurse over the operands
93+
parened = ('({0})'.format(_print_operand(opr))
94+
for opr in self.operands)
95+
return com.pprint_thing(' {0} '.format(self.op).join(parened))
9996

10097

10198
_cmp_ops_syms = '>', '<', '>=', '<=', '==', '!='
@@ -161,10 +158,6 @@ def __init__(self, op, lhs, rhs):
161158
raise BinaryOperatorError('Invalid binary operator {0}, valid'
162159
' operators are {1}'.format(op, keys))
163160

164-
def __unicode__(self):
165-
return com.pprint_thing('({0}) {1} ({2})'.format(self.lhs, self.op,
166-
self.rhs))
167-
168161
def __call__(self, env):
169162
# handle truediv
170163
if self.op == '/' and env.locals['truediv']:

0 commit comments

Comments
 (0)