Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python2..../lib2to3/fixes
File: fix_operator.py
"""Fixer for operator functions.
[0] Fix | Delete
[1] Fix | Delete
operator.isCallable(obj) -> hasattr(obj, '__call__')
[2] Fix | Delete
operator.sequenceIncludes(obj) -> operator.contains(obj)
[3] Fix | Delete
operator.isSequenceType(obj) -> isinstance(obj, collections.Sequence)
[4] Fix | Delete
operator.isMappingType(obj) -> isinstance(obj, collections.Mapping)
[5] Fix | Delete
operator.isNumberType(obj) -> isinstance(obj, numbers.Number)
[6] Fix | Delete
operator.repeat(obj, n) -> operator.mul(obj, n)
[7] Fix | Delete
operator.irepeat(obj, n) -> operator.imul(obj, n)
[8] Fix | Delete
"""
[9] Fix | Delete
[10] Fix | Delete
# Local imports
[11] Fix | Delete
from lib2to3 import fixer_base
[12] Fix | Delete
from lib2to3.fixer_util import Call, Name, String, touch_import
[13] Fix | Delete
[14] Fix | Delete
[15] Fix | Delete
def invocation(s):
[16] Fix | Delete
def dec(f):
[17] Fix | Delete
f.invocation = s
[18] Fix | Delete
return f
[19] Fix | Delete
return dec
[20] Fix | Delete
[21] Fix | Delete
[22] Fix | Delete
class FixOperator(fixer_base.BaseFix):
[23] Fix | Delete
BM_compatible = True
[24] Fix | Delete
order = "pre"
[25] Fix | Delete
[26] Fix | Delete
methods = """
[27] Fix | Delete
method=('isCallable'|'sequenceIncludes'
[28] Fix | Delete
|'isSequenceType'|'isMappingType'|'isNumberType'
[29] Fix | Delete
|'repeat'|'irepeat')
[30] Fix | Delete
"""
[31] Fix | Delete
obj = "'(' obj=any ')'"
[32] Fix | Delete
PATTERN = """
[33] Fix | Delete
power< module='operator'
[34] Fix | Delete
trailer< '.' %(methods)s > trailer< %(obj)s > >
[35] Fix | Delete
|
[36] Fix | Delete
power< %(methods)s trailer< %(obj)s > >
[37] Fix | Delete
""" % dict(methods=methods, obj=obj)
[38] Fix | Delete
[39] Fix | Delete
def transform(self, node, results):
[40] Fix | Delete
method = self._check_method(node, results)
[41] Fix | Delete
if method is not None:
[42] Fix | Delete
return method(node, results)
[43] Fix | Delete
[44] Fix | Delete
@invocation("operator.contains(%s)")
[45] Fix | Delete
def _sequenceIncludes(self, node, results):
[46] Fix | Delete
return self._handle_rename(node, results, u"contains")
[47] Fix | Delete
[48] Fix | Delete
@invocation("hasattr(%s, '__call__')")
[49] Fix | Delete
def _isCallable(self, node, results):
[50] Fix | Delete
obj = results["obj"]
[51] Fix | Delete
args = [obj.clone(), String(u", "), String(u"'__call__'")]
[52] Fix | Delete
return Call(Name(u"hasattr"), args, prefix=node.prefix)
[53] Fix | Delete
[54] Fix | Delete
@invocation("operator.mul(%s)")
[55] Fix | Delete
def _repeat(self, node, results):
[56] Fix | Delete
return self._handle_rename(node, results, u"mul")
[57] Fix | Delete
[58] Fix | Delete
@invocation("operator.imul(%s)")
[59] Fix | Delete
def _irepeat(self, node, results):
[60] Fix | Delete
return self._handle_rename(node, results, u"imul")
[61] Fix | Delete
[62] Fix | Delete
@invocation("isinstance(%s, collections.Sequence)")
[63] Fix | Delete
def _isSequenceType(self, node, results):
[64] Fix | Delete
return self._handle_type2abc(node, results, u"collections", u"Sequence")
[65] Fix | Delete
[66] Fix | Delete
@invocation("isinstance(%s, collections.Mapping)")
[67] Fix | Delete
def _isMappingType(self, node, results):
[68] Fix | Delete
return self._handle_type2abc(node, results, u"collections", u"Mapping")
[69] Fix | Delete
[70] Fix | Delete
@invocation("isinstance(%s, numbers.Number)")
[71] Fix | Delete
def _isNumberType(self, node, results):
[72] Fix | Delete
return self._handle_type2abc(node, results, u"numbers", u"Number")
[73] Fix | Delete
[74] Fix | Delete
def _handle_rename(self, node, results, name):
[75] Fix | Delete
method = results["method"][0]
[76] Fix | Delete
method.value = name
[77] Fix | Delete
method.changed()
[78] Fix | Delete
[79] Fix | Delete
def _handle_type2abc(self, node, results, module, abc):
[80] Fix | Delete
touch_import(None, module, node)
[81] Fix | Delete
obj = results["obj"]
[82] Fix | Delete
args = [obj.clone(), String(u", " + u".".join([module, abc]))]
[83] Fix | Delete
return Call(Name(u"isinstance"), args, prefix=node.prefix)
[84] Fix | Delete
[85] Fix | Delete
def _check_method(self, node, results):
[86] Fix | Delete
method = getattr(self, "_" + results["method"][0].value.encode("ascii"))
[87] Fix | Delete
if callable(method):
[88] Fix | Delete
if "module" in results:
[89] Fix | Delete
return method
[90] Fix | Delete
else:
[91] Fix | Delete
sub = (unicode(results["obj"]),)
[92] Fix | Delete
invocation_str = unicode(method.invocation) % sub
[93] Fix | Delete
self.warning(node, u"You should use '%s' here." % invocation_str)
[94] Fix | Delete
return None
[95] Fix | Delete
[96] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function