astrobase.lcdb module

Serves as a lightweight PostgreSQL DB interface for other modules in this project.

class astrobase.lcdb.LCDB(database=None, user=None, password=None, host=None)[source]

Bases: object

This is an object serving as an interface to a PostgreSQL DB.

LCDB’s main purpose is to avoid creating new postgres connections for each query; these are relatively expensive. Instead, we get new cursors when needed, and then pass these around as needed.

database

Name of the database to connect to.

Type:str
user

User name of the database server user.

Type:str
password

Password for the database server user.

Type:str
host

Database hostname or IP address to connect to.

Type:str
connection

The underlying connection to the database.

Type:psycopg2.Connection object
cursors

The keys of this dict are random hash strings, the values of this dict are the actual Cursor objects.

Type:dict of psycopg2.Cursor objects
open(database, user, password, host)[source]

This opens a new database connection.

Parameters:
  • database (str) – Name of the database to connect to.
  • user (str) – User name of the database server user.
  • password (str) – Password for the database server user.
  • host (str) – Database hostname or IP address to connect to.
open_default()[source]

This opens the database connection using the default database parameters given in the ~/.astrobase/astrobase.conf file.

autocommit()[source]

This sets the database connection to autocommit. Must be called before any cursors have been instantiated.

cursor(handle, dictcursor=False)[source]

This gets or creates a DB cursor for the current DB connection.

Parameters:
  • handle (str) – The name of the cursor to look up in the existing list or if it doesn’t exist, the name to be used for a new cursor to be returned.
  • dictcursor (bool) – If True, returns a cursor where each returned row can be addressed as a dictionary by column name.
Returns:

Return type:

psycopg2.Cursor instance

newcursor(dictcursor=False)[source]

This creates a DB cursor for the current DB connection using a randomly generated handle. Returns a tuple with cursor and handle.

Parameters:dictcursor (bool) – If True, returns a cursor where each returned row can be addressed as a dictionary by column name.
Returns:The tuple is of the form (handle, psycopg2.Cursor instance).
Return type:tuple
commit()[source]

This just calls the connection’s commit method.

rollback()[source]

This just calls the connection’s commit method.

close_cursor(handle)[source]

Closes the cursor specified and removes it from the self.cursors dictionary.

close_connection()[source]

This closes all cursors currently in use, and then closes the DB connection.