Skip to content

Instantly share code, notes, and snippets.

@Ming-Tang
Created August 6, 2011 01:18
Show Gist options
  • Save Ming-Tang/1128876 to your computer and use it in GitHub Desktop.
Save Ming-Tang/1128876 to your computer and use it in GitHub Desktop.
Quine program that saves state by outputting its own source code and session data
from time import gmtime, strftime;data = { }
while True:
inp = raw_input().split(" ")
if inp[0] == '*':
data = { }
elif len(inp) < 2:
break
else:
if inp[0] == '!':
del data[inp[1]]
elif inp[0] == '?':
if inp[1] in data:
print data[inp[1]]
else:
data[inp[0]] = inp[1]
def quine(source):
global data
f = open('session' + strftime("%Y-%m-%d-%H-%M-%S", gmtime()) + '.py', 'w')
quote = '"' * 3
f.write('from time import gmtime, strftime;data = ' + repr(data) + source + '(' + quote + source + quote + ')')
f.close()
quine("""
while True:
inp = raw_input().split(" ")
if inp[0] == '*':
data = { }
elif len(inp) < 2:
break
else:
if inp[0] == '!':
del data[inp[1]]
elif inp[0] == '?':
if inp[1] in data:
print data[inp[1]]
else:
data[inp[0]] = inp[1]
def quine(source):
global data
f = open('session' + strftime("%Y-%m-%d-%H-%M-%S", gmtime()) + '.py', 'w')
quote = '"' * 3
f.write('from time import gmtime, strftime;data = ' + repr(data) + source + '(' + quote + source + quote + ')')
f.close()
quine""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment