class SeaIceConnector – interface for PostgreSQL DB

This is the interface for the SeaIce database. It is assumed that a PostgreSQL database has already been configured. The following queries are implemented:

  • Create the DB schema SI and the various table and triggers (createSchema()).
  • Drop SI , tables, and triggers (dropSchema()).
  • Insert, remove, and update terms, users, and comments.
  • Cast vote and track terms.
  • Calculate term consensus and stability.
  • Inset and delete notifications.
  • Import/export the DB.
class seaice.SeaIceConnector.SeaIceConnector(user=None, password=None, db=None)[source]

Connection to the PostgreSQL database.

This object is capable of either connecting to a local database (specified by the parameters) or a remote database specified by the environment variable DATABASE_URL if no paramters are given. This is to support Heroku functionality. For local testing with your Heroku-Postgres based database, do

export DATABAE_URL=$(heroku config:get DATABASE_URL) && ./ice.py --config=heroku

Parameters:
  • user (str) – Name of DB role.
  • password (str) – User’s password.
  • db (str) – Name of database.
Export(table, outf=None)[source]
Export database in JSON format to outf. If no file name
provided, dump to standard out.
Parameters:
  • table (str) – Name of table.
  • outf (str) – Filename to output table to.
Import(table, inf=None)[source]

Import database from JSON formated inf.

Parameters:
  • table (str) – Name of table.
  • inf (str) – File name from which to import.
castVote(user_id, term_id, vote)[source]

Cast or change a user’s vote on a term. Return the term’s new consensus score.

Parameters:
  • user_id (int) – User ID.
  • term_id (int) – Term Id.
  • vote (int) – +1, 0, or -1.
Return type:

float

checkTermConsistency(term_id)[source]
Check that a term’s consensus score is consistent by scoring it the hard way.
Update if it wasn’t.
Return type:bool
checkTracking(user_id, term_id)[source]

Check tracking.

Return type:bool
classifyTerm(term_id)[source]
Check if term is stable. If so, classify it as being canonical, vernacular,
or deprecated. Update term and return class as string.
Returns:‘canonical’, ‘vernacular’, or ‘deprecated’.
Return type:class
commit()[source]

Commit changes to database made while the connection was open. This should be called before the class destructor is called in order to save changes. It should be called freuqently in a mult-threaded environment.

con = None

The PostgreSQL database connector provided by the psycopg2 package.

createSchema()[source]

Create the SI schema for SeaIce with the tables SI.Users, SI.Terms, SI.Comments, SI.Tracking (vote and star), and various triggers; create SI_Notify with SI_Notify.Notify.

dropSchema()[source]

Drop SI and SI_Notify schemas.

getAllNotifications()[source]

Return an iterator over SI_Notify.Notify.

Return type:dict iterator
getAllTerms(sortBy=None)[source]

Return an iterator over SI.Terms.

Parameters:sortBy (str) – Column by which sort the results in ascending order.
Return type:dict iterator
getAllUsers()[source]

Return an iterator over SI.Users.

Return type:dict iterator
getByTerm(term_string)[source]

Search table by term string and return an iterator over the matches.

Parameters:term_string (str) – The exact term string (case sensitive).
Return type:dict or None
getComment(id)[source]

Get comment by ID.

Parameters:id (int) – Comment ID.
Return type:dict or None
getCommentHistory(term_id)[source]

Return a term’s comment history, ordered by creation date.

Parameters:term_id (int) – Term ID.
Return type:dict iterator
getTerm(id)[source]

Get term by ID.

Parameters:id (int) – Term ID.
Return type:dict or None
getTermByConceptId(concept_id)[source]

Get term by Concept Id.

Parameters:concept_id (str) – Concept Id.
Return type:dict or None
getTermConceptId(id)[source]

Get term string by ID.

Parameters:id (int) – Term ID.
Return type:str or None
getTermStats()[source]
Return the various parameters used to calculate consensus
and stability for each term.
Return type:dict iterator
getTermString(id)[source]

Get term string by ID.

Parameters:id (int) – Term ID.
Return type:str or None
getTermStringByConceptId(concept_id)[source]

Get term string by concept ID.

Parameters:id (int) – Term concept ID.
Return type:str or None
getTermsByTracking(user_id)[source]
Return an iterator over terms tracked by a user (terms that the
user has starred).
Parameters:user_id (int) – ID of the user.
Return type:dict iterator
getTermsByUser(user_id)[source]

Return an iterator over terms owned by a user.

Parameters:user_id (int) – ID of the user.
Return type:dict iterator
getTime()[source]
Get T_now timestamp according to database. This is important when
the SeaIce database is deployed to some anonymous server farm.
Return type:datetime.datetime
getTrackingByTerm(term_id)[source]

Return an iterator over users tracking a term.

Parameters:term_id – ID of term.
Returns:User rows.
Return type:dict iterator
getUser(id)[source]

Get User by ID.

Parameters:id (int) – User ID.
Return type:dict or None
getUserByAuth(authority, auth_id)[source]
Get user identified by an authentication ID. It’s assumed that this ID
is unique in the context of a particular authority, such as Google.

TODO: originally I planned to use (authority, auth_id) as the unique constraint on the SI.Users table. My guess is that most, if not all services have an associated email address. The unique constraint is actually the user’s email. This method should be replaced.

