Reverse engineering Python programs for the Java developer

Coming from the the Java mindset and looking at software written in Python left me with some pain points that I had to figure out how to overcome. The major question is what is this object and what can it do. Here are some commands that I have found useful.

The dir command is useful but I found myself calling it and then calling all the fields to see what they do. This function prints out all the attributes listed with dir. This is different that

__dict__ because often values are not stored there.

def dump(obj, level=0):
    for attr in dir(obj):
        if not attr.startswith("_"):
            val = getattr(obj, attr)
            if isinstance(val, (int, float, str, unicode, list, dict, set)):
                print '%30s=%s' % (attr, str(val)[:40])
> import numpy as np
> dir(np)
['ALLOW_THREADS',
 'BUFSIZE',
 'CLIP',
 'ComplexWarning',
 'DataSource',
 'ERR_CALL',
 'ERR_DEFAULT',
 'ERR_IGNORE',
 'ERR_LOG',
...
> import numpy as np
> dump(np)
       ALLOW_THREADS=1
             BUFSIZE=8192
                CLIP=0
            ERR_CALL=3
         ERR_DEFAULT=521
          ERR_IGNORE=0
             ERR_LOG=5
           ERR_PRINT=4
           ERR_RAISE=2
            ERR_WARN=1
FLOATING_POINT_SUPPORT=1
    FPE_DIVIDEBYZERO=1
         FPE_INVALID=8
        FPE_OVERFLOW=2
       FPE_UNDERFLOW=4
                 Inf=inf
            Infinity=inf
             MAXDIMS=32
    MAY_SHARE_BOUNDS=0
     MAY_SHARE_EXACT=-1
                 NAN=nan
                NINF=-inf
               NZERO=-0.0
                 NaN=nan
                PINF=inf
               PZERO=0.0
               RAISE=2
  SHIFT_DIVIDEBYZERO=0
       SHIFT_INVALID=9
      SHIFT_OVERFLOW=3
     SHIFT_UNDERFLOW=6
UFUNC_BUFSIZE_DEFAULT=8192
   UFUNC_PYVALS_NAME=UFUNC_PYVALS
                WRAP=1
                cast={<type 'numpy.uint16'>: <function <lambd
                   e=2.71828182846
         euler_gamma=0.577215664902
                 inf=inf
               infty=inf
       little_endian=True
                 nan=nan
              nbytes={<type 'numpy.uint16'>: 2, <type 'numpy.
            numarray=removed
          oldnumeric=removed
                  pi=3.14159265359
          sctypeDict={0: <type 'numpy.bool_'>, 1: <type 'nump
            sctypeNA={<type 'numpy.int16'>: 'Int16', 'Int32':
             sctypes={'int': [<type 'numpy.int8'>, <type 'num
            typeDict={0: <type 'numpy.bool_'>, 1: <type 'nump
              typeNA={<type 'numpy.int16'>: 'Int16', 'Int32':
           typecodes={'All': '?bhilqpBHILQPefdgFDGSUVOMm', 'C

Be the first to comment

Enter Comment

Your email address will not be published.


*


*