Skip to content

Commit

Permalink
A trivial bit of error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
urschrei committed Aug 4, 2010
1 parent 2d18a40 commit 8a28d9a
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions bookbyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
import sys
import os
import sqlite3
import re
import tweepy
sys.path.append("/Users/sth/scripts")

# tweepy stuff
auth=tweepy.BasicAuthHandler('robo_dante', 'beatrice')
api=tweepy.API(auth)


# create a SQLite connection, or create a new db and table
connection=sqlite3.connect('dc.db')
cursor=connection.cursor()
try:
Expand All @@ -28,6 +27,11 @@
cursor.execute('INSERT INTO position VALUES (null, ?, ?, ?)',(lastline, "1", 0))
# set up a new blank table, and start off at line 0
# retry logic goes here
try:
cursor.execute('SELECT * FROM position ORDER BY POSITION DESC LIMIT 1')
except sqlite3.OperationalError:
print "Still couldn't execute the SQL query, even though I created a new table. Giving up."
sys.exit()

# get the highest page number, and the line display offset
row=cursor.fetchone()
Expand All @@ -41,12 +45,11 @@

def format_tweet(input_string,next_string):
""" Properly format an input string based on whether it's a header line, or a poetry line
accepts 3 inputs: the current line from a book, the following line, and the current line display
off_set, which is incremented each time a header line is encountered, and subtracted from the
"real" line number, in order to display the correct line number.
returns a ready-to-tweet string, either a canto, or a poetry line. """

# If line is a new Canto, append the following line to it, instead of "l. "
accepts 2 inputs: the current line from a book, and the following line. If the current line
begins with "CANTO", it's a header, so instead of displaying a line number, we join the next line
and increment both the line number and the line offset by 1. This means the line numbers
don't jump, when a header is encountered, as the offset is subtracted from the display line.
Returns a ready-to-tweet string, either a canto, or a poetry line. """
#pattern='^CANTO'
#if re.search(pattern, input_string):
if input_string.startswith("CANTO"):
Expand Down

0 comments on commit 8a28d9a

Please sign in to comment.