コード例 #1
0
ファイル: db.py プロジェクト: jmz527/myLifeDB
def test(tbl='logs'):
    '''
    >>> db, d = test('location')
    for row in d.dict:
        sql = db.is_duplicate('location', row)
        if sql: db.query(sql)
    '''
    from utils import unicode_csv_reader, replace_txt, remove_duplicates
    import tablib

    db = SimpleDB(os.path.join(os.getcwd(), 'test.db'))

    if tbl == 'logs':
        f_name  = 'test_eternity.csv'
        headers = ('day','start_time','stop_time','duration',
                   'parent','activity','note','tags')
    elif tbl == 'location':
        f_name  = 'test_gps.csv'
        headers = ('latitude','longitude','elevation','timestamp')
    else:
        raise Exception, "test(args) must = eternity or gps"

    # get data
    with open(os.path.join(os.getcwd(), f_name), 'r') as f:
        #d = list(set([tuple(row) for row in unicode_csv_reader(f)]))
        d = remove_duplicates([tuple(row) for row in unicode_csv_reader(f)])
        data = tablib.Dataset(*d, headers=headers)

    # TODO - adjust replace_txt() function to accept orderedDicts
    #        since the order of replacement is important.

    # replacement dicts
    parent_dict = {
        u'Media>':                        u'MEDIA',
        u'MISC - Real Life>':             u'REAL_LIFE',
        u'Basic Routine>Meals & Snacks>': u'BASIC',
        u'Basic Routine>':                u'BASIC',
        u'Salubrious Living>':            u'HEALTH',
    }
    activity_dict = {
        u'RL - MISC - Home':    u'HOME',
        u'RL - MISC - Outside': u'OUTSIDE',
        u'へんたい':                u'HENTAI',
        u'アニメ':                 u'ANIME',
        u'Grocery Shopping':    u'GROCERY-SHOPPING',
        u'Restaurant':          u'RESTAURANT',
        u'Shower & Bathroom':   u'SHOWER-BATHROOM'
    }

    # test for duplicates in data (skip over the first row to avoid headers)
    for row in data.dict[1:]:
        if tbl == 'logs':
            row['parent']   = replace_txt(row['parent'], parent_dict)
            row['activity'] = replace_txt(row['activity'], activity_dict)

        sql = db.is_duplicate(tbl, row)
        if sql:
            db.query(sql)
コード例 #2
0
ファイル: mylife.py プロジェクト: jmz527/myLifeDB
def insertEternity(d):
    '''Formats eternity csv data to ready it for import to sqlite
    database fields:
        id, day, start_time, stop_time, duration,
        loc_id, parent, activity, note, tags
    CSV file fields:
        day, start time, stop time, duration,
        hierarchy path, activity name, note, tags
    '''
    headers = ('day','start_time','stop_time',
               'duration','parent','activity','note','tags')
    data = tablib.Dataset(*d, headers=headers)

    # replacement dicts
    parent_dict = {
        u'Media>':                        u'MEDIA',
        u'MISC - Real Life>':             u'REAL_LIFE',
        u'Basic Routine>Meals & Snacks>': u'MEALS-SNACKS',
        u'Basic Routine>':                u'BASIC',
        u'Salubrious Living>':            u'HEALTH',
    }
    activity_dict = {
        u'RL - MISC - Home':    u'HOME',
        u'RL - MISC - Outside': u'OUTSIDE',
        u'へんたい':              u'HENTAI', # Im not actually a hentai fan, this is for tracking how often I masterbate; yes, I realize this repo is public.
        u'アニメ':               u'ANIME',
        u'Grocery Shopping':    u'GROCERY-SHOPPING',
        u'Restaurant':          u'RESTAURANT',
        u'Shower & Bathroom':   u'SHOWER-BATHROOM'
    }

    for row in data.dict[1:]:
        # replace txt
        row['parent']   = replace_txt(row['parent'], parent_dict)
        row['activity'] = replace_txt(row['activity'], activity_dict)
        row['activity'] = row['activity'].upper()
        #row['note']    = replace_txt(row['note'], note_dict)
        #row['tags']    = replace_txt(row['tags'], tags_dict)

        # check if item is a duplicate, if not, insert the item
        sql = db.is_duplicate('logs', row)
        if sql:
            db.query(sql)

    print('Finished inserting new Eternity log data')