me, thinking out loud…

Archive for the ‘ology’ Category

Augmented assignment in Python

If you are new to Python, you should probably stop reading here. But, if you have used Python and numpy, then read on. Before, that try these bits of code.

import numpy
a = numpy.array([1,2])
a = a + 0.5j 
print a
[ 1.+0.5j  2.+0.5j]

The “same thing”, in a slightly different way.

import numpy
a = numpy.array([1,2])
a += 0.5j 
print a
[1 2]

Both the code blocks, look really the same, until you look carefully. Under normal circumstances a = a + b and a += b behave exactly similarly, and we really don’t need to bother about the differences between them.

But, +=, which is an augmented assignment operator, actually tries to perform the operation in-place, unlike the other statement where + actually returns a new object which is again being referenced by the name a.

But, when dealing with numpy arrays, this will lead to trouble. When assigning to an array, it’s dtype is not changed and hence the trouble.

The right way to use the augmented assignment operator, would be:

import numpy
a = numpy.array([1,2], dtype=complex)
a += 0.5j 
print a
[ 1.+0.5j  2.+0.5j]

The same thing is explained in this thread. Also, Thanks to Bhanukiran for asking me this.

Notes from PyCon

Just back after attending PyCon India ’10. It was not as exciting as I hoped it would be. That’s generally the case with any conference I attend. (I guess, I expect too much from them. :P)

The keynote by David Goodger (pronounced like Badger :) was “Good”. It was a very simple one talking of how to get Python into the workplace. His simple recommendation was to use Python if we saw any opportunity where it could be used, without bothering about permissions, convincing people etc. “It’s better to ask for forgiveness, than for permission.” He spoke of myths around Python — scripting language, dynamic language, too much white space, toy language, nobody uses it. He concluded the talk, by saying mentioning Indian driving to be an indication of some quick reflexes that we Indians have. ;) On the whole it was an enlightening, humorous and enjoyable talk.

I didn’t attend too many of other talks. Amongst the ones that I did attend, I particularly liked the one by Asim Mittal on using the Nintendo Wii with Python.

On some thought about my experiences at conferences, I think conferences shouldn’t have any talks at all. Or atleast, I shouldn’t be attending conferences for talks. They are an excuse to catch-up with people. Conferences should have only lightning talks of 10 mins and sprints. Talks, with an extensive explanation of stuff that can be easily found on the web, are a waste of time.

A couple of counting functions

I had a strict character limit of 180 chars for something I was writing. I just wrote a simple function to count characters in a region or a buffer. Another function to count the words.

(defun count-chars ()
  "Count the number of chars in a buffer or region."
  (let* ((beg (if (region-active-p) (region-beginning) (point-min)))
         (end (if (region-active-p) (region-end) (point-max))))
         (message (number-to-string (- end beg)))))
(defun count-words ()
  "Count the number of words in a buffer or region."
  (let* ((beg (if (region-active-p) (region-beginning) (point-min)))
         (end (if (region-active-p) (region-end) (point-max)))
         (count 0))
      (goto-char beg)
      (while (< (point) end)
        (setq count (1+ count))))
    (message (int-to-string count))))

A GNU and a friend

I now have a GNU! I bought it (a GNU soft toy) in memory of my friend swathanthran from #emacs-in. RIP swathanthran

As a bonus, I got the chance to have a photograph with just RMS and me. Also, hoping the monetary contribution will be of some use to FSF-India.

The thrill of getting the GNU (and the GNU itself) and shaking hands, getting photographed with RMS is dedicated to all the lafoots and specially to Voodoo, who taught me the meaning of the word “hacker”.

Happy Hacking!

ps: I just hope and pray nobody else does anything as stupid and shocking as what swathanthran did.

Richard Stallman in IIT-Bombay

RMS will be in IIT-Bombay on the 6th of this month. He will “speak about the goals and philosophy of the Free Software Movement, and the status and history of the GNU operating system, which in combination with the kernel Linux is now used by tens of millions of users world-wide. ”

  • Date: 6th September, 2010
  • Time: 8:30 P.M
  • Venue: PC Saxena Auditorium, IIT Bombay

org2blog on the web

Well, it’s been a week of lull on my blog. Not that I’m very busy. But, just didn’t have anything to post. I was feeling jobless today and searched the web for org2blog users. Here is what I found.

org/elisp – sorting list of org-links on description

2010-08-20 Fri 00:23

A small utility function that I used to sort the names of people who commented on my blog.

(defun org-get-link-desc-from-list ()
  """ Get link description of a list item containing just links """
  (let* ((item-beg (point))
       (item-end (org-end-of-item)) 
       (cur-item (buffer-substring-no-properties
                  item-beg item-end)))
    (goto-char item-beg)
    (org-columns-compact-links cur-item)))

sort-lines wasn’t good enough for me, since it was sorting alphabetically and it ended up being the sorted order of the urls and not the names. This function, when used with org-sort-entries-or-items gave me what I wanted.

Thanks to benny (on #org-mode), I also learnt the difference between let and let*.

Tag Cloud

%d bloggers like this: