Go to the navigation

elblogg

Posts Tagged ‘programming’

Should have guessed it

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:

?View Code PYTHON
>>> from math import floor
>>> a = [1.5, 1.9, 2.5, 3.1, 5.7]
>>> b = [floor(i) for i in a]
>>> print b
[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!

?View Code PYTHON
&gt;&gt;&gt; a = [1.5, 1.9, 2.5, 3.1, 5.7]
&gt;&gt;&gt; b = [i for i in a if 1<=i and i<2]
&gt;&gt;&gt; print b
[1.5, 1.9]

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

?View Code PYTHON
&gt;&gt;&gt; a = [1.5, 1.9, 2.5, 3.1, 5.7]
&gt;&gt;&gt; b = filter(lambda x: 1<=x and x<2, a)
&gt;&gt;&gt; print b
[1.5, 1.9]

and without using a lambda function:

?View Code PYTHON
&gt;&gt;&gt; def myfilter(x):
&gt;&gt;&gt;     return x: 1<=x and x<2
&gt;&gt;&gt; a = [1.5, 1.9, 2.5, 3.1, 5.7]
&gt;&gt;&gt; b = filter(myfilter, a)
&gt;&gt;&gt; print b
[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

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)

From pycon08:

Other videos from PyCon08 can be found here

99%

Woha! Dette blir en liten skrytepost. Jeg fikk nettopp tilbake evalueringen av min forrige oblig-innlevering i INF101, og fikk 99 av 100 poeng. Jeg fikk ett poeng i trekk fordi jeg hadde glemt å legge ved et kjøreeksempel av programmet. Akkurat det var litt surt. Men 99 av 100 er likevel rimelig ok.

My bookshelf

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

developing

Once I had this dream of beeing a professional developer. I do not dream of that anymore, if I dream of it, it would be a nightmare. This summer i have had a few programming projects and my income this summer is based on them. I want developing to be fun, not a job. It would be better if I had an office where I could get peace to the work, and a limited part of the day where I would have to worry about getting the project done.

The way it have been this summer really makes me think that I dont even want to work with computers. But thats not really the problem. The problem is that acacemic work always take a part of your mind, even when you dont work. Or especially when you dont work. And you need good inspiration to get the work done. If you dont have just the perfect working enviroment you’ll really have to twist your mind to get those good ideas. Not too hot, not to cold, not too much noise, and no relaitves or other people expecting you to be social. There is a reason why people have the occupation that geeks are not capable of beeing social. Some geeks may even try to make this impression on their enviroment, beacause then noone will bother him while he is working.

And people are bothering us when we are working. I believe this is beacause they think that youngsters are just playing games on their computers, and are never doing anything important. When we explain that its important to get this work done they dont understand it. My explaination is just technobabble for them.

Bloggurat Twingly BlogRank Blogglisten