SMTP attachment script for vim

November 30th, 2009

It is not uncommon to see ourselves being asked to mail some file or program from someone. So how we do that? We open it in vim to see if it is the right file, then open your web browser or any other mail client, click on Compose, Attach, then Browse through the directory tree to locate the file, finally click send! Uh, that is too pains-taking!

Let us attack the problem at its root. When you look at the file in vim, you are sure that it needs to be sent, why not send directly from vim, without going through all these ordeal? Yeah, it is possible! Here is how we can do that with vim script and some python.

I wrote 2 versions for this: one that uses vim-python package (which allows python in vim scripting directly), another is a standalone vim script that invokes an external python program to get the job done.

It will be good to have the vim-python package, so just install it with:


sudo apt-get install vim-python

Even if you are not willing to do that, you can use my second script; in any case you would need python!
You can grab the first script from here. You need to change 3 things there:
username = “yourusername”
password = “yourpassword”
smtpserver = “yoursmtpserver”
By default it has smtp server for gmail. And that needs authentication. So, put your gmail username and password there. If you want to try a different smtp server, modify these parameters accordingly. For more customization look at the comments in the code.

Put that in some place so that vim will source it at the start. The simplest way is to append it to your .vimrc file.

And here is how you use it. Open a file you want to send. Say you want to send to that to 3 people whose address are, xyz@abc.com,a123@example.com,vim@myvim.com with subject, “Agenda”, do:


:Send xyz@abc.com,a123@example.com,vim@myvim.com Agenda

And you are done! See how simple it is! Vim rocks! :)

Giving the Subject to the mail is optional. If omitted file name is used a subject. Also note that there should be no space before or after the comma in the recipient list.

Now for those of you who wouldn’t take the pain of installing vim-python, here is the way. You would need two codes:

  • A python code that handles sending attachment. Click here to get it.
  • A vim script that invokes the python code. Click here to get it.

Grab the python code. As said above, set the correct username, password, smtpserver etc in the source code. Now, grab the vim script, again as said above put in some place so that it will be sourced at the start.
Put the python file in some place so that it could be invoked like: “python /path/to/file.py” or may be add a chmod a+x to that file, and allow it to be executed as: “/path/to/file.py”.
Note down how you would execute in terminal from anywhere and put that value in the vim script as:

let cmd = "python /path/to/file.py"

Usage is same as the first script.

Hope you will find this useful. Feel free to comment about the script or report any bug or any feature request. Happy vimming!

Python lib for Google Transliteration

November 9th, 2009

Ever since I got acquainted with Google transliteration, I wanted to do something more than just using it in a browser. So finally ended up coding a python lib for that. Click here to download it. Here is how you use it.

rajeshsr@debian:~$python
Python 2.7a0 (trunk:75700, Oct 26 2009, 02:53:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Transliterate
>>> x = Transliterate.Transliteration('ta')
>>> print x.getTransliteration('vankkam vandhanam')
வணக்கம் வந்தனம்

You can also use it as vim filter. You can also make some small changes to the code to make transliteration work offline by caching the results. My code already has a cache variable doing that. Just pickle it. Just let your imagination fly to see what you can do with it.

The entire process taught me a lot. First Google doesn’t expose anything more than JavaScript in order to access transliteration as opposed to translation for which they provide a GET method. So I was searching to see if I could embed some JavaScript engine into python. But that seemed a too heavy solution and I wasn’t inclined to explore a lot of totally new libraries for just this! :) And began to Google, search at IRC and ask people about it.

Finally I got a nice suggestion from one of my friends. He told me to make the HTTP request directly without any JS. This idea was quite new to me. Am not so used to thinking of XMLHttpRequest as yet another HTTP request even when the name says so! ;-) I then used firebug to trace the requests made by Google. And analyzed the results by writing some “stubby” python codes. It really took a good deal of research to figure out how Google operates.

Some of the observations I made are:

  • You can use: http://www.google.com/transliterate/ directly for all languages
  • Google makes a request for each word you want to transliterate
  • Google optimizes the processing time by sending a cache of commonly used words
  • Terminal rendering of tamil fonts is really abysmal, at least in lenny

