蟒蛇
你知道吗?为什么不使其成为单个表达式?
P = (lambda M,P=None:(lambda t:P[:74]+repr(M)[1:-1]+"'))"if P else M[74:-3])(''))
Pc = "(lambda M,P=None:(lambda t:P[:74]+repr(M)[1:-1]+\"'))\"if P else M[74:-3])(''))"
P2c = P('Hi there, mate!', Pc)
print "Encode tests:"
print " P2 = P('Hi there, mate!', Pc) =", P2c
exec 'P2 = ' + P2c
print " P2(\"Test 2's the best.\", P2c) =", P2("Test 2's the best.", P2c)
print "Decode tests:"
print "P2(P2) =", P2(P2c)
print "P(P2) =", P(P2c)
print "P2(P) =", P2(Pc)
print "P(P) =", P(Pc)
旧消息;函数P接受指定的参数,并输出结果代码/解码文本。
def P(data,func=None):
text = ""
if func:
return func[:35]+data+'"\n'+'\n'.join(func.split('\n')[2:])
return data[35:].split('\n')[0][:-1]
# The source code.
Pc = """def P(data,func=None):
text = ""
if func:
return func[:35]+data+'"\\n'+'\\n'.join(func.split('\\n')[2:])
return data[35:].split('\\n')[0][:-1]"""
P2c = P('Hi there, mate!', Pc)
print "Encode test:"
print "P('Hi there, mate!', P) ->"
print P2c
# This is outputted by P('Hi there, mate!', code-of-P)
def P2(data,func=None):
text = "Hi there, mate!"
if func:
return func[:35]+data+'"\n'+'\n'.join(func.split('\n')[2:])
return data[35:].split('\n')[0][:-1]
print "P2('Text 2', P2) -<"
print P2('Text 2', P2c)
print "Decode test:"
print "P2(P2) =", P2(P2c)
print "P(P2) =", P(P2c)
print "P2(P) =", P2(Pc)
print "P(P) =", P(Pc)