elblogg

blah, blah, blag, blog

Posts Tagged ‘python’

Unreadable code

Thursday, November 27th, 2008

If you’re really trying, you will always be able to write completely unreadable code in any programming language. All the language designers attemts to force you to write readable code, and even making it so the easiest way ahead, even for quick hacks is to write it as readable and reusable code will fail if you’re really trying to write unreadable code.

This guy is obviously concerned about “job security”. (not the blog author, but the code author)

  1. #!/usr/bin/env python
  2. import os,sys
  3. C=os.chdir
  4. S=os.system
  5. M=os.mkdir
  6. J=os.path.join
  7. A=os.path.abspath
  8. D=os.path.dirname
  9. E=os.path.exists
  10. W=sys.stdout.write
  11. V=sys.argv
  12. X=sys.exit
  13. ERR=lambda m:W(m+"\n")
  14. PRNT=lambda m:W(m+"\n")
  15. assert len(V)==2,"you must provide a sandbox name"
  16. SB=V[1]
  17. H=A(D(__file__))
  18. SBD=J(D(H),SB)
  19. C(SBD)
  20. PST=J(SBD,‘bin/paster’)
  21. VAR=J(SBD,‘var’)
  22. ETC=J(SBD,‘etc’)
  23. S("mkdir -p "+VAR)
  24. PRNT("restarting "+SB)
  25. CMD=";".join([’source %s’%J(SBD,‘bin/activate’),PST+" serve –daemon –pid-file=%s/sandbox.pid –log-file=%s/sandbox.log %s/sandbox.ini start"%(VAR,VAR,ETC)])
  26. PRNT(CMD)
  27. S(CMD)
  28. PRNT("All done!")
  29. X(0)

Aaannd… Heres my understanding of the code:

  1. #!/usr/bin/env python
  2. import os,sys
  3.  
  4. #bail out if there’s no sandbox specified
  5. assert len(sys.argv)==2,"you must provide a sandbox name"
  6. sandbox=sys.argv[1]
  7.  
  8. #get the dir of this script
  9. home=os.path.abspath(os.path.dirname(__file__))
  10.  
  11. #sandboxdir is this dir/sandboxname
  12. sandboxdir=os.path.join(os.path.dirname(home),sandox)
  13.  
  14. #go into the root of the sandbox
  15. os.chdir(sandboxdir)
  16. PST=os.path.join(sandboxdir,‘bin’,‘paster’)
  17. VAR=os.path.join(sandboxdir,‘var’)
  18. ETC=os.path.join(sandboxdir,‘etc’)
  19.  
  20. #make var directory if it doesnt exists
  21. if not os.path.exists(VAR):
  22.     os.mkdir(VAR)
  23.  
  24. print "restarting "+sandbox
  25.  
  26. # Build the system commands required to restart the sandbox
  27. cmds=[]
  28. cmds.append(’source %s’ % os.path.join(sandboxdir,‘bin’,‘activate’))
  29. cmds.append(PST+" serve –daemon –pid-file=%s/sandbox.pid"
  30.             "–log-file=%s/sandbox.log %s/sandbox.ini start" % (VAR,VAR,ETC))
  31. CMD=";".join(cmds)
  32. PRNT(CMD)
  33.  
  34. #run the cmds
  35. if os.system(CMD) == 0:
  36.     print "All done!"
  37.     sys.exit(0)
  38. else:
  39.     print "Ooops. failed."
  40.     sys.exit(1)

The value of tool…

Wednesday, November 19th, 2008

… is greater than zero.

Python is a dynamically, strong typed programming language. In my opinion that makes it more important to have good tools than for statically typed languages. Sadly, it also makes it more difficult to create great code analysis tools and IDEs. Meanwhile, I find more people in the Python community that is happy coding with Vi(m) even for quite large projects, than in the Java community. My guess is that that is because the tool support for Python has been poor.

When your IDE shouts at you with red lines under your function calls because it’s not able to look up in your custom libraries, of course you’ll refuse to accept it, and go back to your simpler editor.

Torbjørn Nordbye implemented the awesome Ruby support for Netbeans, and has now started on the work to give us Python awesomeness in Netbeans as well.

As mentioned in the comments of a previous blog post I have a good example of simple, obvious bugs that could stay in your application for years, without anyone noticing them, because that piece of code run almost never. Bugs that good tools, and/or compilation of staticly typed languages would find for you, but that unittesting and manual testing of your application probably wouldn’t find.

This is a bug that Tor found while testing his Python support for Netbeans, when he pulled in the datetime module that ships with Jython. There, in some exception handling the code references the non-existent class ValuError, which probably should be ValueError. A simple typo that has been undiscovered for a long time.

We all make errors. Good tools make us do less of them, and find them faster.

Static typing VS. Unit tests

Sunday, November 16th, 2008

Okay. It’s kind of stupid to put static typing up against Unit tests. Even so, lots of people does so a lot of times in the discussion between dynamic and static programming languages.

Static typing provides you with a security net when it comes to typos. Alhough, it doesn’t give you any security against logical errors.

(more…)

the right tool for the job

Wednesday, November 12th, 2008

So, you’re going to parse a webpage, to extract some information. For instance if you want to get the tracking information for your last online order, and you want to display the tracking information changes using growl, dbus notifications or xosd.
You know regular expressions, so you go to the job with your long range missiles ready. But wait a minute, you’ll probably solve the problem but is regular expressions really the right tool?
The pro for regular expressions is that you can use the same tool you always use for parsing jobs, but then again you doesn’t learn anything new out of this. You might fortify your position as regex wizard even more, but how about something completely different?

