Lambdas are fun

One of the questions from The Insider's Guide to Python Interviewing cracked my mind a little bit. Although, Python is not the language you expect to shoot in your own leg consider this example:

fl = []
for i in range(3):
    fl.append(lambda: i)

[f() for f in fl] #what is the output?

This code iterates over a list generated by range and adds three function objects to the empty list named fl. Since the last item cursor will hit in range(3) is 2, the value each lambda will return is 2. It's the tricky part of the question. But what if you would like to use a conventional iterator cursor - i.
[i() for i in fl]

>>>[<function __main__.<lambda>>,
 <function __main__.<lambda>>,
 <function __main__.<lambda>>]

>>><function __main__.<lambda>>

Lambda functions try to recursively call themselves, because the global value of i was overwritten in list comprehension. Same will happen if the value of global scoped i was modified by looping beforehand.

Leave a Comment

python lambda scope

April 22, 2014

Pydoc Online updated to

Pydoc Online for Eclipse was updated to version

What's new:

Since new Eclipse have made some changes for third party plugins, you should put Pydoc Online to dropins directory. Rest of activating plugin steps are the same as in Howto.


At the moment available on SourceForge.

Leave a Comment


July 17, 2013

Viber On Linux: Guided Howto

I've played with Viber somewhere in 2012 on Android. It was pretty raw and i didn't like that there was no option of switching vibrate notifications off (still isn't) so i didn't use it much. And one or two weeks ago i discovered, that they pushed desktop versions for Mac and Windows. Nothing about running Viber on Linux. It was decided to make a hit on Windows version of Viber under Wine. And guess what: it works! Viber worked on Ubuntu Linux 12.04 LTS amd64 with Gnome 3 and Wine 1.4.

Step #1: Install Wine.

$sudo apt-get install -y wine

Step #2: Configure Wine.

wi Now, it's time to tell Wine that we want to mimic Windows XP environment behavior.

You should also configure hardware if you want to make voice calls.

Step #3: Download Windows version Of Viber and install it.

Then run it in terminal
$ wine ViberSetup.exe
Or you can right-click mouse button and choose "Open with Wine Windows Program Loader".

Setup fetches Viber over the network and populates two files on your Desktop. You are encouraged to remove them. Then fire up Viber by Alt+F2 or Super hotkey (Gnome 3 & Unity-style).

Now, all you have to do is to type your phone number and the service will send you the activation code via your Viber mobile. Start it, go to Output and Input section and configure your hardware accordingly to your Wine settings.

Voila, you have Viber on Linux! Instant messaging works fine and i was able to make a voice call without any complexities. Keep in mind, that running in Wine doesn't give you 100% stable application that won't crash.

Leave a Comment

viber linux ubuntu howto

May 13, 2013

Hacking Chrome's Most Visited Page

Ok, Chrome/Chromium has that Opera-like feature called Speed Dial. The really annoying thing is that Chrome's developers made a decision to show thumbnails based on user browsing behavior. So i can't just add the site i want, but ought to browse it very hard to convince Chrome it is of value for me. Then i can pin it and exhale with relief. Or i can add on of those fancy addons that require some customization and additionally waste my RAM. Older versions have had a trick with modifying Preferences file with JSON-like synthax, but now all thumbnail information is stored in sqlite database called Top Sites. If you have fresh-installed version of Chrome it would be easier to perform the hack. First of all, you will need sqlitebrowser. On .deb based GNU/Linux systems it is achievable with:

$sudo apt-get install -y sqlitebrowser
After that go to Google Chrome configuration directory. For Chromium it will have the same path except the chromium dirname instead of google-chrome.
$cd /home/gdmka/.config/google-chrome/Default
$sqlitebrowser Top\ \Sites
Next, going to Browse Data tab and opening the table called 'thumbnails'. Let's do a little research on thumbnails table: url - is an actual link to the webpage; url_rank is the position of the url in list; title - is the descritpion below the thumbnail; thumbnail - is blob, cached images of the webpage; boring_score - according to the comment from source code it is 'Higher boring scores indicate that a higher percentage of a bitmap are all the same brightness (most likely the same color)' at_top - 'Whether this thumbnail was taken while the renderer was displaying the top of the page'. Other field names are pretty much self-consistent.

Now, there's two options to fill the table with needful urls: writing raw sql in Execute SQL tab or working with the graphical inferface. Since graphical is way faster let's add some record with it. Keep in mind that you can always examine SQL done to the table using the Log button.

Now firing up Google Chrome.

It worked, newly added bookmark is now in the Most Visited. Now, you can add all 8 websites you want. Good practice is to add urls properties with GUI and save generated SQL code for later reusage.

Leave a Comment


April 21, 2013

Extract vowels from English alphabet. Functional approach.

One day while looking through ASCII set table a very persistent idea stuck into my head. The essence of it was: if it is possible to assign integer values to alphabet letters like ASCII, is it possible to extract all vowels from it using numeral calculations? That thought was so annoying that i couldn't resist to do some personal research. First of all, the alphabet itself is a positional notation:

>>letters = 'abcdefghijklmnopqrstuvwxyz'
>>pos = dict(zip(xrange(1,27), letters))
>>print pos
>{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
Based on that one can see, that there is a precise step between vowels in the english alphabet.
a = 1, e = 5, i = 9, o = 15, u = 21
Looks like an arithmetic series, where a = 1, e = a+4, i = e+4...

But hey, there's two problems:
  1. From the 3rd element the step changes to six.
  2. "y" is considered a vowel in some cases. See this article on Wikipedia.
And the more proper set will look like:
a = 1, e = 5, i = 9, o = 15, u = 21, y = 25
So what we actually have here is a polynome, but let's stick with the coding part. More formal solution will look like this:
#ASCII representation of english lowercase alphabet
>>alphabet = [chr(i) for i in xrange(97,123)]
>['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
Note that to we use list comprehension to iterate over a set of numbers from 97 to 122 (which are codepoints of lowercase ASCII alphabet) and converting each of them with built-in function chr Next thing is more complex:
#generate a list of vowels
>>vowels = map(lambda x:chr(x),reduce(lambda acc, i: acc + [acc[-1] + i], (4,4,6,6,4), [97]))
>['a', 'e', 'i', 'o', 'u', 'y']
Such functional creatures should be read from right to left. So first we should get a list of integers that represent vowels:
>>codepoints = reduce(lambda acc, i: acc + [acc[-1] + i], (4,4,6,6,4), [97])
>[97, 101, 105, 111, 117, 121]
Right after that the high-order function map takes lambda function that converts integers to letters and applies it to the sequence. And getting a list of consolants becomes that simple:
>>consolants = [i for i in alphabet if i not in vowels]
>['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z']
We can use our own dictionary to test the function:
>>print pos
>>{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
>>vowlz = map(lambda x:pos[x],reduce(lambda acc, i: acc + [acc[-1] + i], (4,4,6,6,4), [1]))
>>print vowlz
>['a', 'e', 'i', 'o', 'u', 'y']
But hey, the vowel function looks pretty raw, because codepoint start is hardcoded and some use-case may not consider "y" as vowel. That's correct and lambda will be useful interface to fix that:
>>vowels1 = lambda step, start: map(lambda x:chr(x),reduce(lambda acc, i: acc + [acc[-1] + i], [4,4,6,6,step], [start]))
>>vowels1(step=4, start=65)
>['A', 'E', 'I', 'O', 'U', 'Y']

Leave a Comment

alphabet algorithms python functional_programming

April 13, 2013

Pages: 1 2 >>
Powered by Django. Blog was developed using JetBrains PyCharm.