โมดูล pyclbr ในไลบรารี Python แยกข้อมูลเกี่ยวกับฟังก์ชัน คลาส และเมธอดที่กำหนดในโมดูล Python ข้อมูลถูกแยกจาก Python source code แทนที่จะเป็นการ import โมดูล
โมดูลนี้กำหนดฟังก์ชัน readmodule() ที่คืนค่า dictionary mapping ชื่อคลาสระดับโมดูลไปยัง class descriptors ฟังก์ชันรับชื่อโมดูลเป็นพารามิเตอร์ อาจเป็นชื่อโมดูลภายในแพ็กเกจ ในกรณีนั้น path คือลำดับของ directory paths ที่เพิ่มเข้าไปใน sys.path ซึ่งใช้ในการค้นหา source code ของโมดูล
โค้ดต่อไปนี้ใช้ฟังก์ชัน readmodule() เพื่อ parse คลาสและเมธอดในโมดูล socket ของ Python library
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
โมดูล pyclbr ยังกำหนดฟังก์ชัน readmodule_ex() ที่คืนค่า dictionary ที่มี function หรือ class descriptors สำหรับแต่ละฟังก์ชันและคลาสที่กำหนดในโมดูล Dictionary ที่คืนค่า map ชื่อฟังก์ชันและคลาสระดับโมดูลไปยัง descriptors ของพวกมัน Nested objects ถูกเพิ่มเข้าไปใน children dictionary ของ 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
ฟังก์ชันเหล่านี้สามารถใช้ร่วมกับ custom modules ได้เช่นกัน เพื่อดึง directory ของคลาสและเมธอดที่ผู้ใช้กำหนดเอง
ในตัวอย่างต่อไป โมดูล 'triangles.py' ถูกใช้เพื่อรับโครงสร้างคลาสของมัน
#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
เราจะได้รับคลาสและเมธอดในโมดูล '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}
โมดูล pyclbr กำหนดสองอ็อบเจกต์ – Class object และ Function object
อ็อบเจกต์ Function มี attributes ต่อไปนี้
| file |
ชื่อไฟล์ที่ฟังก์ชันถูกกำหนดไว้ |
| module |
ชื่อโมดูลที่กำหนดฟังก์ชันที่อธิบาย |
| name |
ชื่อของฟังก์ชัน |
| lineno |
หมายเลขบรรทัดในไฟล์ที่การกำหนดเริ่มต้น |
| parent |
สำหรับ top-level functions, None สำหรับ nested functions, parent |
| children |
Dictionary mapping ชื่อไปยัง descriptors สำหรับ nested functions และ classes |
อ็อบเจกต์ Class มี attributes เพิ่มอีกสองอย่างนอกเหนือจากข้างต้น
| super |
ลิสต์ของ Class objects ที่อธิบาย immediate base classes ของคลาสที่ถูกอธิบาย |
| methods |
Dictionary mapping ชื่อเมธอดไปยังหมายเลขบรรทัด |