Skip to main content

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 attrs for attribute definition and attrs-strict for 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

pyclassparser-0.6.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

pyclassparser-0.6-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page