This process also taught me something about unicodes and got my feet wet into JSON.

The way I organized the library may not be good. Please be kind to me! Am quite new to python. Feel free to comment on anything about my code. By doing so you are helping me become a better programmer.
If you find this library useful by any means, do shoot me a mail. Humans love nothing more than being appreciated, and am no exception to that! ;-)

Happy hacking! :)

The Story of My Experiments with Twitter and Python!

October 26th, 2009

I have recently got my twitter account renovated. Though I have registered long back in twitter, my first tweet was lodged only last week. Initially, I wasn’t much impressed with the idea of twitter, despite being an avid Gmail Status Message freak, which I believe comes close to twitter model. Still, I decided to force myself to give it a try. The hype around it was enough to convince me to take up the enterprise of maintaining an active twitter account! :-P

I really got hooked to it, when I began to get more active with exploring its various features. It was simply awesome to see what many people are upto and what is trendy today. One of the main methods of sharing that seems pervasive across all of twitter is recommending URLs. It is extremely amusing to see things like, what people feel about Windows 7 to what Linus did with it! :) Of course, you can get the same if you are subscribed with slashdot or any other tech-geek sites(you have a better name?!). But twitter stands out in being more interactive, you can get a more personalized recommendations from people like you!

Being given to coding, it is quite natural that I would look to doing something with twitter APIs! So started to ruminate for an interesting problem to work on and came with the problem of designing an app that can parse the tweets and extract the URLs from it.

The rationale why I undertook this is, from the day I started tweeting I was overwhelmed by a lot of URL recommendations, so I thought it may be nice to organize them well. But I wasn’t able to design a neat model for this till now. Anyway, when you wanna learn swimming you dive into water without giving thought. Same applies to coding! Plunge into it, when you are not clear about how you will do it; at least with the projects you have imposed for yourself. Too much of time spent in designing can lead you to lose interest. Better design may come as you iterate more. That is just my opinion and it is prone to evolve as I get more experienced.

First comes the decision of programming language. I chose to code in Python! I have recently been playing around with it and like many other language freaks, I began to fall into love with it. Also it will be a good coding exercise to do this in Python, therby getting acquainted with more exotic language constructs and learn at least a weeny bit of its colossal library.

Now come deciding on what data to work on. From where will I get the tweets? On some cursory exploration, I found that twitter gives a nice RSS feed for user tweets. But they are not of much use, when you want to have a global outlook of what trends today. Twitter offers a nice search, through which you can get information about any topic that may interest you, and the nice thing is that they provide feeds even for that search.
What is more easy than extracting something from an already structured feed! So I set out to code it by getting the tweets from the feeds.

What I finally ended up was an extremely crude model for extracting and displaying URLs. You can get the code here. Just extract it, and add your favorite feeds to the file ‘rss’.
The format is: “Topic Name” == “Feed URL”.
It already contains some feeds to give you some example. Run ‘python parse.py’. It creates a file called ‘index.html’ in the current directory which contains the extracted URL. Let that python script keep running, while you browse ‘index.html’ or stop it and re-run it when you want to update ‘index.html’.

Well, I had completely forgotten about my original intention of doing this task– to get familiar with twitter APIs! Even I was surprised to realize that twitter APIs were redundant for my task. May be I need to figure out some other task, that will need me to learn twitter APIs! :)
Anyway, this simple task was quite enlightening. I began to appreciate python more than ever, for its brevity, beautiful library etc!

Here goes some questions to you. What you think about an URL extractor for twitter? An obvious feature anyone would like to have is, providing more clues about each URL and archiving the URLs somewhere. Anything else? I will try to code, if you give me your requirements.
And here is a more important question, do you think it would be of any avail more than a feed reader? If so, how? What would be needed to make it better? If not, just mention that a feed reader is enough. I will take it as a serious vote!

And feel free to put some feature request here. The only guideline about feature request I would like to give is, don’t make any feasibility analysis of your idea; Conscious thinking can curb creativity! Just brainstorm. You can put it here as comments or shoot me a mail at srrajesh1989 AT gmail DOT com.

And yeah, feel free to add me on twitter! :-)

Brainf*cking Deepavali!!!

October 17th, 2009

I have been receiving a lot of Deppavali wishes, which are unique of its own; either in the design of the greeting or the way it is presented! So I thought, why not produce something different from these wishes. I had been thinking what would befit an unconventional yet interesting Deepavali greeting, which culminated in this:

1)+++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++.+++++++++++++++
++++++++++.+++++++++++++++..+++++++++.>++++++
++++++++++++++++++++++++++.++++++++++++++++++
++++++++++++++++++.++++++++++++++++++++++++++
+++++++..<---------.>----.<++++++.>.+++++++++
++.---.

Here are more greetings that wishes you elaborately!! ;)

2)+++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++.+++++++++++++++
++++++++++.+++++++++++++++..+++++++++.>++++++
++++++++++++++++++++++++++.++++++++++++++++++
++++++++++++++++++.++++++++++++++++++++++++++
+++++++..<---------.>----.<++++++.>.+++++++++
++.---.>+++++++++++++++++++++++++++++++++...-
.++++++++++++++++++++++++++.-----------------.


3)+++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++.
++++++++++++++++++.++++++++++.-----------.>++
++++++++++++++++++++++++++++++.<-------------
--.++++++++++++++++++++++.++++++.>.<---------
-----------.>.+++++++++++++++++++++++++++++++
+++++++++.+++++++++++++++++++++++++.+++++++++
++++++..+++++++++.>++++++++++++++++++++++++++
++++++.<------------------------.<+++++++++++
++.>+++.>.<--------------------.<++++.---.+++
+.---.>+++++++++++++++++++++.+++++++++++++.<-
.>+++.--.>.<---------------------------------
--------------.++++++++++++++++++++++++++++++
+++..<+.>----.<++++++.>.+++++++++++.---.>+...
-.++++++++++++++++++++++++++.----------------
-.


4)+++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++.
++++++++++++++++++.++++++++++.-----------.---
---------------------------------------------
------------------------.++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++.+++++++
+++++++++++++++.++++++.----------------------
---------------------------------------------
------------------.++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++.-----
---------------------------------------------
---------------.+++++++++++++++++++++++++++++
+++++++++++.+++++++++++++++++++++++++.+++++++
++++++++..+++++++++.>++++++++++++++++++++++++
++++++++++++++++++++.------------.+++++++++++
+++++++++++++++++++++++++++++++++++++.<------
-.---.++++.---.>+++++++++++++++++++++.+++++++
++++++.<-.>+++.--.>++++++++++++++++++++++++++
++++++.<------------------.<-.>+++.>.<-------
-------------.<+.---..+++++++++.-.-----------
.++++++.-.>----------------------------------
-.+++++++++++++++++++++++++.<++++.>++++++++++
+++++++++++++++++++++..>.<-------------------
--------------.++++++++++++++++++++++++++++++
+++..<--.>----.<++++++.>.+++++++++++.---.>+..
.-.++++++++++++++++++++++++++.---------------
--.

What are those?!! An extremely unintelligible script! Worse than random zeros and ones! Are we impugned with some cryptographical challenge?! May be such thougths are looming into your mind. Don’t worry! I will explicate more on this. Before that, congratulations to those of you who are able to identify that it is just a BrainF*ck code!!! :p

For those of you who don’t know what BrainF*ck is, you can look at the wiki. Briefly, it is a language that you can learn in less than 10 minutes but may take beyond your life time to master! :)

Try to satiate your curiosity by running the above code with some Brainf*ck interpreter or may be write your own! Writing an interpreter for Brainf*ck is possibly easier job, than say write your first “Hello World” code in it! So many idiosyncrasies, in a simple language! :)

Ok, the above codes produce respectively the following outputs:
1) Happy Deepavali
2) Happy Deepavali!!! :)
Thats my style! I use a lot of smileys while chatting and now with Brainf*ck codes too!
3) Wish You a Happy and Prosperous Deepavali!!! :)
4) Wish You a Happy, Prosperous and Pollution-Free Deepavali!!! :)

