Skip to content

Instantly share code, notes, and snippets.

@ace0
Created January 30, 2020 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ace0/3be26e657d14ded7b5ef7957d163d72f to your computer and use it in GitHub Desktop.
Save ace0/3be26e657d14ded7b5ef7957d163d72f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
'''
Simple vimgolf that downloads challenges at random and compares output
to the expected output.
'''
import feedparser, filecmp, requests, random, subprocess, tempfile
def main():
# Select a challenge link from the challenges feed at random
feedsUrl = 'http://feeds.vimgolf.com/latest-challenges'
feed = feedparser.parse(feedsUrl)
link = random.choice(feed['entries'])['link']
challenge = requests.get(link).json()
# Write the input and output to tmp files
infile = write(challenge['in']['data'])
outfile = write(challenge['out']['data'])
# Open the files in split screen mode in vim
subprocess.run(['vim', '-O', infile, outfile])
# Diff the files on close and report the outcome
if filecmp.cmp(infile, outfile):
print("Great job!")
else:
print("Sorry, not an exact match")
def write(contents):
with tempfile.NamedTemporaryFile('wt', delete=False) as tmp:
tmp.write(contents)
return tmp.name
# Run!
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment