Skip to content

Instantly share code, notes, and snippets.

@Ivoz
Last active January 31, 2016 18:10
Show Gist options
  • Select an option

  • Save Ivoz/3de6ff284d327e49afd0 to your computer and use it in GitHub Desktop.

Select an option

Save Ivoz/3de6ff284d327e49afd0 to your computer and use it in GitHub Desktop.

Revisions

  1. Ivoz revised this gist Jan 31, 2016. 1 changed file with 2 additions and 8 deletions.
    10 changes: 2 additions & 8 deletions quine.py
    Original file line number Diff line number Diff line change
    @@ -12,10 +12,7 @@
    quine = f.read()
    output = subprocess.check_output("%s %s" % (sys.executable, sys.argv[1]))
    if quine == output:
    sys.stdout.buffer.write(output)
    sys.exit()
    else:
    if quine != output:
    sys.exit(1)
    foreword = """#!/usr/bin/env python3
    @@ -35,10 +32,7 @@
    quine = f.read()

    output = subprocess.check_output("%s %s" % (sys.executable, sys.argv[1]))
    if quine == output:
    sys.stdout.buffer.write(output)
    sys.exit()
    else:
    if quine != output:
    sys.exit(1)

    foreword = """#!/usr/bin/env python3
  2. Ivoz revised this gist Jan 31, 2016. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions quine.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/usr/bin/env python3
    '''
    A "self-verifying" quine - `python quine.py quine.py` will only print itself
    if `python quine.py` is the same as `cat quine.py`
    A "self-verifying" quine - `<quine> <source_file>` will only print itself
    if `<quine>` has the same output as `cat <source_file>`
    '''
    source = r'''{foreword}
    @@ -20,8 +20,8 @@
    foreword = """#!/usr/bin/env python3
    \'\'\'
    A "self-verifying" quine - `python quine.py quine.py` will only print itself
    if `python quine.py` is the same as `cat quine.py`
    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)'
    @@ -43,8 +43,8 @@

    foreword = """#!/usr/bin/env python3
    \'\'\'
    A "self-verifying" quine - `python quine.py quine.py` will only print itself
    if `python quine.py` is the same as `cat quine.py`
    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)'
  3. Ivoz revised this gist Jan 31, 2016. 1 changed file with 44 additions and 5 deletions.
    49 changes: 44 additions & 5 deletions quine.py
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,51 @@
    #!/usr/bin/env python3
    '''
    A "self-verifying" quine - `python quine.py quine.py` will only print itself
    if `python quine.py` is the same as `cat quine.py`
    '''
    source = r'''{foreword}
    import sys
    import sys, subprocess
    if len(sys.argv) > 1:
    with open(sys.argv[1], 'rb') as f:
    quine = f.read()
    line = 'source.format(foreword="source = r\'\'\'" + source + "\'\'\'", program=line)'
    output = subprocess.check_output("%s %s" % (sys.executable, sys.argv[1]))
    if quine == output:
    sys.stdout.buffer.write(output)
    sys.exit()
    else:
    sys.exit(1)
    foreword = """#!/usr/bin/env python3
    \'\'\'
    A "self-verifying" quine - `python quine.py quine.py` will only print itself
    if `python quine.py` is the same as `cat quine.py`
    \'\'\'
    source = r\'\'\'"""
    last_line = 'source.format(foreword=foreword + source + "\'\'\'", program=last_line)'
    sys.stdout.write({program})
    '''

    import sys
    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.stdout.buffer.write(output)
    sys.exit()
    else:
    sys.exit(1)

    line = 'source.format(foreword="source = r\'\'\'" + source + "\'\'\'", program=line)'
    sys.stdout.write(source.format(foreword="source = r'''" + source + "'''", program=line))
    foreword = """#!/usr/bin/env python3
    \'\'\'
    A "self-verifying" quine - `python quine.py quine.py` will only print itself
    if `python quine.py` is the same as `cat quine.py`
    \'\'\'
    source = r\'\'\'"""
    last_line = 'source.format(foreword=foreword + source + "\'\'\'", program=last_line)'
    sys.stdout.write(source.format(foreword=foreword + source + "'''", program=last_line))
  4. Ivoz created this gist Jan 31, 2016.
    12 changes: 12 additions & 0 deletions quine.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    source = r'''{foreword}
    import sys
    line = 'source.format(foreword="source = r\'\'\'" + source + "\'\'\'", program=line)'
    sys.stdout.write({program})
    '''

    import sys

    line = 'source.format(foreword="source = r\'\'\'" + source + "\'\'\'", program=line)'
    sys.stdout.write(source.format(foreword="source = r'''" + source + "'''", program=line))