Last active
January 31, 2016 18:10
-
-
Save Ivoz/3de6ff284d327e49afd0 to your computer and use it in GitHub Desktop.
A self-checking python quine, of sorts
This file contains hidden or 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 | |
''' | |
A "self-verifying" quine - `<quine> <source_file>` will only print itself | |
if `<quine>` has the same output as `cat <source_file>` | |
''' | |
source = r'''{foreword} | |
import sys, subprocess | |
if len(sys.argv) > 1: | |
with open(sys.argv[1], 'rb') as f: | |
quine = f.read() | |
output = subprocess.check_output("%s %s" % (sys.executable, sys.argv[1])) | |
if quine != output: | |
sys.exit(1) | |
foreword = """#!/usr/bin/env python3 | |
\'\'\' | |
A "self-verifying" quine - `<quine> <source_file>` will only print itself | |
if `<quine>` has the same output as `cat <source_file>` | |
\'\'\' | |
source = r\'\'\'""" | |
last_line = 'source.format(foreword=foreword + source + "\'\'\'", program=last_line)' | |
sys.stdout.write({program}) | |
''' | |
import sys, subprocess | |
if len(sys.argv) > 1: | |
with open(sys.argv[1], 'rb') as f: | |
quine = f.read() | |
output = subprocess.check_output("%s %s" % (sys.executable, sys.argv[1])) | |
if quine != output: | |
sys.exit(1) | |
foreword = """#!/usr/bin/env python3 | |
\'\'\' | |
A "self-verifying" quine - `<quine> <source_file>` will only print itself | |
if `<quine>` has the same output as `cat <source_file>` | |
\'\'\' | |
source = r\'\'\'""" | |
last_line = 'source.format(foreword=foreword + source + "\'\'\'", program=last_line)' | |
sys.stdout.write(source.format(foreword=foreword + source + "'''", program=last_line)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment