Skip to content

Instantly share code, notes, and snippets.

@DavidMah
Last active December 20, 2015 15:29
Show Gist options
  • Save DavidMah/6154064 to your computer and use it in GitHub Desktop.
Save DavidMah/6154064 to your computer and use it in GitHub Desktop.
Lightweight CLI Flash Cards
#!/usr/bin/env python
import json
import random
import os
import sys
DECKS_DIR = "decks"
# In the decks dir, store JSON encoded files
# matchinf the following format
# [
# {"a": 7, "b": "echo" },
# {"a": 9, "b": "discard" }
# ]
def generate_round(deck):
game = []
for entry in deck:
a = entry['a']
b = entry['b']
game.append({'from': a, 'to': b})
game.append({'from': b, 'to': a})
random.shuffle(game)
return game
def play_round(game):
round_data = game.pop(0)
print "\033[33m%s:\033[0m" % (round_data['from']),
answer = raw_input()
if not is_close_enough(answer, round_data['to']):
# Keep this item within the first ten items
target = random.randrange(min(len(game) + 1, 10))
print target
game.insert(target, round_data)
def is_close_enough(value_one, value_two):
value_one = str(value_one).lower()
value_two = str(value_two).lower()
return value_one == value_two
def usage():
print "./quiz.py deck_name"
def main():
if len(sys.argv) != 2:
usage()
exit(1)
deck_name = sys.argv[1]
deck_path = os.path.join(DECKS_DIR, deck_name)
data = open(deck_path).read()
deck = json.loads(data)
game = generate_round(deck)
while game:
play_round(game)
print "%s rounds remain" % len(game)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment