home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / fixes / fix_has_key.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  3.2 KB  |  69 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Fixer for has_key().
  5.  
  6. Calls to .has_key() methods are expressed in terms of the 'in'
  7. operator:
  8.  
  9.     d.has_key(k) -> k in d
  10.  
  11. CAVEATS:
  12. 1) While the primary target of this fixer is dict.has_key(), the
  13.    fixer will change any has_key() method call, regardless of its
  14.    class.
  15.  
  16. 2) Cases like this will not be converted:
  17.  
  18.     m = d.has_key
  19.     if m(k):
  20.         ...
  21.  
  22.    Only *calls* to has_key() are converted. While it is possible to
  23.    convert the above to something like
  24.  
  25.     m = d.__contains__
  26.     if m(k):
  27.         ...
  28.  
  29.    this is currently not done.
  30. """
  31. from  import pytree
  32. from pgen2 import token
  33. from  import fixer_base
  34. from fixer_util import Name, parenthesize
  35.  
  36. class FixHasKey(fixer_base.BaseFix):
  37.     PATTERN = "\n    anchor=power<\n        before=any+\n        trailer< '.' 'has_key' >\n        trailer<\n            '('\n            ( not(arglist | argument<any '=' any>) arg=any\n            | arglist<(not argument<any '=' any>) arg=any ','>\n            )\n            ')'\n        >\n        after=any*\n    >\n    |\n    negation=not_test<\n        'not'\n        anchor=power<\n            before=any+\n            trailer< '.' 'has_key' >\n            trailer<\n                '('\n                ( not(arglist | argument<any '=' any>) arg=any\n                | arglist<(not argument<any '=' any>) arg=any ','>\n                )\n                ')'\n            >\n        >\n    >\n    "
  38.     
  39.     def transform(self, node, results):
  40.         if not results:
  41.             raise AssertionError
  42.         syms = self.syms
  43.         if node.parent.type == syms.not_test and self.pattern.match(node.parent):
  44.             return None
  45.         negation = results.get('negation')
  46.         anchor = results['anchor']
  47.         prefix = node.get_prefix()
  48.         before = [ n.clone() for n in results['before'] ]
  49.         arg = results['arg'].clone()
  50.         after = results.get('after')
  51.         before.set_prefix(' ')
  52.         n_op = Name('in', prefix = ' ')
  53.         if negation:
  54.             n_not = Name('not', prefix = ' ')
  55.             n_op = pytree.Node(syms.comp_op, (n_not, n_op))
  56.         
  57.         new = pytree.Node(syms.comparison, (arg, n_op, before))
  58.         if after:
  59.             new = parenthesize(new)
  60.             new = pytree.Node(syms.power, (new,) + tuple(after))
  61.         
  62.         if node.parent.type in (syms.comparison, syms.expr, syms.xor_expr, syms.and_expr, syms.shift_expr, syms.arith_expr, syms.term, syms.factor, syms.power):
  63.             new = parenthesize(new)
  64.         
  65.         new.set_prefix(prefix)
  66.         return new
  67.  
  68.  
  69.