Skip to main content

decorator for storing function results in a database

Project description

DB-ify

Decorate functions to write their outputs and given parameters to a MySQL database.

Installation / Set-up

First, navigate to the root folder of this repo, and activate the virtual environment you would like to install in. Then install dbify:

pip install -e .

In order to use dbify, you will need a MySQL server set up.

Usage

This library provides a decorator, dbify, that modifies the function it decorates to store its output to a MySQL database, along with each of the function's given parameters. The function must return a dictionary mapping column names to the values that will be stored in the database.

Note that MySQL database is limited in the types of objects it can store. Right now all return values and arguments to db-ified functions must be of type int, float, str, bool, or None.

Consider the following example:

from dbify import dbify

@dbify('test_database', 'test_table')
def example(a, b, c=0):
    return {
        'x': a + b + c,
        'y': a * b * c,
        'z': a > b
    }
    
example(2, 3, 2)

In this example we assume that there is a database named 'test_database'. Within this database, we will insert into a table called 'test_table', which will be created if it doesn't exist. We also assume that the user has set up a configuration file for connecting to their MySQL server (see more on this below). Executing this code will result in adding the following row to 'test_table':

id           | modified        | a | b | c | x | y  | z
-------------+-----------------+---+---+---+---+----+----------
<unique id>  | <date/time run> | 2 | 3 | 1 | 7 | 12 | 0 (False)

Connecting to a MySQL server

The dbify decorator uses an instance of dbify.connections.DbServer in order to connect to a MySQL server. A DbServer object keeps track of the host name, port, user credentials, etc., required to connect to the database server. There are two options for connecting: (1) directly by host name, and (2) via SSH tunneling. Use the former if your server is accessible over the internet or running on the same machine your code is running on; use the latter if you have a local MySQL server running on another machine that you would like to connect to via SSH.

To connect to the server using method (1), the example code above could read as follows:

from dbify import dbify
from dbify.connections import DbServer

@dbify(
    'test_database', 
    'test_table',
    db_server=DbServer(
        db_name='test_database',
        db_user='root',
        db_password='password',
        db_host='127.0.0.1',
        db_port=3306))
def example(a, b, c=0):
    return {
        'x': a + b + c,
        'y': a * b * c,
        'z': a > b
    }
    
example(2, 3, 2)

To connect to the server using method (2), the example code above could read as follows:

@dbify(
    'test_database', 
    'test_table',
    db_server=DbServer(
        db_name='test_database',
        db_user='root',
        db_password='password',
        db_host='127.0.0.1',
        db_port=3306,
        ssh_address='name-of-remote-server.com',
        ssh_user='user',
        ssh_keyfile='~/.ssh/keyfile',
        local_bind_host='0.0.0.0',
        local_bind_port=3307))
def example(a, b, c=0):
    return {
        'x': a + b + c,
        'y': a * b * c,
        'z': a > b
    }
    
example(2, 3, 2)

The easiest way to connect to a MySQL database server is using a configuration file. By creating a configuration file, you can avoid using an instance of DbServer directly. To create a configuration file, create a file called ~/.dbify_config, and include in it the parameters passed to the DbServer constructor above, e.g., to connect using method (1), the contents of ~/.dbify_config could be:

db_user = root
db_password = password
db_host = 127.0.0.1
db_port = 3306

Tip on viewing your databases

To view your databases on Mac OS, you can use sequel pro. If using a modern version of MySQL, you may need the nightly version.

brew cask install homebrew/cask-versions/sequel-pro-nightly

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

dbify-0.0.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

dbify-0.0.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file dbify-0.0.1.tar.gz.

File metadata

  • Download URL: dbify-0.0.1.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for dbify-0.0.1.tar.gz
Algorithm Hash digest
SHA256 fde66871b5e051aa47f5a6831a8a82334c428fa74e2ec1514d1e91b9c7a66911
MD5 ca90d811ec876318fb133e2108717c82
BLAKE2b-256 666640e767089385ef00144b5b6acc750fe08173d817b00fbbdcc2deccc781ea

See more details on using hashes here.

File details

Details for the file dbify-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: dbify-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for dbify-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b369baf49d0493159764ee97529830ab583e2671d49cac2cb030901fba26e7a8
MD5 087fa58ba8141b391e801a58331964db
BLAKE2b-256 9763188caec1e57908620acd807034361a7b5cb6bbc1de13728b5e71c1ac1545

See more details on using hashes here.

Supported by

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