Newspeak

August 6, 2008

Week 3: Vim tricks - Indenting Code

Filed under: vim — Stephen Paulger @ 9:28 am

For a long time now I have known how to indent using Vim’s ‘>’ command to indent a block of text. I have learnt a few new tricks that might speed me up a bit. (more…)

July 29, 2008

Week 2: Vim tricks - Automatic code completion

Filed under: tech, vim — Stephen Paulger @ 4:20 pm

You may have used an IDE before that allows you to automatically complete parts of your code. In Microsoft’s Visual Studio this is called “intellisense”. It’s very handy for speeding up development and for exploring objects that you can’t quite remember the attributes of. This week I found out how to add it to Vim. (more…)

July 21, 2008

New Vim tricks

Filed under: vim — Stephen Paulger @ 5:05 pm

I’ve been using vim as my main text editor for over 5 years now and because I’m now dependent on it and using it every day at work I’ve decided to challenge myself to learn a new trick every week. Last week’s trick was sorting lines.

Sorting Lines

I actually learnt two new tricks to be able to do this. The first is selecting lines using visual mode.

By placing the cursor on the first line you wish to select then pressing Shift+V and moving the cursor the last line to select you select the range of lines between the two and you can do any of your normal ex commands on just the selected text.

To sort the selected lines type :sort, this runs the sort command and replaces the selection with the result. You notice if you try to sort lines with numbers they will be sorted alphanumerically, ie. 700 would come before 8, if you want to sort numerically use :sort n. You can also remove duplicates from a list by using :sort u, to remove duplicates and sort numerically use :sort un.

March 7, 2008

Optional arguments in Python

Filed under: code, tech — Tags: — Stephen Paulger @ 11:22 am

Optional arguments in python are obviously very useful allowing you to have default values so you don’t need to pass them every time. A simple hello world example always goes a long way…

def hello(to="world"):
    print "Hello,", to
>>> hello("Steve")
Hello, Steve
>>> hello()
Hello, world

Sometimes you’ll see people define an optional argument with a default value of None then override it in the function’s body. This might seem a strange thing to do at first, but there is a reason. Let’s say that we wanted to be able to say hello to a list of people and for it to automatically add “World” to the list.

def hello(to=[]):
    to.append("World")
    if len(to) > 1:
        greeting = "Hello, " + ", ".join(to[:-1]) + " and " + to[-1]
    else:
        greeting = "Hello, " + to[0]
    print greeting
>>> hello(["Steve", "Matt"])
Hello, Steve, Matt and World
>>> hello(["Steve", "Matt"])
Hello, Steve, Matt and World
>>> hello()
Hello, World
>>> hello()
Hello, World and World
>>> hello()
Hello, World, World and World

This happens because the value for the default argument is the same list every time, so when we modify it the function has the same modified list as the default argument when it is subsequently called. This is probably not the behaviour you would expect and you would rarely want to use this behaviour. The way around it is to define the default argument as None then override it.

def hello(to=None):
    to = to or []
    to.append("World")
    if len(to) > 1:
        greeting = "Hello, " + ", ".join(to[:-1]) + " and " + to[-1]
    else:
        greeting = "Hello, " + to[0]
    print greeting

>>> hello()
Hello, World

>>> hello()
Hello, World

>>> hello(["Steve", "Matt"])
Hello, Steve, Matt and World

>>> hello(["Steve", "Matt"])
Hello, Steve, Matt and World

Because None evaluates to false, setting the default value to None means it can be easily overridden to give us the default value we actually want if there wasn’t a passed value.

February 19, 2008

Dw i’n dysgu Cymraeg.

Filed under: uncategorized — Stephen Paulger @ 1:57 pm

S’mae!

Yesterday evening I attended my first Welsh lesson with the University of Wales, Bangor. We were given a sheet with resources online including a link for downloading a Welsh spell checker, nothing for Open Source users or Linux though. There are things out there for Welsh learners and speakers in the open source world though. (more…)

December 18, 2007

MI6 Google Ad Recruitment

Filed under: uncategorized — Stephen Paulger @ 10:11 am

I wonder if this is targeted advertising and they actually want people like me.

November 27, 2007

Economists Say the American Dollar Still Has Sentimental Value.

Filed under: uncategorized — Stephen Paulger @ 10:17 pm




Thank you ulti for the title

« Newer PostsOlder Posts »

Powered by WordPress