Tracing the socket
Well I possess little knowledge with respect to socket programming but a recv call failed with the error message “resource temporarily unavailable”. Now how do I go about fixing this? Well with my limited knowledge of sockets and socket programming it would be helpful to fix it if I could know what exactly is happening at the back, step by step, but how do I do that? Read the hell lot socket programming documentation? Thats out of choice, I don’t have enough time for this. So strace is the first thing to cross my head. Bingo! get strace of the process and just walk through it.
Now in the strace output, I first check if socket is created properly and client is able to connect to it successfully. Hell! connection to the socket fails:
connect(0, {sa_family=AF_FILE, path=”/var/run/nscd/socket”}, 110) = -1 ENOENT (No such file or directory)
Pretty apparent from the message and error ENOENT description(The socket referred to by the pathname in name does not exist.), I check for the path /var/run/nscd/socket. Path does not exist on the system. Checked a little further and no “nscd” process in running on it. Ensure that “nscd” is installed and running on the system. Now, with nscd running on the system, connection to socket is established but to my surprise real issue persists
recvfrom(0, 0x7fffe431de70, 5, 64, 0, 0) = -1 EAGAIN (Resource temporarily unavailable) <0.000008>
How do I fix this now? Checking the details for error EAGAIN, I get its description as below:
The socket is marked non-blocking and the receive operation would block, or a receive timeout had been set and the timeout expired before data was received. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.
Now I know the possibilities for this error, but what exactly is triggering it. Clueless of the reason behind this, I look into the code now. recv() has been called with the flag MSG_DONTWAIT. Whoot!
MSG_DONTWAIT enables non-blocking operation; if the operation would block, the call fails with the error EAGAIN or EWOULDBLOCK
Now what should be the correct replacement for this, with a little bit of research, I find an alternate flag for recv() MSG_WAITALL.
This flag requests that the operation block until the full request is satisfied. However, the call may still return less data than requested if a signal is caught, an error or disconnect occurs, or the next data to be received is of a different type than that returned.
Bingo! I got a hit. With nscd running on the system and recv() called with flag MSG_WAITALL, its running smooth now.
Automated localization
I’ve never done localization of any application myself but I’ve fair enough idea of how it is done. Basically the process involves two major things
- Extraction of text strings from the software
- Generation of translated documentation
In the first part,
- A .POT file is generated which contains all the strings in your application which needs to be translated.
- .po file is generated for a specific language code
.po file contains a filled-in ‘msgid‘ for each instrumented string and an empty ‘msgstr‘ to hold each translation. Now what is alarming here is that normally they edit the .po file manualy to provide the actual translation of each msgstr into the target language. Oh gross ! you start acting like a translator than an engineer/programmer. I find it a sheer waste of time.
I wondered how nice it would have been to have an inbuilt library to do the word to word translation and avoid the unnecessary manual process. Then first thing that comes to mind is “Google Translator“. I search for the APIs to find that its available in AJAX. Now what would you do if yours isn’t a web application. Find a way to call the AJAX methods through the language you are using. Thankfully I find the module “simplejson” in python to my rescue. Now write a python script to call the translate API or find it here.
import urllib
import simplejson
baseUrl = "http://ajax.googleapis.com/ajax/services/language/translate"
def getSplits(text,splitLength=4500):
'''
Translate Api has a limit on length of text(4500 characters) that can be translated at once,
'''
return (text[index:index+splitLength] for index in xrange(0,len(text),splitLength))
def translate(text,src='', to='en'):
'''
A Python Wrapper for Google AJAX Language API:
* Uses Google Language Detection, in cases source language is not provided with the source text
* Splits up text if it's longer then 4500 characters, as a limit put up by the API
'''
params = ({'langpair': '%s|%s' % (src, to),
'v': '1.0'
})
retText=''
for text in getSplits(text):
params['q'] = text
resp = simplejson.load(urllib.urlopen('%s' % (baseUrl), data = urllib.urlencode(params)))
try:
retText += resp['responseData']['translatedText']
except:
raise
return retText
Now call the translate method with text to be translated, source language code & destination language code.
Once .po file is ready with translation, compile it to get the .mo file and you are done. Localization made easy
Recursion gotcha
Well as programmers, we make use of recursion as and when need arises and it fits well within our requirement. Well we do take into consideration, the depth upto which it has to keep recurring but do we think of the recursion depth your compiler is ready to take. Frankly I never botherd till date, dint even thought of such an issue.
For python, default recursion depth limit is 1000. Haven’t yet explored for other languages. So beware while using recursive method calls and incase your recursive calls are exceeding the count of 1000, better change the depth limit explicitly. Add the following lines to avoid the error:“RuntimeError: maximum recursion depth exceeded”.
import sys
sys.setrecursionlimit(x)
where x is an integral value as per your need(say 1500).
Psyco prototype
Python programmer’s dont need to get psyched with the performance issue. Psyco prototype is there for your relief. Its a representation-based Just-in-time specialization for python, which claims to make your python code run as fast as C.
Get the packages from project’s home page, install & add the following lines to your code.
import psyco psyco.full()
To know more, follow the link.
gnowsys-mode
Pencils down declared for GSoC 2009 and here I am to pen down my thoughts/experience. At the start of the project I had visualized it something different but at the end it turned up as much better an application. All thanks to Nagarjuna sir & his great ideas. Since beginning he has been inspiring me to use org-mode features & seriously at the end I feel its the beauty of org-mode that makes gnowsys-mode so simple and yet so pretty.
“gnowsys-mode” is an extension package for Emacs, a text editor. Motivation of the project is collaborative knowledge networking & organization. As a major-mode for Emacs, it dispels the common axiom, that collaborative authoring can be done only through web applications but not from a text editor.
This dialect of knowledge networking offers a prospect of robustness to the shared data, applying GNOWSYS rules of inferences to it. It provides a plethora which lets you semantically conceptualize information/knowledge with the ease of a traditional text editor. Peers using gnowsys-mode may agree on a shared conceptualization of a knowledge domain in the form of ontology, and collectively maintain it over time. Versioning feature of GNOWSYS allows change tracking by creating snapshots & maintaining metadata details of each snapshot.
Adhering to the concepts of semantic web, GNOWSYS allows to convert the knowledge node in RDF tripples. So any knowledge node being created or viewed in gnowsys-mode can also be viewed in respective RDF N3 format. It also enables to visualize the knowledge networks graphically, in the form of concept map.
It is implemented on top of Org mode, which helps to give a formal structure to knowledge network in the form of drawers and also makes it possible to navigate through the network in the text buffer using the node links.
Though Emacs is well known as an editor, gnowsys-mode doesn’t restrict you to work on your desktop but also gives you the freedom to work on a remote GNOWSYS server. We have hosted a server http://sandboxat.gnowledge.org , which is default URL prompted to establish connection with. This is created for the testing purpose, so feel free to connect to it and work around. Or install GNOWSYS on your machine using installer provided here.
To get a live demo on how to use gnowsys-mode, watch the videos provided here.
Get gnowsys-mode from svn repository using:
svn co svn://svn.sv.gnu.org/gnowsys/gnowsys-mode
Pymacs
Pymacs, a powerful tool for two-way communication between Emacs Lisp and Python. Beauty of the application is just too good and saved my life just in time. There is enough documentation on pymacs here. But I felt thats not enough to use it in your application within script. So just taking down a few important kinks out of my understanding.
If you are using it on the elisp side, then load the python library using pymacs-load. Now to call a function of that module, you need to specify the function name as “module-function” i.e module which you loaded followed by the function name, joining the two using “-”.
Note : wherever “_” (underscore) appears in python delimiters, replcae it with “-” (hyphen) in elisp.
Now if you are using it on python side, merely import the required module from Pymacs. Now use “.” (dot) operator to call the functions i.e say you import lisp from Pymacs, now to call function point() do it as lisp.point()
Note : wherever “-” (hyphen) appears in elisp delimiters, replace it with “_” (underscore) in python.