Saturday, April 7, 2012

new python toys

My organization has hired a new programmer who's into Python and I've been learning from him. I was always able to use Python to get done whatever I needed to get done but there are plenty of things I didn't know about until he showed me. Here's a quick list:
  • the * and ** operators
  • Generators, though I have not yet used them (ideas)
  • ipython: an interactive python shell (not necessarily IronPython). It's been allowing me explore the language quickly (much quicker to type '?' than google the documentation) and interactively; e.g.
    sh-4.2$ ipython
    Python 2.7.2 (default, Oct 27 2011, 01:40:22) 
    Type "copyright", "credits" or "license" for more information.
    
    IPython 0.12 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.
    
    In [1]: 1+
      File "", line 1
        1+
          ^
    SyntaxError: invalid syntax
    
    
    In [2]: import ldap.<tab>
    ldap.async       ldap.dn          ldap.ldapobject  ldap.sasl        ldap.thread      
    ldap.cidict      ldap.filter      ldap.modlist     ldap.schema      ldap.threading   
    ldap.controls    ldap.functions   ldap.resiter     ldap.sys         ldap.traceback   
    
    In [2]: import ldap.filter
    
    In [3]: ldap.filter.<tab>
    ldap.filter.escape_filter_chars  ldap.filter.filter_format        
    
    In [3]: ldap.filter.escape_filter_chars?
    Type:       function
    Base Class: 
    String Form:
    Namespace:  Interactive
    File:       /usr/lib64/python2.7/site-packages/ldap/filter.py
    Definition: ldap.filter.escape_filter_chars(assertion_value, escape_mode=0)
    Docstring:
    Replace all special characters found in assertion_value
    by quoted notation.
    
    escape_mode
        If 0 only special chars mentioned in RFC 2254 are escaped.
        If 1 all NON-ASCII chars are escaped.
        If 2 all chars are escaped.
    
    In [4]:
    
    In [4]: ldap.filter.escape_filter_chars??
    Type:       function
    Base Class: 
    String Form:
    Namespace:  Interactive
    File:       /usr/lib64/python2.7/site-packages/ldap/filter.py
    Definition: ldap.filter.escape_filter_chars(assertion_value, escape_mode=0)
    Source:
    def escape_filter_chars(assertion_value,escape_mode=0):
      """
      Replace all special characters found in assertion_value
      by quoted notation.
      
      escape_mode
          If 0 only special chars mentioned in RFC 2254 are escaped.
          If 1 all NON-ASCII chars are escaped.
          If 2 all chars are escaped.
      """
      if escape_mode:
        r = []
        if escape_mode==1:
          for c in assertion_value:
            if c < '0' or c > 'z' or c in "\\*()":
              c = "\\%02x" % ord(c)
            r.append(c)
        elif escape_mode==2:
          for c in assertion_value:
            r.append("\\%02x" % ord(c))
        else:
          raise ValueError('escape_mode must be 0, 1 or 2.')
        s = ''.join(r)
      else:
        s = assertion_value.replace('\\', r'\5c')
        s = s.replace(r'*', r'\2a')
        s = s.replace(r'(', r'\28')
        s = s.replace(r')', r'\29')
        s = s.replace('\x00', r'\00')
      return s
    
    In [5]:
    ...
    In [60]: import string
    
    In [61]: string.<tab>
    string.Formatter        string.capwords         string.ljust            string.rsplit
    string.Template         string.center           string.lower            string.rstrip
    string.ascii_letters    string.count            string.lowercase        string.split
    string.ascii_lowercase  string.digits           string.lstrip           string.splitfields
    string.ascii_uppercase  string.expandtabs       string.maketrans        string.strip
    string.atof             string.find             string.octdigits        string.swapcase
    string.atof_error       string.hexdigits        string.printable        string.translate
    string.atoi             string.index            string.punctuation      string.upper
    string.atoi_error       string.index_error      string.replace          string.uppercase
    string.atol             string.join             string.rfind            string.whitespace
    string.atol_error       string.joinfields       string.rindex           string.zfill
    string.capitalize       string.letters          string.rjust            
    
    In [61]: string.whitespace
    Out[61]: '\t\n\x0b\x0c\r '
    
    In [62]: string.punctuation
    Out[62]: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
    
    In [63]: