]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/lib2to3/fixes/fix_operator.py
1 """Fixer for operator functions.
3 operator.isCallable(obj) -> hasattr(obj, '__call__')
4 operator.sequenceIncludes(obj) -> operator.contains(obj)
5 operator.isSequenceType(obj) -> isinstance(obj, collections.Sequence)
6 operator.isMappingType(obj) -> isinstance(obj, collections.Mapping)
7 operator.isNumberType(obj) -> isinstance(obj, numbers.Number)
8 operator.repeat(obj, n) -> operator.mul(obj, n)
9 operator.irepeat(obj, n) -> operator.imul(obj, n)
13 from lib2to3
import fixer_base
14 from lib2to3
.fixer_util
import Call
, Name
, String
, touch_import
24 class FixOperator(fixer_base
.BaseFix
):
29 method=('isCallable'|'sequenceIncludes'
30 |'isSequenceType'|'isMappingType'|'isNumberType'
33 obj
= "'(' obj=any ')'"
35 power< module='operator'
36 trailer< '.' %(methods)s > trailer< %(obj)s > >
38 power< %(methods)s trailer< %(obj)s > >
39 """ % dict(methods
=methods
, obj
=obj
)
41 def transform(self
, node
, results
):
42 method
= self
._check
_method
(node
, results
)
43 if method
is not None:
44 return method(node
, results
)
46 @invocation("operator.contains(%s)")
47 def _sequenceIncludes(self
, node
, results
):
48 return self
._handle
_rename
(node
, results
, u
"contains")
50 @invocation("hasattr(%s, '__call__')")
51 def _isCallable(self
, node
, results
):
53 args
= [obj
.clone(), String(u
", "), String(u
"'__call__'")]
54 return Call(Name(u
"hasattr"), args
, prefix
=node
.prefix
)
56 @invocation("operator.mul(%s)")
57 def _repeat(self
, node
, results
):
58 return self
._handle
_rename
(node
, results
, u
"mul")
60 @invocation("operator.imul(%s)")
61 def _irepeat(self
, node
, results
):
62 return self
._handle
_rename
(node
, results
, u
"imul")
64 @invocation("isinstance(%s, collections.Sequence)")
65 def _isSequenceType(self
, node
, results
):
66 return self
._handle
_type
2abc
(node
, results
, u
"collections", u
"Sequence")
68 @invocation("isinstance(%s, collections.Mapping)")
69 def _isMappingType(self
, node
, results
):
70 return self
._handle
_type
2abc
(node
, results
, u
"collections", u
"Mapping")
72 @invocation("isinstance(%s, numbers.Number)")
73 def _isNumberType(self
, node
, results
):
74 return self
._handle
_type
2abc
(node
, results
, u
"numbers", u
"Number")
76 def _handle_rename(self
, node
, results
, name
):
77 method
= results
["method"][0]
81 def _handle_type2abc(self
, node
, results
, module
, abc
):
82 touch_import(None, module
, node
)
84 args
= [obj
.clone(), String(u
", " + u
".".join([module
, abc
]))]
85 return Call(Name(u
"isinstance"), args
, prefix
=node
.prefix
)
87 def _check_method(self
, node
, results
):
88 method
= getattr(self
, "_" + results
["method"][0].value
.encode("ascii"))
90 if "module" in results
:
93 sub
= (unicode(results
["obj"]),)
94 invocation_str
= unicode(method
.invocation
) % sub
95 self
.warning(node
, u
"You should use '%s' here." % invocation_str
)