Parse Python classes and attributes from a string of Python code.
Project description
PyClassParser
PyClassParser is a Python library designed to parse Python class definitions and extract class attributes. It uses the attrs library for defining class attributes and ensures type safety with attrs-strict.
Currently it only supports Pydantic class format.
Features
- Parses Python class definitions to extract class names and their attributes.
- Uses
attrsfor attribute definition andattrs-strictfor type validation. - Handles optional attributes and attributes with default values.
Installation
To install PyClassParser, you need to have Python 3.x installed. You can install the required dependencies using pip:
pip install attrs attrs-strict
Usage
Here's how you can use PyClassParser to parse Python class definitions:
Example Code
Create a Python script, for example example.py:
from pyclassparser import ClassParser
code = """
class MyClass:
attr1: int
attr2: str = "default"
class AnotherClass:
attr3: float
"""
parser = ClassParser(code=code)
print(parser.output)
print(parser.class_data_dict)
Example Output
Running the above script should give you the parsed output of class definitions:
class MyClass:
attr1: int
attr2: str = "default"
class AnotherClass:
attr3: float
{'MyClass': [ClassATTR(attr_value='attr1', attr_type='int'), ClassATTR(attr_value='attr2', attr_type='str')],
'AnotherClass': [ClassATTR(attr_value='attr3', attr_type='float')]}
Contributing
Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.
License
This project is licensed under the MIT License.
Acknowledgements
This library utilizes attrs and attrs-strict for attribute definition and type validation. Special thanks to the authors and contributors of these libraries.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyclassparser-0.7.tar.gz.
File metadata
- Download URL: pyclassparser-0.7.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c13bd5f552ede66902fd1b41eaf2c87a2b39a886209321d9bd6e52429e27808c
|
|
| MD5 |
da658a8a6d1612b728b4967ca43b21a2
|
|
| BLAKE2b-256 |
dfc3c8949894c6fd9a98789a736657f23afa68e395c878c343718e6df78cda96
|
File details
Details for the file pyclassparser-0.7-py3-none-any.whl.
File metadata
- Download URL: pyclassparser-0.7-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
377ba7d4322ad3d139dddbcc0eb32af19f00a902729e334e59366844466d78c0
|
|
| MD5 |
933c7a7492b33ec4f512ad7b59a4692f
|
|
| BLAKE2b-256 |
9c5c4b2ee02d8633404911344a36746e7b4bbffa7dd53c4ec9e389ed4d239e13
|