2008-08-03

CaveMan Programming: Smooge Learns Python

Ok I will admit that I love to write little scripts and stuff, but haven't really progressed in my programming from writing Pascal and BASIC programs of my teenage years.. I see myself as being very much Oook the programmer. Oook gets up, fights T-Rex mailserver, tries to learn new cave painting programming language, and then Oook goes off to slap a Bronto-DNS server and forgets cave-paintings. My long term programming goals are:

  1. Go beyond bourne/awk/sed and learn a modern language like python. [My perl never progressed beyond Perl-4.]
  2. Write a configuration management language using Python and Puppets syntax.. called Ruppet (pronounced with a Scooby-Doo lisp).
  3. Write a hack like game in Java for telephones using a RPG system I am designing for Paper and Pencils.
Well I am still on step 1, and learning lots of things people probably know and will get a kick out of seeing Oook learn. Oook is working seeing what average dice-rolls would be from rolling an openended (eg if a dice rolls a magic number you roll again and add). Oook had done this in awk, but decides he will try new cave paintings called python.

Todays lesson: for loops can use memory.

LOTS of memory if you don't know what you are doing. That is because they aren't your C loop... they are a foreach loop. So when you do a

dicerolls = 10
for i in range(0,dicerolls):
blah open-ended rolling blah

That doesn't look very interesting.. or use a lot of memory... but when Oook changes the number of dicerolls from 10 to 1 billion.. the 32 bit system gets OOM and Oook looks and wonders what went wrong. So Ook goes to 100 million, and starts the open-ended simulation again... Oook then hears disk drives spinning and wonders what is going on.. Oook brings up a top and sees that 100% of the CPU and 1.5 GB of ram is being used by a Python process. Oook then goes to browser to look up why python use so much memory.. his earlier awk script didnt... but browser take long time and disk drive work harder.. Oook remember to kill dice program and tries again. Oook learns that for expands everything out so he had created a list of 1 billion (Oook puts finger to side of mouth when he says that) and that was a lot of memory...

Oook thinks for a bit.. Oook reads about lambdas and other things but does not think he is ready for that level of Witch Doctor Magic. Oook reads web-page further and finds that he could use while loop.

dicerolls = 1000000000
count = 0
while (count < dicerolls):
blah blah blah
count = count + 1

Oook watches diceroll come up and sees that it matches what he wants.. Oook happy. Oook goes back to Bronto DNS server that decided to eat Oooks IP address.