Skip to main content

A Python tool for generating Python classes and scripts from XML files

Project description

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:

  1. Define classes based on the XML structure.
  2. Generate instances for each XML element.
  3. Export instances to CSV files.
  4. 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:

  • os
  • xml.etree.ElementTree
  • uuid
  • collections
  • keyword
  • pandas
  • graphviz

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

  1. Place your XML file (e.g., drugbank_partial.xml) in the project directory.
  2. 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:

  1. Class Files: Individual Python files for each class.
  2. Main Script: A generated_main.py file to initialize instances and export them to CSV.
  3. Dependency Graph: A class_dependencies.pdf file 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_graph function.

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!

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

xml_to_python_generator-1.0.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xml_to_python_generator-1.0.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file xml_to_python_generator-1.0.0.tar.gz.

File metadata

  • Download URL: xml_to_python_generator-1.0.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for xml_to_python_generator-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6e8d2fd4591fd5738d38130935749a5bcde6deb3ae06885c3010b6ac069cf486
MD5 649e1bd947664c229d61dbd1ed1767d8
BLAKE2b-256 9ee3588a02141c6c1eb3efc625d56e4b4cd1c69756f0c116bade7718db7ab7cb

See more details on using hashes here.

File details

Details for the file xml_to_python_generator-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for xml_to_python_generator-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 57236ab5c92d24d69f3fd284834881f534ff713fa02623c284906e3e3bc4253f
MD5 7788f74531c34d09a06ef091b33c11a6
BLAKE2b-256 a776d324338579671f0a636dc6640df498a56d418532684adc47321459b8ea40

See more details on using hashes here.

Supported by

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