Now. Most webpages is written in HTML, and some even in XHTML, for HTML documents languages like Python has a built-in parser, after the model of the SAX-parser. (It’s probably the other way around, the SAX parser is built on the base of the HTML parser…) Most programming languages has good support for XML, so for XHTML documents, you can use the HTML-parser, a SAX-parser or even the XML-DOM parsers.

The benefit of doing it this way is that your parser will probably be more robust to minor changes in the webpage. You don’t reinvent the wheel (The best way I’ve found to parse HTML documents using regular expressions is to make a specialized SAX-like parser anyway). Your code will probably be readable in a year, and others might even be able to understand your code. And finally, you learn something new, which might give you a fresh view on a lot of problems.

Now back to the original issue, to make a parser for the parcel tracking of your postal service. Here’s an example parsing the shipment tracking page of posten, the norwegian postal service.

(more…)

Unit tests saves the day

Monday, November 10th, 2008

Currently I’m developing a webservice interface for queue handling in Asterisk towards another company, that is developing a CRM solution which will utilize my webservice for Computer Telephony Integration (CTI).

So, today the developer in the other company got such an understanding of the service that he started suggesting changes. A lot of changes in the DTD etc. These changes would imply some changes in the architecture downwards in the system. It is quite scary to start tearing things apart, when you already got something working. That is, if you don’t have a large amount of unittests.

Luckily I had good unittest coverage. So changing stuff was really just a matter of changing the tests, do fit the proposed changes. Test to see where I needed to change something. Do the changes and run the tests again.

It feels very good to have proper knowledge that everything actually works properly after a refactoring like this.

Unit testing is like love. It’ll keep you going when everything else fails. (citation)

Should have guessed it

Wednesday, November 5th, 2008

I have sort of a love/hate relationship with Python, the programming language. Coming from PHP and Java which both have excellent documentation, while lots can be said about the solutions. With Python 2.6 the documentation really has improved, but its still not really there, while the solutions are so elegant that the only reason you don’t guess how to do stuff is that it feels too simple.

Today I had one of these realizations, while reading Wayne’s snippet of the day.

Python has a really nice list-generation scheme, so you can generate lists of the content you want from another list of object with only one line of code.

Example:

  1. >>> from math import floor
  2. >>> a = [1.5, 1.9, 2.5, 3.1, 5.7]
  3. >>> b = [floor(i) for i in a]
  4. >>> print b
  5. [1.0, 1.0, 2.0, 3.0, 5.0]

Here I generated a list of whole numbers from a list of non-whole numbers.

Now We’ll take this further.
Lets say we want to filter the list, so we’ll only get the numbers that satisfy 1<=x<2.

We could do this using filter() and lambda functions. Which is totally OK. It’ll make Python newbies totally confused (which it did with me until recently, when I realized what lambda functions really are (I’ll get back to this)), but it’ll work nicely.

But wouldn’t it be nice if we could use the same list generation scheme for this as well?
Guess what. You can!

  1. >>> a = [1.5, 1.9, 2.5, 3.1, 5.7]
  2. >>> b = [i for i in a if 1<=i and i<2]
  3. >>> print b
  4. [1.5, 1.9]

Doing the same using filter and a lambda function will look like this

  1. >>> a = [1.5, 1.9, 2.5, 3.1, 5.7]
  2. >>> b = filter(lambda x: 1<=x and x<2, a)
  3. >>> print b
  4. [1.5, 1.9]

and without using a lambda function:

  1. >>> def myfilter(x):
  2. >>>     return x: 1<=x and x<2
  3. >>> a = [1.5, 1.9, 2.5, 3.1, 5.7]
  4. >>> b = filter(myfilter, a)
  5. >>> print b
  6. [1.5, 1.9]

All of these will work equally fine, and none of them is very hard to understand, as long as you keep in mind that lambda functions just are like anonymous functions/classes.

But there is something very facinating with the elegance and simplicity in the syntax of the list generator snippet.

Python Magazine, the march issue

Wednesday, April 2nd, 2008

I’ve been a subscriber of Python Magazine since January, and I’ve bought every issue there are on PDF. It is a great magazine, and I would recommend it to every pythonista out there. No, strike that, make that every programmer out there. Because we want them all to be bitten by the snake don’t we?

When people have asked me if there was anything I would complain about with the magazine, I’ve had to answer “That it takes so long time before the paper copy reaches me, that I’ve already read it all on PDF before”.

That’s not completely true. Because I like reading on paper, and waits patiently for the paper copy to fall into my mailbox.

And now It’s also completely false, because the march issue arrived today, about 15 days after it was published online, and about a week after the february issue.

So, great work guys! And the article lineup is really cool. I can’t wait to poke around with the flickr and Google Calendar API’s.

I’ve also started playing with the idea that I might write an article for them some day.

How (not) to write Unix Deamons (with python)

Saturday, March 29th, 2008

From pycon08:

Other videos from PyCon08 can be found here

My bookshelf

Sunday, January 30th, 2005

I took a picture of my new laptop the other day. It was then I saw how geeky my bookshelf was, containing these books:

  • Web Database Applications with PHP and MySQL
  • Learning XML
  • Learning XSLT
  • Python - how to program
  • MySQL reference manual
  • Programming Languages - concepts and constructs
  • HTML 4.01 Specification
  • Conputer Networks
  • A history of modern computing
  • A brief history of the future - The origins of the Internet
  • Learning Java
  • Data Structures and Algorithms in Java
  • Designing with web standards
  • On to Java
  • Java network programming and distributed computing
  • Windows ME annoyances
  • Java som første programmeringsspråk
  • Java software solutions
  • Learning WML and WMLScript
  • Human computer interaction