README: XML to Python Code Generator
Overview
This project is a Python-based tool that dynamically generates Python classes, instances, and dependency graphs from a given XML file. The tool analyzes the structure of the XML document and creates Python code that can:
- Define classes based on the XML structure.
- Generate instances for each XML element.
- Export instances to CSV files.
- Visualize class dependencies as a PDF graph using Graphviz.
Features
- Automatic Class Generation: Generates Python classes with attributes and relationships derived from XML tags and attributes.
- Instance Creation: Creates instances for each XML element.
- CSV Export: Automatically saves instances to CSV files.
- Dependency Graph: Generates a PDF graph of class dependencies using Graphviz.
Prerequisites
Python Libraries
This project requires the following Python libraries:
osxml.etree.ElementTreeuuidcollectionskeywordpandasgraphviz
Install the required libraries using:
pip install pandas graphviz
Graphviz
The graphviz library must be installed on your system for generating dependency graphs. Follow the installation guide for your platform:
- Ubuntu/Debian:
sudo apt-get install graphviz - MacOS:
brew install graphviz - Windows: Download and install from Graphviz Download Page.
Usage
Example
- Place your XML file (e.g.,
drugbank_partial.xml) in the project directory. - Run the script using:
python your_script_name.py
Main Function
Call the generate_python_code function with the path to your XML file:
from your_script_name import generate_python_code
generate_python_code("path_to_your_xml.xml")
Output
The script will generate the following in the generated_code directory:
- Class Files: Individual Python files for each class.
- Main Script: A
generated_main.pyfile to initialize instances and export them to CSV. - Dependency Graph: A
class_dependencies.pdffile visualizing class relationships.
Functions Explained
Key Functions
escape_string(value: str) -> str
Escapes problematic characters (e.g., \, ', \n) in strings.
sanitize(name: str) -> str
Sanitizes XML tag/attribute names by:
- Removing namespaces.
- Replacing invalid characters with underscores.
- Adding underscores if the name conflicts with Python keywords.
analyze_structure(root: ET.Element)
Analyzes the XML structure to:
- Count child elements and attributes.
- Identify potential fields for class definitions.
define_class(class_name: str, potential_fields: dict, element_counts: dict) -> str
Generates Python class code for a given class name based on the XML structure.
generate_instance(...)
Creates an instance of a class and recursively handles its child elements.
parse_element(...)
Parses an XML element and generates corresponding Python code dynamically.
generate_python_code(xml_file: str, output_dir: str = "generated_code", generate_graph: bool = True)
Main function to generate Python code and optional dependency graphs.
generate_dependency_graph(output_dir: str, potential_fields: dict)
Generates a dependency graph of classes and their relationships, saved as a PDF file.
Directory Structure
project_directory/
|-- your_script_name.py # Main script
|-- drugbank_partial.xml # Example XML file
|-- generated_code/ # Output directory
|-- base_model.py # Base model class
|-- generated_main.py # Main script to initialize instances
|-- *.py # Generated class files
|-- class_dependencies.pdf # Dependency graph
Customization
You can customize the behavior of the script by modifying:
- BaseModel Class: Extend the base model functionality in
base_model.py. - Dependency Graph: Adjust graph attributes in the
generate_dependency_graphfunction.
Error Handling
If an error occurs, the script prints a detailed error message. Ensure the input XML file is well-formed and adheres to XML standards.
License
This project is licensed under the MIT License. Feel free to use and modify it as needed.
Happy coding!
Metadata
Release files for xml-to-python-generator 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| xml_to_python_generator-1.0.1.tar.gz | 6.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| xml_to_python_generator-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.3 kB
Release files / xml_to_python_generator-1.0.1.tar.gz
| Download URL | xml_to_python_generator-1.0.1.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
46aa41b6441fa0ec48ddafa1b6d4bbdd9eef1ea13927707764cf96e29e2d4503
|
|
BLAKE2b-256 checksum How to use checksums |
ba65d04b9f5a5b6ab01089b9d5edd613138baf5fe27b1eee798d9bf4807b4ab6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Release files / xml_to_python_generator-1.0.1-py3-none-any.whl
| Download URL | xml_to_python_generator-1.0.1-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
52ac33a71a3cdcc4ce278255082caf7f73ac2e8ae848a96e78219d349dcc65da
|
|
BLAKE2b-256 checksum How to use checksums |
584b326ba3be56193b38b843e87d1c0e1e0502bdefe6b0b8b57d74bc04609238
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|