Monday, 3 October 2011

Finally, I have my hands on them

It had been so long, since I started to think about buying a book titled "The C Programming Language" by Bryan W. Kernighan and Dennis M. Ritchie. If I put it as accurately I can as possible now a days, It'll be 3 year after I learned how to program (Obviously, In C, we initiated). This book is a standard reference for "C" language till the date, a gift from authors of the language, themselves. It wasn't available at local book stalls. (I do think, that's the case, even now.)

The other book shown here, Is called "Hacker's Delight.". Well, it's no immediate use to me. But I liked the content of that Book. It's a bible of Bit level Algorithms and techniques. What I already know about bit arithmetic become nothing in front of this Excellent book. A must have, If you want to know your machine.
Want to become a Embedded expert and so on. 

And If I say, these are first Computer Science books that I'm buying with my own money and Out of requirements by academic needs. And I already have a copy of e-version of these books, long back. I just needed a printed version, to serve as a quick reference.

Also, It's which made my long desire to earn these books possible. Their delivery is very good and on time. Packaged really well, with a book-mark-let & bill.

What I use daily is Python, I love to code in python, think in python, (not eating python, never tried, or haven't got a chance ;-) ). If  you come to know any book about python worth buying as reference, and is small like these two listed here ( I don't want to buy a {Java, ...} "Black Book" or {C++, ..}"Complete Reference" kind of mammoth book. And most likely I may already having a e-book of your suggestion. What I need is a strong reason to have that book in print.

By the way, one of this books are worth owing (if not both). Go ahead, have it handy...

Tuesday, 20 September 2011

Software Engineering - Continues refinement

Every programmer wishes to write some code, which is both elegant, and readable. A master piece to reference to...
And there may not be a single programmer, who said at least once in their life time that, "If I had time, I'd rewrite it" or any similar one.

Today, let's discuss, how the idea of sorting a list of list went on,  

I had to sort a list of lists, that's the result of a search, comes from Web Service. After that, the result is extracted from the XML document that's returned and formatted for front end. So, now all the data is in the form of strings. And I'm ended up with the task of sorting a list of list of strings, based on different items in the inner lists @ different times. And Obviously we can't compare the data as strings, we have to convert them into their proper data type before comparison.

Since, I works on a Python based project, First thing, that came to mind is to use, sorted inbuilt function, and pass it a function to compare. But the new way of doing this pass a function to key argument of sorted instead of cmp argument.

This is a bad idea, that we are doing array sub-scripting on the lambda itself. Which is a lot inflexible. A quick visit to python wiki showed me, how to convert a cmp to key argument. This is more flexible, Now I can tell, which column to sort, how to convert that particular column to proper data type etc.
Now, compare() is a very simple function, which I don't have to define separately, so I put that into cmp_to_key() function as a default parameter, a lambda.
But, hold on, Why should we create a lambda, if we can do that comparison directly, without creating a function first? So, our lambda got removed and comparison is performed directly on __lt__ special function
Ok, now we are in good shape. Now, we can shift our focus into, How to specify a a function, which will convert a string in the lists to a proper data type. I'm using DataTables jQuery plugin for client side display of paginating table. So, what I get back to server to determine column is a number. So, I made a list, which I can map to in sorting function based on which column to sort.

Here, we are making a lot of repetitions, with DRY principle in mind, I decided to normalize those, like Database normalizations, you can say. But there's another reason for the decision, we have another type of searches, whose return records varies, and in effect will create a lot of duplicates of anonymous functions which does same thing. So here goes the solution

Here goes final solution, bear in mind this is simplified version, I removed all the complexities and simplifications involved, because of Classes, packages, etc., which is there in real system. But, I do believe, this shows how a piece of code evolved to solve a problem, And why it took time and knowledge to become perfect / or near perfect.
NB: sorted function uses a condition at end to assign a value to reverse, which is actually a Boolean. That single statement alone undergone a lot of transformation in the process. Well, that's for another blog entry.

Tuesday, 6 September 2011

Start on Microchip programming... for hobby or for money

One of my friend asked me today the following question, 
I'm often asked about this, someway or other.
Let me answer this now for all....

Q: "I want to start programming on chip.. Can you suggest a good chip and a device to program it? Also tell me any sites which can help me."

A:"Simple one is Arduino. You will get it packaged with a programmer.
If you want some more powerful and commercial one, Go for Microchip's PIC family of processors. After you are familiar with those, and need even more power, try AVR from ATMEL."

Some resources from my Bookmarks is given below:
My bookmarks become so messy now a days, and I'm not getting time to organize them.
So, these are the quickest ones that I picked up from it. I'll let you know, if I found some others.
My old Book mark is here at
you may find it useful."

Wednesday, 11 May 2011

Tic Tac Toe in python - Just for fun

The other day, I was looking for some GNU/Linux administration reference, I ended up viewing a "tic tac toe" program in Ruby at a Linux admin's blog.

He wrote that in ruby and took him 90mins. That was written using class.
But "Zen of Python in mind", I wrote my own version without class.
I prefer this way (sparse is better than dense), and I enjoy Python more when, I think of my Java days. 
I haven't read ruby code before hand so, I did not wrote a clone of ruby code.
And this took me around 40min. Because, I realized some missing features only when I played it several times.
Then only I added those.
Specifically, drawing complete board after someone wins and stopping the game when all fields are filled out.

I'll not say, the program is beautiful and very readable.
However, it's good enough for playing with it.

Friday, 15 April 2011

Structures: C -> Java -> Python -> Erlang

With C we had structures and unions.
I like them, because, I don't always need to embed behaviors with data(class).

So you'll do something like

With Java, I'm forced to use classes, because that's how java is structured.
They are purely object oriented. (eg: in C++)

In python, you can take java's approach; but since Python is not Java ;-) and we
tend to write less,
We'll use dictionaries (or hashes in Perl)

I'm a newbie to earlang (and to functional programming),
I think there's no dictionaries, but tuples.

Here's how we use tuples to achieve same (also applicable to python)

much like json, isn't it?
notice, there are no quotes around someone, that's an atom (this is erlang dude).

This in python:
Well, how do we understand what's what?

hmm, ok, we can tag them with atoms, (this is a programming practice in erlang world)
so our earlier example becomes,

That's it.
You can do this with python too, like as I've shown below;
(but I personally prefer the use of dictionaries, as I've shown before)
But don't try to write programs in earlnag way in python, why?
Python got dicts, it's just an overkill to use tuples like this.
Also, in earlang, there're some language specific features that we can make use of.

In Earlang, we extarct values using pattern matching operator,

 2nd line will cause error.

it'll fail with an error. see, how it matches atoms at LHS and RHS.
(in python, we don't have this feature, since '=' is just an assignment operator as every
other object oriented/proceedural programmer think it is.)
we can use this in many interesting ways, for example, to distuinguish different
data structures at runtime and extarct them properly.

Just for the sake of it, I'll show another method to extract values called
"unification", which is useful for selective extarction; here it is...
But differntially from object oriented approch, we no more had a template now.
We'll use functions to create and parse these dictionaries and tuples.
(Pythonists like to write more functions than classes, because, python make it
really unncessary to write classes always, and we have 'Zen of python'.)

Monday, 4 April 2011

My Desktop setup

Here goes my desktop.

Operating System: GNU/Linux
Flavour: Ubuntu (Debian Derivative)
Version: 10.10 (kept up to date)
Hardware(striped down): Intel C2D, 4GB, 320GB. 
Manufacture & Model: Toshiba Satellite Pro.

Desktop Manager: Gnome
Compositing Manager: Compiz
Gnome-panel : set to be hidden always and removed all that I can from the panel. (ie., panel is empty now, but we can't remove last panel completely.)
Icon ThemeFenza Dark. (see link for ppa)
Menu: Cardapio Menu (using ppa) it even have awn applet. 

Other gtk themes in use: Elegent, Equinox, A new hope.
Other Icon theme in use: awoken
Cursor theme: X11-Gear (Sorry, I can't find exact package now, but it's there)
File Manager: nautilus-elementary  (nautilus isn't complete with this, sleek elegant look,breadcrumbs, inbuilt terminal, clutter view) and make use of extra pane and tabs ;)
File Preview: Gloobus preview

My Conky Configuration File

Edit :                        (Aug 17, 2013)
All my configurations including conky's are now hosted at

I've been hearing about this conky for a long time.
I think I tried it once in my 5 years of linux experience.
But, I haven't experienced it till last day.

Conky is a system monitor originally based on torsmo! What the heck is this "torsomo"?
Honestly, I don't know. Le me google that for you....

Torsmo is a system monitor that sits in the corner of your desktop. It's very simple, customizable and it renders only text on the desktop (and percentagebars if you want it to ;) and the only lib it uses is Xlib.
Torsmo can show various information about your system and it's peripherals, including:
  1. Kernel version
  2. Uptime
  3. System time
  4. Network interface information
  5. Memory and swap usage
  6. Hostname
  7. Machine, i686 for example
  8. System name, Linux for example
  9. Temperatures from i2c-sensors
  10. Temperature from ACPI
  11. Battery capacity from ACPI/APM
  12. Number of processes running or sleeping
  13. Local mails (unread and all)
  14. Filesystem stats

 Enough, right?
Ok, Now what's conky?

Conky seems to fit in the same reign as of torsmo, but with more awesomeness, customizability and lots of inbuilt variables(almost 300) to access almost all features of system. Even bindings for POP, IMAP, music players like mpd, moc, audacious...
You can use Lua scripts to customize it and use cairo to draw whatever way you wish...

Enough said, I don't need another resource hog, well, you are wrong, conky is designed to be lightweight. 

search for conky themes various configurations. 

OK, Now goes my custom conky configuration file. I customized it the last day.
You can mix and match it however you like it.
You can see the result here.

Looks nice, how can I try this,

All that's easy, if you are on linux.

See conky documentation on how to install it on your system.

After that install .conkyrc from here

And place, ".conkyrc" on your home directory.

More on readme file.

Thursday, 6 January 2011

AJAX File Upload with Web2py

It was not that long, since I experienced a problem while trying to upload a file using an ajax  trapped form. I thought, it must be me doing something wrong. I was using web2py to embed another page into a page via ajax. That is better known to web2py folk as LOADing a component.

It's just happened that one of such component contains a file upload form. It was my first time using LOAD function provided by web2py. Basically it make use of jQuery to load the page via ajax into a target div and traps input of any form in that page, so that page doesn't reload. Oh, I forgot to say that web2py is bundled with jQuery.

It's always boring and tedious to understand a problem without experiencing it. So, Let's play with an example, (PS: I"m using web2py a full stack python framework, but you can use any language at server side and this problem will be there because, it's a problem with ajax)

My model which defines table like this,

In RDBMS world, it column 'file' of table 'image' will transformed to a type of CHAR(or VARCHAR) and column must not be empty (notnull)
Web2py can enforce this at many levels.
notnull = True is enforced by database
required = True is enforced by DAL (database abstraction layer) of web2py
requires = IS_NOT_EMPTY() is enforced by SQLFORM.
You are free to use any of them and it's up to you to use all of them or only one of them. (we give choice :-) )

Now, the index function which maps a url and renders output

As you see web2py automates almost everything (yes, you can customize everything, and do it manually, if you want). This code generate an upload form as per database table IMAGE. automatically upload file into database, and if there's errors shows them to the user. After successful upload page is redirected to /upload/default/index.html. Wow, Magick! :-)

Now go to myform.html > You will be greeted with a file upload form and it just work.

OK, now we decided to embed myform.html into another page(say index.html).

For that we wrote {{=LOAD(URL(c='default', f='myform.load'))}} in index.html and it got transformed to:

for non-web2py folks, URL function generate url, and here .load is served with a content type of text/html. By using .load extension page is rendered without any template we set up for regular html display. The url /example/default/myform.load is of course a relative url, it get interpreted as Here 'example' is application, 'default' is controller and myform is a function. ie., will map to a function called myform() in a file in an application called example and rendered based on extension(.load, .html, .json, .xml etc).

I can't figure out what's going on. It always showed an error message stating that file is empty.
And I fire up my browser's developer plugins and it become clear that file is not being sent to server via ajax. Yes, it's because ajax can't sent files (read XMLHTTP object).

There are two remedies for this (as far as I can think of)
1) use an iframe to load component. Since there is no AJAX there, it'll work.
this is as simple as writing {{=IFRAME(_src=URL(c='default', f='myform.load'))}} in your view. (here index.html).

But, I hate iframe you may say (whether you say it or not, I'll). I learned a lesson in past that users care about functionality and not a bit about whether we use this or that and follows standard. So, I'm ready to break some if that cause better usability. (So I used iframe). Don't be disappointed, I have another option for you folks,

2) Use one of many ajax file upload plugins. I am going to show a solution based on uploadify library. It uses swfupload. and jQuery. So, if you really want to avoid iframe and burden users by making them download few more dependencies download uploadify library and extarct it into your server. (That said, this library is great if you want to implement multi-file upload). By using uploadify, you are going to do DB insert yourself. (if that's OK go ahead). Extract uploadify download into a folder called 'uploadify' under static directory.

change myform function to:

However, I tried it without embedding into another page, but it'll work even if it's embedded.
There are many different plugins available to do the same, each differs in their approach(like submitting form to an embed iframe.
So take your time to find them and go through them. (So server side code may change depending on your library of choice).
Please look these libraries:

  1. valums ajax upload
  2. jQuery Form plugin

Happy experimentations  :)