me, thinking out loud…

Posts tagged ‘note’

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.

Advertisements

Some fun from #org-mode

2010-10-02 Sat 20:26

This is a conversation from #org-mode, published without permission from plovs, BerntH and bremner. If any of you want any of the content removed, I will do so.

<punchagan> hi all

<BerntH> hi punchagan [18:36]

<punchagan> BerntH: ever used/tried blorg or blorgit? [18:47]

<punchagan> I like the way org-publish works, but blorg exports one org file as a blog. If blorg were to be re-written on top of org-publish, how should it be done? use one file per post or just one file for the whole blog? [18:52]

<bremner> punchagan: another option is to use ikiwiki and use the org-mode plugin to render pages. This is what i do, although only a bit of the site is in org. [19:44]

<punchagan> bremner: can I have a look at your site? [19:45]

<bremner> sure, it ain’t pretty, but: http://www.cs.unb.ca/~bremner [19:46]

<punchagan> thanks. I’ll keep this in mind. [19:47]

<punchagan> I just loved the way this site looks and behaves – http://julien.danjou.info/projects.html

<bremner> well, the side bar at least is possible with ikiwiki. Other than that I suppose it is mainly a matter of css [19:48]

<BerntH> punchagan: nope [19:49]

<punchagan> ok BerntH [19:50]

<plovs> punchagan ikiwiki does have an org-mode plugin [19:56]

<punchagan> plovs: yes, bremner told me that and I’ve seen on Worg too. :)

<plovs> punchagan ah, yes, sorry

<punchagan> plovs: it’s alright. [19:59]

<plovs> punchagan another possibility is org2blog, which uses wordpress

<punchagan> plovs: I’m the author of it. :D

<plovs> lol, ik, that punchagan [20:17]

<punchagan> lol

<punchagan> plovs: were you kidding? or serious? [20:19]

<plovs> punchagan sorry to say i was serious, but it will not happen again :-)

<plovs> i really like org2blog, played with it yesterday

<punchagan> plovs: no. I was just wondering if you were playing around with me. :P

<punchagan> I’m happy some one thinks it is useful.

<plovs> lol, no, i just didn’t recognized your name, although it is kind of hard to miss [20:21]

<punchagan> I hope you don’t mind, if I make this anecdote public?

Asking vs. Figuring out

Should one take the easy route of asking someone who knows, if she is sitting right next to you? Or should we try and find out first, before asking her? Should one value his time more than others’ or others’ time more than his own?

Would we learn more if we tried to find out ourselves? Does learning more, make sense? Is it worth spending the extra time?

I’m just talking of simple things that you could figure out by spending a couple of minutes of searching the web or 5 minutes of thinking or 10 minutes of reading a book.

Happy Teachers’ Day!

Well, it turned out to be not so Happy. I came to know of this sad news. A prof published his students’ work as his own, with no mention of their names! This guy is such a shame to his profession! Shame!

I wonder how this guy has even been able to sleep every night. Isn’t there something called a conscience?

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*.

Contributing to FOSS

2010-08-19 Thu 00:06

gora asked for some good links (on ##linux-india) to redirect newbies, enthusiastic about contributing to FOSS.

I dug up a few links from the web – some that I read, when I was starting off with using GNU/Linux and FOSS, others are newer links.

Just making a note of them here; hoping someone would find it useful.

  • How to Become a Hacker – A definitive how-to on becoming a hacker. Answers all the right questions. :)
  • Philosophy of the GNU Project – This is the page, where I started. Thanks to Voodoo for giving me the link to this page. I distinctly remember that day. :) I’m going to pass on this link to any newbie who comes my way, no matter how much bashing I get from other FOSS evangelists.
  • 6 Easy Ways To Get Started Programming Open Source – A neat list giving steps to get started with programming for open source. I like #4. Understanding the social dynamics of the project is an important thing which is often overlooked by newbies.
  • OpenHatch – A community site to help newbies get started with contributing to FOSS. A cool site. Also, here is a story about OpenHatch.
  • Look at the Numbers! – an interesting paper giving quantitative data in support of using FOSS.
  • Ubuntu – Code of Conduct – A nice description of general conduct expected in any of the FOSS communities, not just Ubuntu.

I’d be happy to add any other links that any of you share. Thanks!

One Hundred

2010-08-17 Tue 20:18

The previous post, was the One Hundredth post on this blog. As 9 suggested, I just want to say Yay! and thank my readers!

I’d like to thank all the people who gave me those small nudges to write something down here and hope at least one person has found something useful here.

Thanks to all the people dropping in here, regularly; Bearing with whatever sort of stuff I put up here. :)

Thanks to everybody who took the time to comment here.

Special thanks to niku, Rini, and ntveem for inspiring me with their blogs. [All three of them have moved on to better things than blogging.]

Tag Cloud

%d bloggers like this: