Skip to content
A simple quine template and compiler in Python.
Python
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
README.md
compile.py
pre-quine.py

README.md

Quine in Python

A quine is a self-reproducing program, that is, a program whose output is the same as its source code.

In this repo we have two files, pre-quine.py which is the template of the quine and compile.py which takes the template and fills in the tape.

To compile the template, we pass it as an argument to the compiler.

andrei@css-tech-x ~/quine> python3 compile.py pre-quine.py > output.py

The quine will look as follows.

andrei@css-tech-x ~/quine> cat output.py 

tape = [10, 116, 97, 112, 101, 32, 61, 32, 10, 97, 116, 32, 61, 32, 56, 10, 10, 100, 101, 102, 32, 100, 101, 99, 111, 100, 101, 95, 116, 97, 112, 101, 40, 116, 97, 112, 101, 95, 112, 97, 114, 116, 41, 58, 10, 32, 32, 32, 32, 114, 101, 116, 117, 114, 110, 32, 39, 39, 46, 106, 111, 105, 110, 40, 109, 97, 112, 40, 99, 104, 114, 44, 32, 116, 97, 112, 101, 95, 112, 97, 114, 116, 41, 41, 10, 10, 108, 101, 102, 116, 32, 61, 32, 100, 101, 99, 111, 100, 101, 95, 116, 97, 112, 101, 40, 116, 97, 112, 101, 91, 58, 97, 116, 93, 41, 10, 109, 105, 100, 100, 108, 101, 32, 61, 32, 115, 116, 114, 40, 116, 97, 112, 101, 41, 10, 114, 105, 103, 104, 116, 32, 61, 32, 100, 101, 99, 111, 100, 101, 95, 116, 97, 112, 101, 40, 116, 97, 112, 101, 91, 97, 116, 58, 93, 41, 10, 112, 114, 105, 110, 116, 40, 108, 101, 102, 116, 32, 43, 32, 109, 105, 100, 100, 108, 101, 32, 43, 32, 114, 105, 103, 104, 116, 41, 10]
at = 8

def decode_tape(tape_part):
    return ''.join(map(chr, tape_part))

left = decode_tape(tape[:at])
middle = str(tape)
right = decode_tape(tape[at:])
print(left + middle + right)

And this will match the output of the program as desired.

andrei@css-tech-x ~/quine> python3 output.py 

tape = [10, 116, 97, 112, 101, 32, 61, 32, 10, 97, 116, 32, 61, 32, 56, 10, 10, 100, 101, 102, 32, 100, 101, 99, 111, 100, 101, 95, 116, 97, 112, 101, 40, 116, 97, 112, 101, 95, 112, 97, 114, 116, 41, 58, 10, 32, 32, 32, 32, 114, 101, 116, 117, 114, 110, 32, 39, 39, 46, 106, 111, 105, 110, 40, 109, 97, 112, 40, 99, 104, 114, 44, 32, 116, 97, 112, 101, 95, 112, 97, 114, 116, 41, 41, 10, 10, 108, 101, 102, 116, 32, 61, 32, 100, 101, 99, 111, 100, 101, 95, 116, 97, 112, 101, 40, 116, 97, 112, 101, 91, 58, 97, 116, 93, 41, 10, 109, 105, 100, 100, 108, 101, 32, 61, 32, 115, 116, 114, 40, 116, 97, 112, 101, 41, 10, 114, 105, 103, 104, 116, 32, 61, 32, 100, 101, 99, 111, 100, 101, 95, 116, 97, 112, 101, 40, 116, 97, 112, 101, 91, 97, 116, 58, 93, 41, 10, 112, 114, 105, 110, 116, 40, 108, 101, 102, 116, 32, 43, 32, 109, 105, 100, 100, 108, 101, 32, 43, 32, 114, 105, 103, 104, 116, 41, 10]
at = 8

def decode_tape(tape_part):
    return ''.join(map(chr, tape_part))

left = decode_tape(tape[:at])
middle = str(tape)
right = decode_tape(tape[at:])
print(left + middle + right)
You can’t perform that action at this time.