Skip to content

Instantly share code, notes, and snippets.

@c6p
Created August 13, 2015 18:46
Show Gist options
  • Save c6p/52fab7166afb5b457668 to your computer and use it in GitHub Desktop.
Save c6p/52fab7166afb5b457668 to your computer and use it in GitHub Desktop.
Simple console FlashCard using Leitner System
#!/usr/bin/python3
from time import time
import sqlite3
from sys import argv
import sys, tty, termios
from textwrap import fill
filepath = "/home/can/.flashcard.sqlite"
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def now():
return int(time())
def dropTable(cursor, table):
cursor.execute("DROP TABLE IF EXISTS '" + table + "'")
def createTable(cursor, table):
cursor.execute("CREATE TABLE IF NOT EXISTS '" + table +
"'(last_review INTEGER DEFAULT 0, num_completion INTEGER DEFAULT 0,"
+ " front TEXT PRIMARY KEY, back TEXT)")
def insert(cursor, table, front, back, replace=False):
cursor.execute("INSERT OR " + ("REPLACE" if replace else "IGNORE")
+ " INTO '" + table + "'(front, back) VALUES(?, ?)", (front, back))
def delete(cursor, table, front):
cursor.execute("DELETE FROM '" + table + "' WHERE front=?", (front,))
def update(cursor, table, front, last_review, num_completion):
cursor.execute("UPDATE '" + table +
"' SET last_review=?, num_completion=? WHERE front=?",
(last_review, num_completion, front))
def selectForReview(cursor, table, now, review):
cursor.execute("SELECT * FROM '" + table +
"' WHERE last_review <= CASE num_completion " +
" ".join(["WHEN " + str(i) + " THEN " + str(now-t)
for i,t in enumerate(review)]) + " ELSE " +
str(now-review[-1]) + " END")
def listTables(cursor):
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
def loop(cursor, table):
ESCAPE = chr(27)
SPACE = chr(32)
C_BOLDBLACK = "\033[1m"
C_BOLDRED = "\033[1;31m"
C_BOLDGREEN = "\033[1;32m"
C_GRAY = "\033[0;38;5;250m"
C_CLEAN = "\033[0m"
def keyAction(char):
if c == ESCAPE:
sys.exit(0)
elif c == "x":
delete(cursor, table, front)
elif c == "q":
update(cursor, table, front, now(), num+1)
elif c == "w":
update(cursor, table, front, 0, 0)
seconds_in_a_day = 86400
review = [d*seconds_in_a_day for d in
(0,4,7,12,20,30,60,90,150,270,480,730,1460,2190,4015)]
selectForReview(cursor, table, now(), review)
cards = cursor.fetchall()
lencards = " / "+str(len(cards))
for i,card in enumerate(cards):
front, back, num = card[2], card[3], card[1]
print(str(i+1)+lencards + C_GRAY +"\nFRONT: " + C_CLEAN + C_BOLDBLACK +
fill(front, 60, subsequent_indent=" "))
print(C_GRAY + "---any key - x:delete - Esc:quit---" + C_CLEAN)
c = getch() # any key
if c in ("x", ESCAPE):
keyAction(c)
else:
print(C_GRAY + "BACK : " + C_CLEAN +
fill(back, 60, subsequent_indent=" "))
print(C_GRAY + "---"+C_BOLDGREEN+"q"+C_GRAY+":correct - "+
C_BOLDRED+"w"+C_GRAY+":wrong - x:delete - Esc:quit---" + C_CLEAN)
c = getch()
while c not in ("q", "w", "x", ESCAPE): c = getch()
keyAction(c)
conn = sqlite3.connect(filepath)
conn.isolation_level = None
cursor = conn.cursor()
argc = len(argv)
if argc > 1:
first = argv[1]
if argc == 2:
listTables(cursor)
if (first,) in cursor.fetchall():
loop(cursor, first)
else:
print("Category does not exist")
elif first == "insert" and argc == 5:
createTable(cursor, argv[2])
insert(cursor, argv[2], argv[3], argv[4])
elif first == "replace" and argc == 5:
createTable(cursor, argv[2])
insert(cursor, argv[2], argv[3], argv[4], True)
elif first == "delete" and argc == 4:
delete(cursor, argv[2], argv[3])
elif first == "remove-category" and argc == 3:
dropTable(cursor, argv[2])
else:
print ("Wrong parameters")
else:
listTables(cursor)
for t in cursor:
print(t[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment