Created
January 30, 2020 16:21
-
-
Save ace0/3be26e657d14ded7b5ef7957d163d72f to your computer and use it in GitHub Desktop.
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
#!/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