Last active
November 9, 2017 11:15
-
-
Save jamesray1/0bf776991c50f2662408f6943cc39e3a to your computer and use it in GitHub Desktop.
Learn Python the Hard Way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from urllib.request import urlopen | |
import sys | |
WORD_URL = "http://learncodethehardway.org/words.txt" | |
WORDS = [] | |
PHRASES = { | |
"class %%%(%%%):": | |
"Make a class named %%% that is-a %%%.", | |
"class %%%(object):\n\tdef __init__(self, ***)" : | |
"class %%% has-a __init__ that takes self and *** params.", | |
"class %%%(object):\n\tdef ***(self, @@@)": | |
"class %%% has-a function *** that takes self and @@@ params.", | |
"*** = %%%()": | |
"Set *** to an instance of class %%%.", | |
"***.***(@@@)": | |
"From *** get the *** function, call it with params self, @@@.", | |
"***.*** = '***'": | |
"From *** get the *** attribute and set it to '***'." | |
} | |
# do they want to drill phrases first | |
if len(sys.argv) == 2 and sys.argv[1] == "english": | |
PHRASE_FIRST = True | |
else: | |
PHRASE_FIRST = False | |
# load up the words from the website | |
for word in urlopen(WORD_URL).readlines(): | |
WORDS.append(str(word.strip(), encoding="utf-8")) | |
def convert(snippet, phrase): | |
class_names = [w.capitalize() for w in random.sample(WORDS, snippet.count("%%%"))] | |
other_names = random.sample(WORDS, snippet.count("***")) | |
English = [] | |
param_names = [] | |
for i in range(0, snippet.count("@@@")): | |
param_count = random.randint(1,3) | |
#if param_names == []: | |
# word = random.sample(WORDS, param_count) | |
# param_names.append(f'{word}') | |
#else: | |
# param_names.append(', '.join( | |
# random.sample(WORDS, param_count))) | |
param_names.append(', '.join(random.sample(WORDS, param_count))) | |
for sentence in snippet, phrase: | |
result = sentence[:] | |
# fake class names | |
for word in class_names: | |
result = result.replace("%%%", word, 1) | |
snippet = snippet.replace("%%%", word, 1) | |
# fake other names | |
for word in other_names: | |
result = result.replace("***", word, 1) | |
snippet = snippet.replace("***", word, 1) | |
# fake parameter lists | |
for word in param_names: | |
result = result.replace("@@@", word, 1) | |
snippet = snippet.replace("@@@", word, 1) | |
English.append(result) | |
#English = English[2:len(English)-2] | |
#param_names.append(param_name) | |
return English[0], snippet | |
# keep going until they hit CTRL-D | |
try: | |
while True: | |
snippets = list(PHRASES.keys()) | |
random.shuffle(snippets) | |
for snippet in snippets: | |
phrase = PHRASES[snippet] | |
# phrase in line below before = was originally answer | |
phrase_out, snippet_out = convert(snippet, phrase) | |
def getMultiLineInput(): | |
guess = "" | |
while True: | |
try: | |
line = input() | |
except KeyboardInterrupt: | |
break | |
if guess != "": | |
guess += '\n' + line | |
else: | |
guess += line | |
return guess | |
if PHRASE_FIRST: | |
print(f"The following phrase is how to read out a Python code snippet:\n{phrase_out}") | |
print("Enter exactly the corresponding code, then press enter and then press CTRL-C:") | |
guess = getMultiLineInput() | |
if guess == snippet_out: | |
print("Correct, you entered it exactly right!\n\n") | |
else: | |
print(f"That's not exactly correct. You entered:\n{guess}\nThe corresponding code is:\n{snippet_out}\n\n") | |
else: | |
print(f"Here is some Python code:\n{snippet_out}") | |
print("Enter exactly how you would read it out, then press enter and then press CTRL-C:") | |
guess = getMultiLineInput() | |
if guess == phrase_out: | |
print("Correct, you entered it exactly right!\n\n") | |
else: | |
print(f"That's not exactly correct. Here is how you would read it out:\n{phrase_out}\n\n") | |
except EOFError: | |
print("\nBye") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment