Skip to content

Instantly share code, notes, and snippets.

@Ivoz

Ivoz/quine.py

Last active Jan 31, 2016
Embed
What would you like to do?
A self-checking python quine, of sorts
#!/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