Python: A recipe for cryptic code?
By Angsuman Chakraborty, Gaea News NetworkWednesday, April 13, 2005
I have heard that Python is a great programming language which is so much superior to everything around etc.
The following code in python is touted as the world’s smallest p2p client & server.
# tinyp2p.py 1.0 (documentation at https://freedom-to-tinker.com/tinyp2p.html)
import sys, os, SimpleXMLRPCServer, xmlrpclib, re, hmac # (C) 2004, E.W. Felten
ar,pw,res = (sys.argv,lambda u:hmac.new(sys.argv[1],u).hexdigest(),re.search)
pxy,xs = (xmlrpclib.ServerProxy,SimpleXMLRPCServer.SimpleXMLRPCServer)
def ls(p=”"):return filter(lambda n:(p==”")or res(p,n),os.listdir(os.getcwd()))
if ar[2]!=”client”: # license: https://creativecommons.org/licenses/by-nc-sa/2.0
myU,prs,srv = (”https://”+ar[3]+”:”+ar[4], ar[5:],lambda x:x.serve_forever())
def pr(x=[]): return ([(y in prs) or prs.append(y) for y in x] or 1) and prs
def c(n): return ((lambda f: (f.read(), f.close()))(file(n)))[0]
f=lambda p,n,a:(p==pw(myU))and(((n==0)and pr(a))or((n==1)and [ls(a)])or c(a))
def aug(u): return ((u==myU) and pr()) or pr(pxy(u).f(pw(u),0,pr([myU])))
pr() and [aug(s) for s in aug(pr()[0])]
(lambda sv:sv.register_function(f,”f”) or srv(sv))(xs((ar[3],int(ar[4]))))
for url in pxy(ar[3]).f(pw(ar[3]),0,[]):
for fn in filter(lambda n:not n in ls(), (pxy(url).f(pw(url),1,ar[4]))[0]):
(lambda fi:fi.write(pxy(url).f(pw(url),2,fn)) or fi.close())(file(fn,”wc”))
Source: TinyP2P
I started my programming career with assembly language (writing tiny .com programs and playing with cuties like masm and tasm), then moved to C, even mess with java byte codes for fun.
But this wonderful gem of a program (displayed above) beat the crap out of me.
Python, going by this example, looks to me a sure-fire recipe for creating obfuscated code. I would rather read 80×86 instructions, thank you very much!
PS. Java programmers, you can relax. Python programmers (oops hackers as per naive bayesian dude) writing such code will never be able to maintain it. So you will definitely be called in to replace such “hackers” and rewrite the whole thing in Java (use the word refactoring to get better traction with management).
July 28, 2010: 5:02 pm
Alright, so I’m a little late to the party. But whatever. Ed Welch, I’ve been coding in Python for only a few months and I can already read that quite easily. It takes a bit to get used to; I thought it was terrible at first, but it grew on me. I’d say this is because I come from a Java/C++ background. The “”.join() already tells me it’s creating a string from an iterable datatype. The rest is a simple list comprehension making a list from a dictionary. If you had made an effort and actually finished that chapter of Dive Into Python, you’d know this. |
Ed Welch |
October 3, 2007: 7:21 am
I tend to agree with the artical. Have a look at examples in the Python tutorials: This statement could easily be written using a loop and be much easier to understand. It seems to be cool to Python programmers try to write the entire program in one line and the syntax of the language seems to be designed that way. |
April 13, 2005: 7:08 am
You picked what is probably the single most obsfucated chunk of Python code in existence and are attempting to make a generalization about the entire language based on it. It’s folly. > Why not in assembly language? It would have been much faster and more compact. It would have taken longer, required many more lines, and would have been limited to running on a specific platform. > Any code written in high level language should be readable and maintable. More folly. Python syntax *encourages* readable and maintainable code, it doesn’t require it. |
verbat |
April 13, 2005: 6:37 am
not the shortest p2p app, there is at least a shorter one in ruby and an even shorter one in perl. See Btw, doing this things in python is more impressive. |
Jay |
April 13, 2005: 5:32 am
From https://www.gamedev.net/hosted/javaextreme/obfpoll.htm: import java.awt.*;import java.util.*;public class C extends Frame{Date D=new Date();void T(Date d){D=d;repaint();}double P=Math.PI,A=P/2,a,c,U=.05;int W,H,m,R;double E(int a,int u){return(3*P/2+2*P*a/u)%(2*P);}void N(Graphics g,double q,double s){g.fillPolygon(new int[]{H(s,q),H(U,q+A),H(U,q+3*A)},new int[]{J(s,q),J(U,q+A),J(U,q+3*A)},3);}public void paint(Graphics g){Color C=SystemColor.control;g.setColor(C);g.fillRect(0,0,W=size().width,H=size().height);W-=52;H-=52;R=Math.min(W/2,H/2);g.translate(W/2+25,H/2+36);g.setColor(C.darker());for(m=0;m javac C.java Author & Submitter: David M. Hewitt/1999″ |
April 13, 2005: 5:14 am
@Laszlo Marai BTW: You mentioned that the code is meant to be compact and not readable or maintable. Then why did he code it in Python? Why not in assembly language? It would have been much faster and more compact. Any code written in high level language should be readable and maintable. I seriously doubt you can match this level of obfuscation in Java. Heck I can even read the decompiled obfuscated(using standard obfuscators; not by the programmer) java code properly. PS: Be sure to check the categories this post is under. |
April 13, 2005: 5:06 am
Be sure to catch the readable rebuke : …wonder if that runs in Jython? |
April 13, 2005: 3:55 am
I’m a java programmer (and an occasioanl python/jython one). But this is not really fair. One can write obfuscated code in any language. This code is like when you write multiple statments in one line in any C style language (like in java). Or you can nest a lot of method calls, which you normally don’t do if you wan’t maintanable code. This code is meant to be compact and not readible or maintainable. There are no blocks (no indentations) only one liner methods and lambda (inline defined) functions. Python does look good when you program it in the right, standard style. And there is such a thing, just as with java (though there are some annoying inconsistencies, like some people and the stdlib uses camel case here and underscores there, etc). |
Nathan