def likeconvert(likesRoot): histPath = likesRoot + '/history' convertcsv2db(likesRoot + '/totals.csv', likesRoot + '/likes.pdl') db = Base(likesRoot + '/likes.pdl') db.open() db.add_field('history', "") db.add_field('liked', "") dirContents = os.listdir(histPath) histFiles = [] for File in dirContents: if ".csv" in File: histFiles.append(File) for histFile in histFiles: try: csvfile = open(histPath + '/' + histFile, 'rb') reader = csv.DictReader(csvfile) for row in reader: if histFile.endswith('history.csv'): recName = histFile[:-11] print(recName) if db(userID=recName): rec = db(userID=recName).pop() if not rec['liked']: db.update(rec, liked=row['liked']) else: tmpLiked = rec['liked'] tmpLiked += " " + row['liked'] db.update(rec, liked=tmpLiked) if not rec['history']: db.update(rec, history=row['messageID']) else: tmpHist = rec['history'] tmpHist += " " + row['messageID'] db.update(rec, history=tmpHist) db.commit() except csv.Error: print("Could not open CSV file")
def likeconvert(likesRoot): histPath = likesRoot + "/history" convertcsv2db(likesRoot + "/totals.csv", likesRoot + "/likes.pdl") db = Base(likesRoot + "/likes.pdl") db.open() db.add_field("history", "") db.add_field("liked", "") dirContents = os.listdir(histPath) histFiles = [] for File in dirContents: if ".csv" in File: histFiles.append(File) for histFile in histFiles: try: csvfile = open(histPath + "/" + histFile, "rb") reader = csv.DictReader(csvfile) for row in reader: if histFile.endswith("history.csv"): recName = histFile[:-11] print(recName) if db(userID=recName): rec = db(userID=recName).pop() if not rec["liked"]: db.update(rec, liked=row["liked"]) else: tmpLiked = rec["liked"] tmpLiked += " " + row["liked"] db.update(rec, liked=tmpLiked) if not rec["history"]: db.update(rec, history=row["messageID"]) else: tmpHist = rec["history"] tmpHist += " " + row["messageID"] db.update(rec, history=tmpHist) db.commit() except csv.Error: print("Could not open CSV file")
for r in db('age') >= 20: print(r) print('#######') print('shortguy') for r in db('name').ilike('jeremy'): print(r) print('#######') #db support list comprehension really_really_really_short = next(r for r in db('size') < 1.0) #update supports record(s) value(s) and updates the indicie db.update(really_really_really_short, size=0.1) fp() #even shorter db.update(db, age='23') fp() #delete supports single and multiple records db.delete(r for r in db('size') >= 0.2) fp() del db[next(r for r in db('size') < 0.2)['__id__']] fp() #useful utility functions db.add_field('mood', default='catcucumber') # adds field, with optional default value db.drop_field('mood') # drops field db.path # path of db, can be changed db.name # name of db, stripped of path db.fields # fields of db, excludes __id__ & __version__ len(db) # number of records in db