Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Python Class Browser Support (pyclbr)

Module pyclbr trong thư viện Python trích xuất thông tin về functions, classes, methods định nghĩa trong Python module. Thông tin trích xuất từ source code Python thay vì import module.

Module định nghĩa hàm readmodule() trả về dictionary mapping module-level class names đến class descriptors. Hàm nhận tên module làm tham số. Có thể là tên module trong package. Lúc đó path là sequence directory paths prepended vào sys.path, dùng để locate module source code.

Code sau dùng readmodule() parse classes và methods trong module socket của thư viện chuẩn:


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

Module pyclbr cũng định nghĩa readmodule_ex() trả về dictionary chứa function hoặc class descriptors cho mỗi function và class định nghĩa trong module. Dictionary trả về map module-level function/class names đến descriptors. Nested objects nhập vào children dictionary của parent.


>>> x = pyclbr.readmodule_ex('socket')
>>> for k, v in x.items():
...     print(k, v)
IntEnum 
IntFlag 
_intenum_converter 
_GiveupOnSendfile 
socket 
fromfd 
fromshare 
socketpair 
SocketIO 
getfqdn 
create_connection 
getaddrinfo 

Các function này dùng được với custom modules để lấy directory của user-defined classes và methods.

Ví dụ: module triangles.py lấy 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

Lấy classes và methods trong module triangles:


>>> 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}

Module pyclbr định nghĩa hai object: Class object và Function object.

Function object attributes:

fileTên file định nghĩa function.
moduleTên module định nghĩa function.
nameTên function.
linenoSố dòng bắt đầu định nghĩa trong file.
parentTop-level functions: None. Nested functions: parent.
childrenDict mapping name đến descriptors cho nested functions/classes.

Class object có thêm 2 attributes:

superList Class objects mô tả immediate base classes.
methodsDict mapping method names đến line numbers.