Cryptic Christmas
Michele Simionato
mis6 at pitt.edu
Tue Dec 24 08:35:11 EST 2002
--- begin suprise.py.cry ---
cHJ
pbnQgcicnJw
ogI CAg
IF 9 fL
F8s X yxf
X1 8pI CA
gIC AgICA gIF
9fX19fX18KI
CAgICgt
LXwgfCB8\
nICAgICAg
ICAgICAgICg
tLS8gICAgKSxf
KSAgICAgICAgLF8
pIAogICAgICAgfC
B8IHwgIF8gLF8s\nX
yAgICAgICAgfCAgICAg
fF8gLF8gJyAsIF98Xyx
fLF8sIF8gICwKICAgICBf
X3wgfCB8ICgvX3wg\nfCAoX
3wgICAgIHwgICAgIHwgfHwgIH
wvXylffCB8IHwgfChffC9fKV9
fXywKICAgICggICAgICB8\nX19f
LCAgICxfX3wgICAgIFxfX19fKSAgf
F9fLCAgICAgICAgICAgfF9fLAoKCi
AgICAgICAgICAg\nKiAgICAgICAgICA
gICAsCiAgICAgICAgICAgICAgICAgICAg
ICAgXy9eXF8KICAgICAgICAgICAg\nICA
gICAgICAgIDwgICAgID4KICAgICAqICAgIC
AgICAgICAgICAgICAvLi0uXCAgICAgICAgICo
K\nICAgICAgICAgICAgICAqICAgICAgICBgLyZc
YCAgICAgICAgICAgICAgICAgICAqCiAgICAgICA
g\nICAgICAgICAgICAgICAsQC4qO0AsCiAgICAgIC
AgICAgICAgICAgICAgIC9fby5JICVfXCAgICAq\nCiA
gICAgICAgKi
AgICAgICAgI
CAgKGAnLS06
byhfQDsKICA
gICAgICAgIC
AgICAgICAgI
C9gOy0t\nLi
xfXyBgJykgI
CAgICAgICAg
ICA
gKgogICAgIC
AgI CAg
IC A gI
CAg I CA7
QG BvI CU
gTy wqYCd gJl
wg\nCiAgICA
gICAgIC
AgICogICA
gKGAnLS0p
X0AgO28gJSc
oKVwgICAgICAq
CiAgICAgICAgICA
gICAg\nICAgL2A7
LS0uX2AnJy0tLl9PJ
0A7CiAgICAgICAgICAg
ICAgICAvJiosKCl+b2A
7LS4sXyBgIiJg\nKQogIC
AgICogICAgICAgICAgL2AsQ
CA7KyYgKCkgbypgOy0nO1wKIC
AgICAgICAgICAgICAgKGAi\nI
i0tLixfMCArJSBAJyAmKClcCiAg
ICAgICAgICAgICAgIC8tLixfICAgI
GBgJyctLS4uLi4tJ2Ap\nICAqCiAg
ICAgICAgICAqICAgIC9AJTtvYDo7Jy0
tLC5fXyAgIF9fLidcCiAgICAgICAgICAg
ICAg\nOyosJigpOyBAICUgJl47fmAiYG8
7QCgpOyAgICAgICAgICoKICAgICAgICAgIC
AgICAvKCk7IG9e\nfjsgJiAoKS5vIComYDsgJ
U9cCiAgICAgICAgamdzICAgYCI9Ij09IiI9PSws
LC4sPSI9PSI9PT0i\nYAogICAgICAgICAgIF9fL
i0tLS0uKFwtJycjIyMjIy0tLS4uLl9fXy4uLi0tLS
0tLl8KICAgICAg\nICAgJ2AgICAgICAgICBcKV9gIiI
iIiJgCiAgIC
AgICAgICAgI
CAgICAgLi0t
JyAnKQogICA
gICAg\nICAg
ICAgICBvKCA
gKV8tXAogIC
AgICAgICAgI
CAgICAgIGAi
IiJ
gIGAKCicnJw
o=\ n
--- end surprise.py.cry ---
To see the surprise under the Christmas tree:
python pycrypt.py surprise.py.cry
--- begin pycrypt.py ---
#!/usr/local/bin/python
"""Program to artistically encript and decript Python scripts,
according to their file name estension.
Author: M. Simionato - mis6 at pitt.edu"""
usage="""\
Examples of usage:
./pycrypt.py program.py
creates a crypted program.py.cry
./pycrypt.py program.py.cry
decodes and executes program.py.cry
"""
import os,sys
from base64 import encodestring,decodestring
Nrow=50; Ncol=78
class GeometricFigure:
"""This class allows to define geometric figures according to their
equation in the cartesian plane. Moreover addition and subtraction
of geometric figures are defined as union and subtraction of sets.
This program could be useful to teach geometry to students, too :-)
"""
def __init__(self,equation,**parameters):
"Initialize "
self.eq=equation
self.par=parameters
for (k,v) in self.par.items(): #specifies the parameters
self.eq=self.eq.replace(k,str(v))
self.contains=eval('lambda x,y : '+self.eq)
def combine(self,fig,operator):
"""Combine self with the geometric figure fig, using the
operators "or" (addition) and "and not" (subtraction)"""
comboeq="("+self.eq+")"+operator+"("+fig.eq+")"
combopar=fig.par.copy(); combopar.update(self.par)
return GeometricFigure(comboeq,**combopar)
def __add__(self,fig):
"Union of sets"
return self.combine(fig,' or ')
def __sub__(self,fig):
"Subtraction of sets"
return self.combine(fig,' and not')
def format_text(text,fig):
"Format the text in the shape given by fig"
i=0; row=0; L=len(text); EOF=0; out=[]
while not EOF:
for col in range(Ncol):
if fig.contains(col,row):
out.append(text[i]); i+=1
if i==L: EOF=1; break
else: out.append(" ")
out.append('\n'); row+=1
if row==Nrow: row=0
return ''.join(out)
def encript_decript(fname,shape):
"""If file name ends with ".py", encript the text;
otherwise decript and execute it."""
if fname.endswith('.py'): #encode
text=file(fname).read()
text=encodestring(text).replace('\n','\\n')
shaped=format_text(text,shape)
print >> file(fname+'.cry','w'),shaped,
print shaped
print "Written on",fname+".cry"
elif fname.endswith('.cry'): #decode
text=file(fname).read().replace(' ','').replace('\n','')
code=decodestring(text.replace('\\n','\n'))
os.popen('python2','w').write(code) #execute
else:
print "Unrecognized file type.\n",usage
#Library of useful geometric figures; you may want to play with them
bigdisk=GeometricFigure('(x-x0)**2/4+(y-y0)**2 <= R**2',
x0=26,y0=20,R=12) # not used
smalldisk=GeometricFigure('(x-x0)**2/4+(y-y0)**2 <= r**2',
x0=26,y0=20,r=4) # not used
otherdisk=GeometricFigure('(x-x1)**2/4+(y-y0)**2 <= r**2',
x1=68,y0=20,r=4) # not used
square=GeometricFigure('abs(x-x0)/2<L and abs(y-y0)<L',
x0=40,y0=10,L=10) # not used
triangle=GeometricFigure('((x-x0)**2 <= A*(y-ymin)**2 and ymin <= y <= ymax)',
A=.5,ymin=10,ymax=40,x0=40)
rectangle=GeometricFigure('abs(x-x0)/2<Lx and abs(y-y0)<Ly',
x0=40,y0=45,Lx=3,Ly=5)
circle=GeometricFigure('R1**2 <= (x-x0)**2/4+(y-yc0)**2 <= R2**2',
R1=3,R2=4,x0=40,yc0=11)
XmasTree=triangle+rectangle+circle
#Main
if __name__=='__main__':
try: fname=sys.argv[1]
except IndexError: print usage
else: encript_decript(fname,shape=XmasTree)
--- end pycrypt.py ---
More information about the Python-list
mailing list