The db module

The db module contains the DBConnection class and the Profiles database table

The Database Table

In the Profiles database table each row corresponds to a unique Profile

The table only has two fields per row:

How is profile data saved to the database?

When a Profile calls save() and has local = False, it will connect() to the database specified by the DABATASE_URL environment variable and use it to query_profile() settings

Important!!

You MUST configure the DATABASE_URL environment variable to save/load remotely

  • InstaTweet uses SQLAlchemy to create a DBConnection – any db it supports is compatible

  • See the db module for more information

One Last Thing!

The DBConnection is meant to be used as a context manager

with DBConnection() as db:
    # Do Something
  • A SESSION is created/destroyed when saving, loading, and InstaTweeting a Profile

If you don’t want that, here’s instructions on Persisting The DBConnection

…

InstaTweet.db.DATABASE_URL

The Database URL to use, obtained from the DATABASE_URL environment variable

…

class InstaTweet.db.Profiles(**kwargs)[source]View on GitHub

Database table used for storing Profile settings

The table currently has only 2 fields, for the name and pickle bytes of the profile

name

The Profile name

config

The pickle bytes from Profile.to_pickle()

…

class InstaTweet.db.DBConnection[source]View on GitHub

Database Connection class with context management ooh wow

Uses SQLAlchemy to connect and interact with the database specified by DATABASE_URL environment variable

Sample Usage:

def poop_check():
    with DBConnection() as db:
        if db.query_profile(name="POOP").first():
            raise FileExistsError('DELETE THIS NEPHEW......')
SESSION

The currently active session; closed on object exit

Type

scoped_session

ENGINE

The engine for the currently set DATABASE_URL; reused after first connection

Type

Engine

static connect()[source]View on GitHub

Creates a scoped_session and assigns it to DBConnection.SESSION

query_profile(name)[source]View on GitHub

Queries the database for a Profile by its name

Parameters

name (str) – the profile name (ie. the Profile.name)

Returns

the Query NOT the Profile

Return type

Query

load_profile(name)[source]View on GitHub

Loads a profile from the database by name

Parameters

name (str) – the profile name (ie. the Profile.name)

Raises

LookupError – if the database has no profile saved with the specified name

Return type

Profile

save_profile(profile, alert=True)[source]View on GitHub

Saves a Profile to the database by either updating an existing row or inserting a new one

Parameters
  • profile (Profile) – the Profile to save

  • alert (bool) – if True, will print a message upon successfully saving

Return type

bool

delete_profile(name, alert=True)[source]View on GitHub

Deletes a Profile from the database by name

Parameters
  • name (str) – the profile name (ie. the Profile.name)

  • alert (bool) – if True, will print a message upon successfully deleting

Return type

bool