Python class browser support

PythonServer Side ProgrammingProgramming

The pyclbr module in Python library extracts information about the functions, classes, and methods defined in a Python module. The information is extracted from the Python source code rather than by importing the module.

This module defines readmodule() function that return a dictionary mapping module-level class names to class descriptors. The function takes a module name as parameter. It may be the name of a module within a package. In that case path is a sequence of directory paths prepended to sys.path, which is used to locate the module source code.

Following code uses readmodule() function to parse classes and methods in Python library's socket module.

import pyclbr

mod = pyclbr.readmodule("socket")

def show(c):
   s = "class " + c.name
   print (s + ":")
   methods = c.methods.items()
   for method, lineno in methods:
      print (" def " + method)
   print()

for k, v in mod.items():
   show(v)
class IntEnum:

class IntFlag:
   def _missing_
   def _create_pseudo_member_
   def __or__
   def __and__
   def __xor__
   def __invert__

class _GiveupOnSendfile:

class socket:
   def __init__
   def __enter__
   def __exit__
   def __repr__
   def __getstate__
   def dup
   def accept
   def makefile
   def _sendfile_use_sendfile
   def _sendfile_use_send
   def _check_sendfile_params
   def sendfile
   def _decref_socketios
   def _real_close
   def close
   def detach
   def family
   def type
   def get_inheritable
   def set_inheritable

class SocketIO:
   def __init__
   def readinto
   def write
   def readable
   def writable
   def seekable
   def fileno
   def name
   def mode
   def close

The pyclbr module also defines readmodule_ex() function that returns a dictionary containing a function or class descriptors for each function and class defined in the module. The returned dictionary maps module-level function and class names to their descriptors. Nested objects are entered into the children dictionary of their parent.

>>> x = pyclbr.readmodule_ex('socket')

>>> for k,v in x.items():
print (k,v)


IntEnum <pyclbr.Class object at 0x000002095D7D0048>
IntFlag <pyclbr.Class object at 0x000002095D7D04E0>
_intenum_converter <pyclbr.Function object at 0x000002095D82F940>
_GiveupOnSendfile <pyclbr.Class object at 0x000002095D822898>
socket <pyclbr.Class object at 0x000002095D8227B8>
fromfd <pyclbr.Function object at 0x000002095D8340B8>
fromshare <pyclbr.Function object at 0x000002095D82FEF0>
socketpair <pyclbr.Function object at 0x000002095D834128>
SocketIO <pyclbr.Class object at 0x000002095D82FA20>
getfqdn <pyclbr.Function object at 0x000002095D8344E0>
create_connection <pyclbr.Function object at 0x000002095D834518>
getaddrinfo <pyclbr.Function object at 0x000002095D834550>

These function can be used along with custom modules as well to fetch directory of user defined classes and methods.

In following example a module 'triangles.py' is used to obtain its class structure.

#triangles.py
import math
class Triangle:
   def __init__(self, a, b, c):
      self.a = a
      self.b = b
     self.c = c
   def area(self):
      s=(self.a+self.b+self.c)/2
      area=math.sqrt(s*(s-self.a)*(s-self.b)*(s-self.c))
      return area
class EquiTriangle(Triangle):
   def __init__(self, a):
      b = a
      c = a
      super().__init__(a,b,c)
   def area(self):
      area=math.sqrt(3)*pow(self.a,2)/4
      return area

We shall now obtain the classes and methods in 'triangles' module.

>>> br = pyclbr.readmodule_ex('triangles')
>>> for i,j in br.items(): print (i,j.methods)

Triangle {'__init__': 3, 'area': 7}
EquiTriangle {'__init__': 12, 'area': 16}

The pyclbr module defines two objects – Class object and Function object.

The Function object possesses following attributes

file
 Name of the file in which the function isdefined.
module
 The name of the module defining the functiondescribed.
name
 The name of the function.
lineno
 The line number in the file where the definitionstarts.
parent
 For top-level functions, None. For nestedfunctions, the parent.
children
 A dictionary mapping name to descriptors fornested functions and classes.

The Class object has two more attributes in addition to above

super
 A list of Class objects which describe the immediate base classes of the class being described.
methods
 A dictionary mapping method names to line numbers.
raja
Published on 17-Jan-2019 07:21:15
Advertisements