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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file pyclassparser-0.6.tar.gz.

File metadata

  • Download URL: pyclassparser-0.6.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for pyclassparser-0.6.tar.gz
Algorithm Hash digest
SHA256 d1be7392cb02269f996d1eed3be018e9a073747f62b87acbeda628367ba4564c
MD5 22bb7da92179702d6695c158566b592b
BLAKE2b-256 14cb66e5ba4f9d4189b28e3ad119a642da70724560928a681ef9271e97b9af49

See more details on using hashes here.

File details

Details for the file pyclassparser-0.6-py3-none-any.whl.

File metadata

  • Download URL: pyclassparser-0.6-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for pyclassparser-0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 6553e3cec1895f075a60d7aae9318f18c4ba38f22619d779396a18c0db513191
MD5 536e495baf1a59497381c1dcf74daefd
BLAKE2b-256 7baf604ba36c4795b625ff471367830039ae019b991e2e19e651fe2259a94326

See more details on using hashes here.

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