Skip to main content

zackdbtool

a python package to connect db and data sources like google sheets

Set up

make a json file that contains the database credentials.

see example: dbcredentials_example.json (memory and filedb are for sqlite quick usage, you can add more like test1)

{
    "test1":{
        "conn_engine":"mysql+pymysql",
        "host":"192.168.0.12",
        "port":3306,
        "user":"test_user",
        "passwd":"********",
        "db":"public",
        "testtable":"user"
    },
    "memory":{
        "conn_engine":"sqlite",
        "host":"/:memory:",
        "port":"",
        "user":"",
        "passwd":"",
        "db":"test",
        "testtable":""
    },
    "filedb":{
        "conn_engine":"sqlite",
        "host":"/filedb.db",
        "port":"",
        "user":"",
        "passwd":"",
        "db":"test",
        "testtable":""
    }
}

save the file to a location, e.g. $HOME/.credentials/dbcredentials_example.json

save your google service account json file to a location e.g. $HOME/.credentials/service_account.json

add two environment variable for those two files:

DB_CREDENTIALS_PATH=$HOME/.credentials/dbcredentials_example.json
SERVICE_ACCOUNT_JSON_PATH=$HOME/.credentials/service_account.json

in linux/mac you can (NOTE: in vscode jupyter, it doesn't read the .bashrc, you can either add a .env or use the same file path below)

vi $HOME/.bashrc

inseart two rows to the end of the file and restart the terminal

export DB_CREDENTIALS_PATH=$HOME/.credentials/dbcredentials_example.json
export SERVICE_ACCOUNT_JSON_PATH=$HOME/.credentials/service_account.json

if you run your app as a systemd service:

vi /etc/systemd/system/YOURSERVICENAME.service

add environment variable

[Service]
Environment=DB_CREDENTIALS_PATH=$HOME/.credentials/dbcredentials.json
Environment=SERVICE_ACCOUNT_JSON_PATH=$HOME/.credentials/ga-service-account.json

then reload daemon

systemctl daemon-reload

in windows, use the search bar to search "environment variable" add the two variables to user/system variables

if you want to build app in docker

docker run -e DB_CREDENTIALS_PATH=dbcredentials.json -e SERVICE_ACCOUNT_JSON_PATH=ga-service-account.json dockerimagename

download the repository, and

pip install .

or through pipy

pip install zackdbtool

Connect a database

from zackdbtool import db_engine
import pandas as pd
dbsource= 'mydb' 
engine = db_engine(dbsource, db='test')
df = pd.read_sql(f'SELECT * FROM user limit 10', engine)
print(df)

if you don't have a database running you can use memory or filedb to create a testing database without any setup

from zackdbtool import db_engine
import pandas as pd
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import sessionmaker

dbsource= 'memory' 
engine = db_engine(dbsource)
Base = declarative_base()
sesson = sessionmaker(bind=engine)

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    name = Column(String)
    fullname = Column(String)

Base.metadata.create_all(engine)

ed_user = User(name="ed", fullname="Ed Jones")
session.add(ed_user)
session.commit()

with engine.connect() as c:
    data = c.execute('select * from users').fetch_all()
    print(data)

use google services

view_id = 'your site view id'
metrics = ['ga:users','ga:newUsers','ga:pageViews','ga:sessions']
# find more setting at https://ga-dev-tools.web.app/query-explorer/
dimensions = ['ga:month', 'ga:year']
start_date = '2000-01-01'
df = gareports(view_id, metrics, dimensions, start_date=start_date)

google sheet

  SAMPLE_SPREADSHEET_ID_PV = 'your google sheet id'
  SAMPLE_RANGE_NAME_PV = 'your sheet name'
  dedupCol = 'idcolname' 
  skiprows = 0
  dfpv = readgooglesheets(SAMPLE_SPREADSHEET_ID_PV, SAMPLE_RANGE_NAME_PV,dedupCol = dedupCol, skiprows = skiprows)
  print(dfpv.head())

Metadata

Release files for zackdbtools 0.1.14

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

Source distribution (sdist)

Source distribution for zackdbtools 0.1.14
File Size Uploaded Provenance Actions
zackdbtools-0.1.14.tar.gz 10.1 kB Without provenance Details for zackdbtools-0.1.14.tar.gz

Release files / zackdbtools-0.1.14.tar.gz

Download URL zackdbtools-0.1.14.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d8f64928751a5d1b2edad1fbc637d68f5febc8c6c0d650bab48f1980eb671ead
BLAKE2b-256 checksum
How to use checksums
ff04c867118247b045ae2fa356edd029b6bb6edfa656898143bbfbcda031198b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Security for zackdbtools 0.1.14

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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