Yeah, Pollution-Free that needs to be accentuated more than anything else! Ok, Let us not digress.

Have I succeded in projecting to your mind that I am a BrainF*ck pro?! :p Then lemme extricate myself from your misconception, before you begin to think deep! How do you think I would have done that?! Taking a piece of paper or may be an editor, keeping track of contents of all cells and finally generated the code after a tedious process?! Haha, ok, you have got it right! I did it like that!! :)
Hmm, those of you who know me still doubt if I ever did that, for I even haver to a division involving 2 big numbers! Ok, am gonna make a bold confession! I didn’t do like that!

Yeah, I took a different path to doing this. So how would I have done that? Any guesses?!

Ok, here goes the answer. Metaprogramming to the rescue! I wrote a C++ code to search for a Brainf*ck code that will do the job. If this shall make you even more curious, I wouldn’t pester you anymore by keeping you on tenterhooks! Here goes the code:

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 using namespace std;
 5 
 6 string str;
 7 int mxSz = 0;
 8 string dum;
 9 
10 void getVal(vector<unsigned char> vec, int cV, int ind)
11 {
12     if(dum.size() > mxSz) return;
13     if(ind == str.size())
14     {
15         cerr << dum << endl << flush;
16         mxSz = dum.size();
17         return;
18     }
19     unsigned char& cnt = vec[cV];
20     string t = dum;
21 
22     while(str[ind] > cnt)
23         cnt++, dum += "+";
24     while(str[ind] < cnt)
25         cnt--, dum += ("-");
26     dum += ".";
27 
28     getVal(vec, cV, ind + 1);
29 
30     if(cV + 1 < vec.size())
31     {
32         dum += ">", getVal(vec, cV + 1, ind + 1);
33         dum.erase(dum.end() - 1);
34     }
35     if(cV >= 1)
36     {
37         dum += "<", getVal(vec, cV - 1, ind + 1);
38         dum.erase(dum.end() - 1);
39     }
40     dum = t;
41 }
42 
43 int main()
44 {
45     getline(cin, str);
46     int cnt = 0;
47     for(int i = 0; i < str.size(); ++i)
48     {
49         while(str[i] > cnt)
50             cnt++, printf("+"), ++mxSz;
51         while(str[i] < cnt)
52             cnt--, printf("-"), ++mxSz;
53         printf(".");
54         ++mxSz;
55     }
56     puts("");
57     vector<unsigned char> vec(4);
58     getVal(vec, 0, 0);
59 }

Feel free to use the above code, and if possible give me a credit! :)

The code is a simple backtracking to generate a Brainf*ck code that would print the input string. I am interested in seeing the shortest possible code. But I make some assumptions:

1. I don’t use any of the Brainf*ck idioms involving operators [ and ]. For example, to copy the value to an adjacent cell etc. That may reduce the size of the code. That is, I consider a subset of Brainf*ck allowing only: +, -, <, >, .
Here after when I reference an arbitaray Brainf*ck code, I am referring to this subset only unless otherwise stated.
Note that ‘,’ used anywhere here is just a separator or a punctuation. It has nothing to do with the Brainf*ck operator “,”.

2. Given that I don’t use any Brainf*ck idioms, another assumption I make is, when you move to a cell, it makes no sense not to use the cell to produce character. That is my program doesn’t search Brainf*ck codes which may have “>>” or “<<" as substrings. But this may be proved wrong; for a long enough input string an optimal code may consist of the above strings. For example, an imput string of the form "*rAZr*", ( * represents an arbitrary sequence of letters ) it may be optimal to have a "<<" after printing AZ. Still I am not sure about whether it is true or not.
More formally:
While searching for an optimal(shortest) Brainf*ck code, is it enough to search across a language that doesn't contain ">>+” or “<<+" ?
where '+' is the regexp operator while '>‘,’>’ belong to the alphabet of the language.

Am not sure if am excluding any other valid Brainf*ck language apart from what I said. If I do, please do tell me.

