Skip to main content

Alchemyrohan

pypi v22|v23 Python 3.10|3.11|3.12 SqlAlchemy

Alchemyrohan is a helpful tool for creating SqlAlchemy models based on the database schema.


📖 Content


🔧 How to Install

With pip package installer from PyPI directly:

pip install alchemyrohan

or from source:

pip install pdm
git clone https://github.com/wamberger/alchemyrohan.git
pdm install

🗄 Database Support

This project is currently designed to work with the following databases:

  • SqLite
  • Oracle

🔨 How to use

⌨️ In Command-Line

You can execute the tool by entering the command along with its arguments:

usage: arohan [-h] -c CONN_STR [-p PATH] [-y PY_PATH] -m MODELS

options:
  -h, --help            show the help message and exit
  -c CONN_STR, --conn_str CONN_STR
                        Connection string to connect with the database - Required argument. 
                        Accepts a string.
  -p PATH, --path PATH  Path where to save the models - Optional argument. 
                        Defaults to the current working directory. Accepts a string.
  -y PY_PATH, --py_path PY_PATH
                        Pythonic path to models in the project. Optional argument. 
                        Defaults to 'py_path'. Accepts a string.
  -m MODELS, --models MODELS
                        Names of the database tables. Required argument. Add every table with -m=

Example:

arohan -c=sqlite:////path/to/your/database/test.db  -y=py.path.to.models -p=path/to/models/ -m=table1 -m=table2 -m=tables3

💻 As Script

You can find an example script in the directory help_file/.

🪄 Functions

🔮 Main Function

assemble_models() is the main function. This function is used to create a SqlAlchemy database model and is accepting the following arguments:

argument description
conn_str Credential string to connect to the database
table_names Names of the tables
path Absolute path to the location where the created models will be saved
py_path pythonic path to the models

Simple example how to use the function assemble_models():

import os

from sqlalchemy.exc import SQLAlchemyError
import alchemyrohan as ar


def main() -> None:

    path = os.path.dirname(__file__)

    # Sqlite example
    conn_str = f"sqlite:///{path}{os.sep}test_sqlite{os.sep}test.db"
    # Oracle-Database example
    # db_creds = f'oracle+oracledb://{username}:{password}@{hostname}:{port}/{service_name}'

    path = os.path.join(path, 'test_models') # path to save models
    py_path = 'tests.test_models' # pythonic path to models

    table_names = ['parent', 'child'] # all names will be capitilized

    try:
        ar.assemble_models(conn_str, table_names, path, py_path)
    except SQLAlchemyError as e:
        raise SQLAlchemyError(e) from e


if __name__ == '__main__':
    main()
💉 Optional Function

get_model(): It retrieves the desired database object of the SqlAlchemy model. It requires the table_name and py_path_to_model arguments.

import alchemyrohan as ar

model = ar.get_model('child', 'py.path.to.model')

🗂 Models

Created SqlAlchemy models have some additional features:

  • Default values.
  • Parent-child relationships.
  • When 'printing', the string will contain the model/object name and attribute names with their values.

All models are named with the same convention as they are in the database, with one difference: they are capitalized according to Python class naming conventions.

Example of one created model:

from sqlalchemy import Column
from tests.test_models import Base
from sqlalchemy import ForeignKey
from sqlalchemy.dialects.sqlite import INTEGER
from sqlalchemy.dialects.sqlite import TEXT
from sqlalchemy.orm import relationship


class Child(Base):
    __tablename__ = 'child'


    id = Column(INTEGER, primary_key=True)
    parent_id = Column(INTEGER, ForeignKey('parent.id'), nullable=True, default=None)
    name = Column(TEXT, nullable=True, default=None)
    grade = Column(INTEGER, nullable=True, default=None)


    parent_Parent = relationship("Parent", back_populates="children_Child", lazy="joined")
    
    def __str__(self):

        return (f'User(id={self.id},'
			f'parent_id={self.parent_id},'
			f'name={self.name},'
			f'grade={self.grade})')

📚 Dependencies

  • sqlalchemy (version 2.x.x) is an ORM and provides code for its models.
  • oracledb (version 2.x.x) is used to shape a database table model with an Oracle table schema.

❗Important Note

You should always check the code and correct it manually if necessary. This may be necessary in cases when:

  • You add the wrong path.

  • You are creating only one model which has relationships to other tables, thus you will need to create those models or delete the relevant part of the code.

  • Your tables have no primary keys. SQLAlchemy requires at least one primary key.

  • Your database may have some data types or features which have not yet been tested - Please report!

📋 Release Notes

  • v0.5.0:

    • code refactor.
    • The 'validate' method has been removed from the created models and is no longer available.
    • Updated README.md.
  • v0.4.1 - Some vital changes! Please look at CHANGELOG.md. In short:

    • New command-line: arohan.
    • Removed functions: is_model(), reload_module(), is_module().
    • Changing parameter names in the function assemble_models().
    • Bug fixing (relationship).
    • In models, method validate.
  • v0.4.0 - Main update! Not compatible with previous versions. Changes:

    • Changed function name from assemble_model() to assemble_models().
    • Updated parameters of the assemble_models() function.
    • Revised code structure of the assemble_models() function.
    • Adjusted naming or added text in utils.py, wizard.py and __init__py.
    • Updated README.md file.
  • v0.3.2 - text fixing and adding third party licenses

  • v0.3.1 - bug fixing

  • v0.3.0 - added additional functions

  • v0.2.0 - tested with Oracle database

  • v0.1.0 - creation of the initial code and tested with SqLite database

📄 License and Third-Party Licenses

Alchemyrohan is MIT licensed, as stated in the LICENSE file.

The following software components are included in this project:

  • SqlAlchemy (MIT License)
  • python-oracledb (Apache License 2.0)

THIRD PARTY LICENSES

Metadata

Release files for alchemyrohan 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for alchemyrohan 0.5.0
File Size Uploaded Provenance
alchemyrohan-0.5.0.tar.gz 1.5 MB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for alchemyrohan 0.5.0
File Interpreter ABI Platform
alchemyrohan-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.6 MB

Release files / alchemyrohan-0.5.0.tar.gz

Download URL alchemyrohan-0.5.0.tar.gz
Size 1.5 MB
Tags Source
SHA-256 checksum
How to use checksums
cef061050dcf5c71f62a7cb2ebeea4614e9d6af5c723b75ba956c68828917917
BLAKE2b-256 checksum
How to use checksums
a9e16e50ad952b5cf3b42fed9ebe454809fcb9f0ea2824ee9c5d4bab98445337
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / alchemyrohan-0.5.0-py3-none-any.whl

Download URL alchemyrohan-0.5.0-py3-none-any.whl
Size 13.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac589bad311553a668af93dcedf2f3a3dec7d00c7744796c475729ed7993319b
BLAKE2b-256 checksum
How to use checksums
94f0b83dab2d508a931c50641ff79d0f275076107ed4aabf2136110e5220d51f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Security for alchemyrohan 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page