Parameters:
  • authority (str) – Organization providing authentication.
  • auth_id (str) – Authentication ID.
Returns:

Internal surrogate ID of user.

Return type:

int or None

getUserNameById(id, full=False)[source]

Get username by ID.

Parameters:
  • id (int) – User ID.
  • full (bool) – Get full name
Returns:

If full is set, then return the first and last name of the user. Otherwise, just return the first name.

Return type:

str or None

getUserNotifications(user_id)[source]

Return an iterator over SI_Notify.Notify.

Return type:dict iterator
getVote(user_id, term_id)[source]

Get user’s vote for a term.

Parameters:
  • user_id (int) – User ID.
  • term_id (int) – Term ID.
Returns:

+1, 0, or -1.

Return type:

int or None

insertComment(comment)[source]

Insert a new comment into the database and return ID.

Parameters:comment (dict) – New comment as dictionary. Default values will be used for ommitted columns.
Return type:int
insertNotification(user_id, notif)[source]

Insert a notification.

Parameters:notif (seaice.notify.BaseNotification) – Notification.
insertTerm(term)[source]

Add a term to the database and return the term’s ID.

Parameters:term (dict) – Term row to be inserted. Default values will be used for omitted columns.
Returns:ID of inserted row. If term[‘id’] isn’t assigned, the next available ID in the sequence is given.
Return type:int or None
insertTracking(tracking)[source]

Insert a tracking row, skipping if (term_id, user_id) pair exists.

Parameters:tracking (dictionary) – Expect dictionary with keys ‘user_id’, ‘term_id’, and ‘vote’.
Returns:(term_id, user_id)
Return type:(int, int) or None
insertUser(user)[source]

Insert a new user into the table and return the new ID.

Parameters:user (dict) – Default values are used for any omitted columns.
Return type:int or None
postScore(term_id, U, D)[source]
Postscore term. Input the reputations of up voters and down voters and
compute the consensus score. Update the term row as a side-affect.
Parameters:
  • U (int –> int) – Up voters.
  • D (int –> int) – Down voters.
preScore(term_id)[source]
Prescore term. Returns a tuple of dictionaries (User.Id -> User.Reputation)
of up voters and down voters (U, D).
Parameters:term_id (int) – Term ID.
Returns:(U, D)
Return type:((int –> int), (int –> int))
removeComment(id)[source]

Remove comment and return ID.

Parameters:id (int) – Comment ID.
Return type:int or None
removeNotification(user_id, notif)[source]

Remove a notification.

Parameters:notif (seaice.notify.BaseNotification) – Notification.
removeTerm(id)[source]

Remove term row from the database.

Parameters:id (int) – Term Id.
Returns:ID of removed term.
Return type:int or None
search(string)[source]
Search table by term_string, definition and examples. Rank
results by relevance to query, consensus, and classificaiton.
Parameters:string (str) – Search query.
Return type:dict list
trackTerm(user_id, term_id)[source]
User has starred a term. Return True if a row was
inserted into the Tracking table, False if not.
Return type:bool
untrackTerm(user_id, term_id)[source]

Untrack term.

updateComment(id, comment)[source]

Update term comment. Note that user authentication is handled up stream!

Parameters:
  • id (dict) – Comment ID.
  • comment – Expects at least the key ‘comment_string’ with string value.
updateTerm(id, term)[source]
Modify a term’s term string, deifnition and examples.
Note: term ownership authenticated upstream!
Parameters:
  • id (int) – Term ID.
  • term (dict) – Dictionary containing atleast the keys ‘term_string’, ‘definition’, and ‘examples’ with string values.
updateUser(id, first, last, enotify)[source]

Update user’s name.

Parameters:
  • id (int) – User ID.
  • first (str) – First name.
  • last (str) – Last name.
updateUserReputation(id, rep)[source]
Set reputation of user. This triggers an update of the consensus score
and term stability. Commit updates immediately.
Parameters:
  • id (int) – User ID.
  • rep (int) – New reputation score.

class ScopedSeaIceConnector

digraph inheritancecd7b16f004 { rankdir=LR; size="8.0, 12.0"; "seaice.ConnectorPool.ScopedSeaIceConnector" [style="setlinewidth(0.5)",URL="#seaice.ConnectorPool.ScopedSeaIceConnector",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="A SeaIce DB Connector which is released to the pool it from whence it ",height=0.25,shape=box,fontsize=10]; "seaice.SeaIceConnector.SeaIceConnector" -> "seaice.ConnectorPool.ScopedSeaIceConnector" [arrowsize=0.5,style="setlinewidth(0.5)"]; "seaice.SeaIceConnector.SeaIceConnector" [style="setlinewidth(0.5)",URL="#seaice.SeaIceConnector.SeaIceConnector",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="Connection to the PostgreSQL database. ",height=0.25,shape=box,fontsize=10]; }

class seaice.ConnectorPool.ScopedSeaIceConnector(pool, db_con)[source]

Bases: seaice.SeaIceConnector.SeaIceConnector

A SeaIce DB Connector which is released to the pool it from whence it came when it goes out of scope. This type of connector is produced by seaice.ConnectorPool.SeaIceConnectorPool.getScoped() and should not be used directly.

Parameters:

Table Of Contents

Previous topic

The seaice package

Next topic

class ConnectorPool

This Page