Guest User

Untitled

a guest
Dec 6th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. import random
  2. from urllib import urlopen
  3. import sys
  4.  
  5. WORD_URL = "http://learncodethehardway.org/words.txt"
  6. WORDS = []
  7.  
  8. PHRASES = {
  9. "class %%%(%%%):":
  10. "Make a class named %%% that is-a %%%.",
  11. "class %%%(object):\n\tdef __init__(self, ***)" :
  12. "class %%% has-a __init__ that takes self and *** parameters.",
  13. "class %%%(object):\n\tdef ***(self, @@@)":
  14. "class %%% has-a function named *** that takes self and @@@ parameters.",
  15. "*** = %%%()":
  16. "Set *** to an instance of class %%%.",
  17. "***.***(@@@)":
  18. "From *** get the *** function, and call it with parameters self, @@@.",
  19. "***.*** = '***'":
  20. "From *** get the *** attribute and set it to '***'."
  21. }
  22.  
  23. # do they want to drill phrases first
  24. if len(sys.argv) == 2 and sys.argv[1] == "english":
  25. PHRASE_FIRST = True
  26. else:
  27. PHRASE_FIRST = False
  28.  
  29. # load up the words from the website
  30. for word in urlopen(WORD_URL).readlines():
  31. WORDS.append(word.strip())
  32.  
  33.  
  34. def convert(snippet, phrase):
  35. class_names = [w.capitalize() for w in
  36. random.sample(WORDS, snippet.count("%%%"))]
  37. other_names = random.sample(WORDS, snippet.count("***"))
  38. results = []
  39. param_names = []
  40.  
  41. for i in range(0, snippet.count("@@@")):
  42. param_count = random.randint(1,3)
  43. param_names.append(', '.join(random.sample(WORDS, param_count)))
  44.  
  45. for sentence in snippet, phrase:
  46. result = sentence[:]
  47.  
  48. # fake class names
  49. for word in class_names:
  50. result = result.replace("%%%", word, 1)
  51.  
  52. # fake other names
  53. for word in other_names:
  54. result = result.replace("***", word, 1)
  55.  
  56. # fake parameter lists
  57. for word in param_names:
  58. result = result.replace("@@@", word, 1)
  59.  
  60. results.append(result)
  61.  
  62. return results
  63.  
  64.  
  65. # keep going until they hit CTRL-D
  66. try:
  67. while True:
  68. snippets = PHRASES.keys()
  69. random.shuffle(snippets)
  70.  
  71. for snippet in snippets:
  72. phrase = PHRASES[snippet]
  73. question, answer = convert(snippet, phrase)
  74. if PHRASE_FIRST:
  75. question, answer = answer, question
  76.  
  77. print question
  78.  
  79. raw_input("> ")
  80. print "ANSWER: %s\n\n" % answer
  81. except EOFError:
  82. print "\nBye"
Add Comment
Please, Sign In to add comment