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, itsconfigdata will be updatedOtherwise, the
DBConnectionwillsave_profile()data in a new table row
Important!!
You MUST configure the DATABASE_URL environment variable to save/load remotely
InstaTweet uses
SQLAlchemyto create aDBConnectionβ any db it supports is compatibleSee the
dbmodule for more information
One Last Thing!
The DBConnection is meant to be used as a context manager
with DBConnection() as db:
# Do Something
A
SESSIONis 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_URLenvironment variable
β¦
- class InstaTweet.db.Profiles(**kwargs)[source]View on GitHubο
Database table used for storing
ProfilesettingsThe table currently has only 2 fields, for the
nameand 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
SQLAlchemyto connect and interact with the database specified byDATABASE_URLenvironment 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_sessionand assigns it toDBConnection.SESSION
- query_profile(name)[source]View on GitHubο
Queries the database for a
Profileby 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
Profileto the database by either updating an existing row or inserting a new one
- delete_profile(name, alert=True)[source]View on GitHubο
Deletes a
Profilefrom 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