Skip to main content

bellman_tools

Project description

Bellman Tools (bellman_tools)

Utilities for reading from and writing to Microsoft SQL Server using SQLAlchemy and pandas.

Supports Python 3.10+ and SQLAlchemy 2.x.

Installation

pip install bellman-tools

Prerequisites

  • Microsoft ODBC Driver for SQL Server (e.g. "ODBC Driver 18 for SQL Server")
  • pyodbc installed (pulled in automatically)

Configure the connection

Set an environment variable named DATABASE_CONNECTION_STRING. The string must include a {db} placeholder that will be replaced by the database name you pass to Sql(db=...).

Example .env (place it in your current working directory):

DATABASE_CONNECTION_STRING="mssql+pyodbc://username:password@server-host:1433/{db}?driver=ODBC+Driver+18+for+SQL+Server&TrustServerCertificate=yes"

If your machine provides an older driver name, you can also use:

DATABASE_CONNECTION_STRING="mssql+pyodbc://username:password@server-host:1433/{db}?driver=SQL+Server"

Notes on environment loading

  • bellman_tools.sql_tools looks for a .env file only in your current working directory when it is imported. If not found, it prints the directory and continues.
  • Prefer setting environment variables via your shell or secrets manager. The .env support is a convenience fallback.

Quickstart

Query data

from bellman_tools import sql_tools

SQL = sql_tools.Sql(db="DB")
df = SQL.load_dataframe_from_query("SELECT TOP 1 * FROM Test")

Upload data

Define a SQLAlchemy model for your target table (one-time setup):

Tip: You can generate model boilerplate from an existing table:

print(UPLOAD.create_schema(table_name="YourTable"))

Then create a file in your project, e.g. database\Test.py:

from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Integer, String
from bellman_tools.database.db_template import db_template as DBTemplate

Base = declarative_base()

class Test(Base, DBTemplate):
    __tablename__ = "Test"
    __table_args__ = {"schema": "dbo"}
    ID = Column(Integer, primary_key=True)
    Test = Column(String)

Insert a DataFrame, optionally avoiding duplicates against existing rows:

import pandas as pd
from bellman_tools import sql_tools, upload_tools

SQL = sql_tools.Sql(db="SAM")
UPLOAD = upload_tools.Upload(SQL)

df = pd.DataFrame([{"Test": "Testing with Upload tools"}])

UPLOAD.load_basic_df_to_db(
    df_incoming=df,
    SQL_Alchemy_Table=Test,
    check_with_existing=True,  # optional: compare with existing rows before insert
)

DataFrame comparison utility

Use compare_df_with_existing_and_get_only_new_rows to filter only new rows by comparing two DataFrames on shared columns.

import pandas as pd
from bellman_tools import sql_tools

df_incoming = pd.DataFrame({"id": [1, 2], "value": ["a", "b"]})
df_existing = pd.DataFrame({"id": [1], "value": ["a"]})

df_diff = sql_tools.compare_df_with_existing_and_get_only_new_rows(
    df_incoming=df_incoming,
    df_existing=df_existing,
)
# df_diff contains the row id=2, value="b"

Key options:

  • ignored_columns: columns to exclude from the comparison. Defaults to ID, InsertedAt, InsertedBy, InsertedHost in upload workflows. You can pass your own list.
  • cast_to_existing_dtypes: when True, casts df_incoming to the dtypes of df_existing for compared columns to avoid false mismatches.

API at a glance

  • Sql

    • Sql(db, server="default", fast_executemany=True, ...)
    • load_dataframe_from_query(sql_query, replace_nan=True) -> pandas.DataFrame
    • execute_sql(sql_query) -> bool
  • Upload

    • Upload(sql: Sql)
    • load_basic_df_to_db(df_incoming, SQL_Alchemy_Table, mapping_columns_to_db=None, check_with_existing=False, str_existing_query=None, col_precision=None, cast_to_existing_dtypes=True, insert_via_pandas=False, insert_line_by_line=False, add_inserted_at=True, add_inserted_by=True, add_inserted_host=True, ...)
    • create_schema(table_name) -> str (returns Python class boilerplate for a table)

Troubleshooting

  • "No .env file found in current directory": Set DATABASE_CONNECTION_STRING in your environment or create a .env in your current working directory.
  • ODBC driver errors: Install the Microsoft ODBC Driver for SQL Server and ensure the driver= value in your connection string matches the installed driver name.
  • SQLAlchemy 2.x: This package is compatible with SQLAlchemy 2.x; queries use sqlalchemy.text under the hood.

Links

  • Source: https://github.com/davidbellman/bellman_tools

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

bellman_tools-0.1.5.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

bellman_tools-0.1.5-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file bellman_tools-0.1.5.tar.gz.

File metadata

  • Download URL: bellman_tools-0.1.5.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for bellman_tools-0.1.5.tar.gz
Algorithm Hash digest
SHA256 15521cf8fbc49dfa5905ee23582429353491359b2cb5125741e1886b802eb523
MD5 c9c0a8e6b47e49fb4c21fd39853d3802
BLAKE2b-256 6d8e1fc38cd657a86f7b8751dfabcf57d61535f2ad56a3cc17eaf91dccc9fe73

See more details on using hashes here.

File details

Details for the file bellman_tools-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: bellman_tools-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for bellman_tools-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 952f55c2c06e33689c3286bcea02eadd232265ff32bf9b9bd980fc545c0dea1d
MD5 36d3c99986ad23a50a5d73e3c5f265e3
BLAKE2b-256 ab63522465927e551a7b1f49c34da33436c0fbcb7dda9d2203705807204e9e63

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