I have also imposed a restriction on the number of cells that may be used by the output Brainf*ck code, the maximum being set to 4. And my program has output optimal answer for the first 3 strings. For the last one I have put the optimal program I have got till now; it is till running and I will update if I get a more optimal program.

Another important thing you may notice is that the state formulation renders itself to a dynamic programming problem, but I did not memoize the result and am solving the same sub-problem again. But full memoization is impossible considering the prohibitive number of states for using the 4 cells and I have traded it off with liberal use of computing time.

Some note on the Brainf*ck code generated above:
1) Uses 2 cells
2) Uses 3 cells
3) Uses 3 cells
4) Uses 3 cells

The first 3 codes seems to be optimal possible with respect to the language I search. As I said the third code is still running and I am in no way sure about its optimality.

At this point I would like to make a formal definition of a Brainf*ck task of printing a sequence of characters being K-cell optimal. What I mean by a Brainf*ck task being K-cell optimal is that, the shortest possible code has to invariable use K-cells, if it uses (K+i) cells or (K – j) cells for all positive ‘i’ and 1 <= j < K then it cannot be optimal. That is let us define a function F(L) which denotes the length of the shortest possible code "using" exactly L cells. Then F(L) should be ternary searchable and has a minimum value for some 'K'. The question of whether F(L) satisfies this condition is not rigorously clear, though intutition seems to be suggesting that.

Even if it fails to satisfy that condition the definition of "K-cell" optimality can be extended to denote a set of values, using any of which you can get a globally optimal(shortest) code. Obviously it is clear that, if such a set exists then it should be finite, because the number of characters of code using just 1 cell is finite and gives an upper bound on the shortest possible code.
What I mean by "using" L cells is that you should have visited the first L cells.
Note that the formalizations I have made are quite general and doesn't lose their validity in searching for the shortest arbitrary Brainf*ck code.

The reason why I introduce these notions is that, I want to claim that the first 3 programs are respectively, 2-cell, 3-cell and 3-cell optimal with respect to the model I search. No claim yet on the 4th program! Any comments here is welcome.

Now about the shortness of the code. Here goes the length of the program for each of the Brainf*ck codes:
1)275
2)359
3)675
4)1171
It makes no sense to claim that those are the shortest possible codes, Brainf*ck idioms can make it shorter. May be I can say it is the shortest in a model allowing only: +, -, <, >, .

Another interesting question worth investigating will be, how will you come up with a code corresponding to F(L), defined above or at least find the value of F(L) even in my restricted model. Is this problem NP-Hard?! I guess so, but am awaiting a rigorous proof. It is polynomial obviously for L = 1. I guess it can be polynomial for L = 2 also, by some greedy strategy, but am yet to explore on that. What about an arbitray L?
If you have any thought on this please feel free to share!

If you have read this far, thank you very much!

Happy Deepavali!!! :)

About me!

September 9th, 2009

Before you immerse yourself in my vast world of deep meditation, it may be essential to be informed about me! ;)

To describe me well, there exists a beautiful word – a “philosopher”! haha, well not the sort of sorcerer, that would loom into your mind on hearing this word! Literally, philosopher means lover of knowledge. That’s exactly what I am! I love to learn a lot. My interests sprawls across Mathematics, Computer programming and Algorithms (well that’s what am doing as an undergrad!), Physics, History, Philosophy, Psychology and even Human Anatomy! It is both a blessing and a curse in disguise. Since everything interest me, I would read about it all and probably that would be branded as a waste of time, for diverse reading is mostly superficial. Still I strive to strike a balance. With all fairness, I must confide that these days am more preocuppied with Computer and Maths while occassionally reading other subjects. But still it’s a bliss to learn, ponder and finally correlate between apparently disparate topics.

The purpose of having this blog is sharing my ideas. Sharing not to advertise, but to be questioned, dissuaded, to get more insights into alternative views etc. As I keep musing, I will post on a variety of topics, some philosophical, some technical, some psychological etc. So feel free to comment on that and fight with me, if you think am thinking wrong! I will also try to post some interesting technical articles, tutorials etc. as I believe I learn a lot by posting what I know!

Hope this blogs gets more interesting and useful.:)