Skip to main content

TonyDBC

Latest version: 1.3.13

Available on PyPI: https://pypi.org/project/tonydbc/

2024-01-07: Release announcement on Medium

Supports high-level database operations within Python. TonyDBC is short for Tony’s Database Connector, named for Maria’s lover in West Side Story.

TonyDBC uses the MariaDB/Connector but adds these features:

  • Support for the object-oriented context manager design pattern, which automatically closes connections.
  • A way to convert between pandas DataFrames and MariaDB tables, including serialization of complex data types like dictionaries and numpy arrays.
  • A way to autoresume when the connection is lost.
  • Support for fast INSERT and UPDATE from DataFrame.to_sql. Currently pandas’ implemention using sqlalchemy is 300x slower.
  • A way to quickly and automatically clone a database from a DDL script and copy the contents from the production database.
  • Batch INSERT and UPDATE offline and resume when Internet is available.
  • Logging database actions using the standard logging module.
  • In a CI/CD context, automatically connecting to the correct production or test database.
  • Support for the np.Int64Dtype data type.
  • Support for pandas timestamps.
  • Able to bulk insert data containing NULLs

Installation

To install TonyDBC from PyPI on Windows:

pip install tonydbc

To install TonyDBC on Ubuntu or Debian, first the MariaDB Connector / C must be installed, then the MariaDB Connector / Python must be installed, because neither can be installed via pip3:

cd ~

sudo apt-get install -y wget curl python3-packaging gcc

# Install mariadb CS Package Repository so apt-get can find the right package
# See https://mariadb.com/docs/skysql/connect/programming-languages/c/install/#Installation_via_Package_Repository_(Linux)
wget https://r.mariadb.com/downloads/mariadb_repo_setup || wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
chmod +x mariadb_repo_setup
sudo ./mariadb_repo_setup  --mariadb-server-version="mariadb-11.1.5"
sudo apt-get install -y libmariadb3 libmariadb-dev

pip3 install tonydbc

Usage

A typical use case:

from tonydbc import TonyDBC, load_dotenvs
import pandas as pd

load_dotenvs()

sql_params = {
    "host": os.environ["MYSQL_HOST"],
    "user": os.environ["MYSQL_USER"],
    "password": os.environ["MYSQL_PASSWORD"],
    "database": os.environ["MYSQL_DATABASE"],
    "port": int(os.environ["MYSQL_PORT"]),
}

with TonyDBC(**sql_params) as db:
    # Do stuff
    contoso_users_df = db.query_table("user", f"""
        SELECT *
        FROM `user`
        WHERE
            `user`.`company` = 'Contoso'
        LIMIT 10;
        """)

    print(contoso_users_df.first_name)

Timezone Handling in TonyDBC

Internally, MariaDB stores timestamps in UTC and its insert and select return timestamps in the session timezone for that database connection session.

db = TonyDBC() follows the same convention as MariaDB, and tracks timezone as db.session_timezone.

This db.session_timezone is set initially by:

  1. In the db = TonyDBC(session_timezone="Asia/Bangkok") initializer via the parameter session_timezone, which defaults to None.
  2. If None is specified, it will be taken from the DEFAULT_TIMEZONE environment variable; e.g. DEFAULT_TIMEZONE=Asia/Bangkok.

Note: If NEITHER of the above is specified, an AssertionError will be raised during initialization.

  1. Later, you can call db.set_timezone("Asia/Bangkok") to set the timezone to something else.

As with MariaDB commands, all insert and select statements in TonyDBC will return timestamps in the session timezone.

''Note that TonyDBC will warn if the session timezone is not the same as the system timezone.''

Metadata

Release files for tonydbc 1.3.13

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

Source distribution (sdist)

Source distribution for tonydbc 1.3.13
File Size Uploaded
tonydbc-1.3.13.tar.gz 64.6 kB Details

Built distribution (wheel)

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

Total release size: 108.2 kB

Release files / tonydbc-1.3.13.tar.gz

Download URL tonydbc-1.3.13.tar.gz
Size 64.6 kB
Tags Source
SHA-256 checksum
How to use checksums
96140c36746fd75493674007b45f58f50618fc56073e44f0cae671332477bf1e
BLAKE2b-256 checksum
How to use checksums
63b17c3afdebe99987df8497b4946b8f7a29b6fafc9508d9a61d8b375031d27b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Release files / tonydbc-1.3.13-py3-none-any.whl

Download URL tonydbc-1.3.13-py3-none-any.whl
Size 43.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
275401d847c4e8f4ca3da12b75b50575d9f42ae4e22657a5075e820ba60f8c70
BLAKE2b-256 checksum
How to use checksums
6cc9a90b798ff118287aaee509daba38e1ae2426f7fa6b0d785a43f4da708a4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

1.3.13 This release

2 release files

1.3.12

2 release files

1.3.9

2 release files

1.3.6

2 release files

1.3.5

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.2.30

2 release files

1.2.29

2 release files

1.2.28

2 release files

1.2.27

2 release files

1.2.26

2 release files

1.2.23

2 release files

1.2.21

2 release files

1.2.20

2 release files

1.2.19

2 release files

1.2.18

2 release files

1.2.16

2 release files

1.2.14

2 release files

1.2.12

2 release files

1.2.11

2 release files

1.2.10

2 release files

1.2.9

2 release files

1.2.8

2 release files

1.2.7

2 release files

1.2.6

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.14

2 release files

1.0.13

2 release files

1.0.12

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

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