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:
name
: primary key for lookups/insertionsconfig
: stores the Profile as pickle bytes viato_pickle()
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
If the
profile_exists()
in the database already, itsconfig
data will be updatedOtherwise, the
DBConnection
willsave_profile()
data in a new table row
Important!!
You MUST configure the DATABASE_URL
environment variable to save/load remotely
InstaTweet uses
SQLAlchemy
to create aDBConnection
β any db it supports is compatibleSee 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 aProfile
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
settingsThe table currently has only 2 fields, for the
name
and pickle bytes of the profile- 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 byDATABASE_URL
environment variableSample 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
- ENGINEο
The engine for the currently set
DATABASE_URL
; reused after first connection- Type
- static connect()[source]View on GitHubο
Creates a
scoped_session
and assigns it toDBConnection.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
- Return type
- 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
- 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
- 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