Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, March 18, 2009

One-liners

Python doesn't seem to be the language you'd immediately turn to if you wanted a one-liner to gazump your programmer buddies (well, at least it isn't for me), but can it be done? You betcha:

Lambda hell.


No way am I going to type this out! Especially as someone has already done this for us. :D

Edit: Forgot to mention who alerted me this monstrosity.

Wednesday, March 4, 2009

NDSTrim

Finally, a new post! Yay me, for drudging up enough silly stuff to write a post about!

Seriously though, it has been a pretty hectic week (tests, exams, assignments, the usual) so it's a wonder that I have any time left over for blogging. Anyhoos, I thought I would post up something that didn't take too much time to make, and is at least a somewhat fun exercise in Python coding for myself.

So, without further ado, let me present ndstrim.py! For those of you who like to back up their precious NDS games regularly, like I do, space is a consideration, especially if your collection runs into hundreds of games, like mine (I really do have a lot of games, don't ask). What this Python script does is trim your NDS backups (or ROMs) by a factor of up to 50%, depending on the actual size of the game itself.

How can it save that much, you wonder? Well, considering some ROMs might be 70-80 MB, they'll decide to use 128MB ROM cartridges, with the remaining space doing precisely...nothing. The space can add up real quick!

Anyway, the original C++ code was posted on a forum somewhere (I won't tell because I've forgotten where), and I simply took the liberty of translating it to the Bestest Programming Language in The World TM, because I can. If anyone reading this knows the original source of the code, please do tell me and so I can properly thank the guy. :)

I might write up a GUI (with wxPython) if I feel up to it, so Windows aficionados can enjoy the fruits of my work too! If you're impatient for some GUI goodness, go here and get your fix.

Monday, February 23, 2009

List Comprehensions

I love them. They're just so Pythonic.

What they do is provide you a succinct way to describe a list. You know, our favourite little squiggly snakes with [] at the ends. If you haven't the foggiest what a list comprehension is (and you'd be missing out on a lot of fun), here's a little example to get your juices flowing. Let's say, just for argument's sake, you want to represent the following mathematical expression in your program:

M = {x| x in S and x odd}

How would you do that in Java? You would likely go:

ArrayList M = new ArrayList();

for (int x: S) {
  if (x % 2 == 1)
    M.add(x);
}

How utterly clumsy and unPythonic. In Python you'd just do this:

M = [x for x in S if x % 2 == 1]

So easily comprehensible that anyone not well-versed in Python's syntax could probably guess what it is doing, and very, very snug, spanning only one line! (Then again, snakes do have a reputation of having flexible bodies. ;) ) Doesn't it make you say, “If only [insert here your favourite language before you fell in love with the beauty that is Python] had such awesome syntax?”

So next time you have the urge to create a list in your super-duper Python program (and the urge should be often), do it the Python way. Now, please excuse me while I go off to drool at all the posters of Pythonidae in my bedroom ;).