Process In Data


In [25]:
import sqlite3
import time
import datetime

In [26]:
def processSinglePrice():
    # get the next input row to process
    conn = sqlite3.connect('in.db')
    c = conn.cursor()
    c.execute("SELECT min(iPriceId) FROM iPrice WHERE status = 'wait' ")
    row = c.fetchone()[0]
    
    if row == None:
        print 'No rows to input rows to process'
        return
        
    c.execute("UPDATE iPrice SET status = 'processing' WHERE iPriceID = %s" %row) #)) #, 'dPrice'))
    conn.commit()
    
    # select data to process
    c.execute("SELECT * FROM iPrice WHERE iPriceID = %s " %row )
    price_data = c.fetchone()
    print price_data
    iPriceId = price_data[0]
    iDate = price_data[1]
    status = price_data[2]
    date = price_data[3]
    symbol = price_data[4]
    bid = price_data[5]
    rate = price_data[6]
    ask = price_data[7]

    print ask
    
    # !! PROCESS DATA !!
    dconn = sqlite3.connect('data.db')
    dc = dconn.cursor()
    
    try:
        start = datetime.datetime.now()
        roll_ave = (rate + ask ) /2 # not really but will do for first example
        error = 'good'
        time.sleep(2.0/60.0)
        end = datetime.datetime.now()
    
        # Insert data into data, and p table
        print 'inserting into dTables'
        dPrice = ( iPriceId, iDate, start, end, date, symbol, bid, roll_ave, ask )
        dc.execute('INSERT INTO dPrice (iPriceId, iDate, pStartDate, pEndDate, date,symbol,bid,roll_ave,ask) \
                           VALUES (?,?,?,?,?,?,?,?,?)', dPrice)
    
        pPrice =   (iPriceId, iDate, start, end, error, date, symbol, bid, rate, ask )
        dc.execute('INSERT INTO pPrice (iPriceId, iDate, pStartDate, pEndDate, error, date, symbol, bid, rate, ask)  \
                           VALUES (?,?,?,?,?,?,?,?,?,?)', pPrice)
    except Exception, e:
        print 'ERROR !!!', str(e)
        print 'TODO write to pPrice ERROR msg'
        
    dconn.commit()
    dconn.close()
   
    # set in table to done
    conn = sqlite3.connect('in.db')
    c = conn.cursor()   
    c.execute("UPDATE iPrice SET status = 'done' WHERE iPriceID = %s" %row) #)) #, 'dPrice'))
    conn.commit()
    conn.close()
    print 'Done'

In [ ]:


In [ ]:
print 'x'

In [ ]:


In [27]:
for x in range(10):
    processSinglePrice()
    
print 'Done.'


(27, u'2015-05-30 11:16:31.859444', u'processing', u'2015-05-30 15:15:00 UTC+0000', u'GBPUSD', 1.5289, 1.5295, 1.53)
1.53
inserting into dTables
Done
(28, u'2015-05-30 11:16:39.179710', u'processing', u'2015-05-30 15:15:00 UTC+0000', u'GBPUSD', 1.5289, 1.5295, 1.53)
1.53
inserting into dTables
Done
(29, u'2015-05-30 11:16:46.792799', u'processing', u'2015-05-30 15:15:00 UTC+0000', u'GBPUSD', 1.5289, 1.5295, 1.53)
1.53
inserting into dTables
Done
(30, u'2015-05-30 11:16:54.023519', u'processing', u'2015-05-30 15:15:00 UTC+0000', u'GBPUSD', 1.5289, 1.5295, 1.53)
1.53
inserting into dTables
Done
(31, u'2015-05-30 13:46:54.273525', u'processing', u'2015-05-30 16:55:00 UTC+0000', u'GBPUSD', 1.5289, 1.5295, 1.53)
1.53
inserting into dTables
Done
(32, u'2015-05-30 13:47:01.472458', u'processing', u'2015-05-30 16:55:00 UTC+0000', u'GBPUSD', 1.5289, 1.5295, 1.53)
1.53
inserting into dTables
Done
(33, u'2015-05-30 13:47:08.713723', u'processing', u'2015-05-30 16:55:00 UTC+0000', u'GBPUSD', 1.5289, 1.5295, 1.53)
1.53
inserting into dTables
Done
(34, u'2015-05-30 13:47:15.938801', u'processing', u'2015-05-30 16:55:00 UTC+0000', u'GBPUSD', 1.5289, 1.5295, 1.53)
1.53
inserting into dTables
Done
(35, u'2015-05-30 13:47:23.146648', u'processing', u'2015-05-30 16:55:00 UTC+0000', u'GBPUSD', 1.5289, 1.5295, 1.53)
1.53
inserting into dTables
Done
No rows to input rows to process

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: