Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Mavis Bacon.. mmm.. bacon.
import urllib2
import random
import difflib
import datetime
def fetch(url):
"""Fetches requested url, returns lines in list"""
response = urllib2.urlopen(url)
lines = response.readlines()
return lines
def parse(lines):
"""Parses lines, returning code grouped by block in dictionary"""
block_tags = ['def']
ignore_tags = ['#']
docstring_tags = ['"""', "'''"]
block_depth = 0
result = {}
key = None
is_docstring = False
for line in lines:
# Strip trailing CR/LF/spaces
line = line.rstrip()
# Skip comments, etc
if search_start(line, ignore_tags):
# Handle docstrings
if any(line.strip() == docstring_tag for docstring_tag in docstring_tags):
# For lines who only content is a docstring tag, open or close the docstring toggle
is_docstring = not is_docstring
elif search_start(line, docstring_tags) and search_end(line, docstring_tags):
# For single line docstrings (lines that start and end with a docstring tag), skip
elif search_start(line, docstring_tags) and not search_end(line, docstring_tags):
# For lines that start with docstring tag, but doesn't end with tag
is_docstring = not is_docstring
# If docstring is true, line is part of a docstring block... skip.
if is_docstring:
# Get the line's whitespace depth
line_depth = len(line) - len(line.lstrip())
# If is new block definition, grab the line text to use as key
if search_start(line, block_tags):
block_depth = len(line) - len(line.lstrip())
key = line.strip()
result[key] = [line[block_depth:]]
if line_depth > block_depth and key is not None:
# Remove block_depth leading whitespace, and append line to block
return result
def search_start(line, tags):
"""Returns boolean set if line starts with any tag."""
# Strip the line
line = line.strip()
# Test if starts with any tag
starts = any(line.startswith(tag) for tag in tags)
return starts
def search_end(line, tags):
"""Returns boolean set if line ends with any tag."""
# Strip the line
line = line.strip()
# Test if ends with any tag
ends = any(line.endswith(tag) for tag in tags)
return ends
def torture_someone(functions):
# Choose random function from list of functions
function = random.choice(functions.values())
# Get index of random line in function
target_index = random.choice(range(len(function)))
# Make them do it until they get it right
accuracy = 0.00
while accuracy != 1.0:
# Create a visual separator
print "=" * 60
# Print lines from function, highlighting line to type
for line_index, line in enumerate(function):
if line_index == target_index:
result = "type this->{}".format(line)
result = " |{}".format(line)
print result
# Make someone type it in, record the time to the timer.
start_time =
submission = raw_input("your turn >")
# Replace tabs with 4 whitespaces
submission = submission.replace("\t", " ")
duration = - start_time
# Judge the puny human
accuracy = difflib.SequenceMatcher(None, submission, function[target_index]).ratio()
print "-" * 60
# Report their success
if accuracy == 1.0:
print "Nicely done, 100% match in {} seconds.".format(duration.seconds)
print "Nope...{0:.0%} accuracy in {1} seconds. Try again.".format(accuracy, duration.seconds)
print "Me >{}".format(function[target_index])
print "You >{}".format(submission)
def main():
url = ''
print "Pulling sample code from {}".format(url)
lines = fetch(url)
functions = parse(lines)
while True:
if __name__ == "__main__":
Copy link

manonthemat commented Oct 21, 2013

What does Line 68 do?

starts = any(line.startswith(tag) for tag in tags)

I thought I knew what it does, but when running with python 3, I get this confusing error message:

TypeError: startswith first arg must be bytes or a tuple of bytes, not str

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment