Tuesday, September 18, 2012

Calling external Python function from a C/C++ routine

An aricle by Jum Du at CodeProject (see: http://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I) gives a very detailed overview of how to call a Python function from C/C++ routines in an embedded interpreter using CPython interface. While this quite useful, however, one needs to keep the .py file at the same place where the C/C++ executable resides.

I needed a solution where the .py file could reside anywhere on the local file system. Turns out that the modification is quite simple, you just need to make sure that sys.path is appended with the correct path at runtime where the .py file can be found. The follwing is the pseudo(C++)-code of how I do this:

PyObject* runFunction(std::string scriptFilePath, std::string funcName, PyObject *arglist)
{
    // this code is based on http://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I
    try {
        PyObject *pName, *pModule, *pDict, *pFunc, *pValue = Py_None;
        std::string thePath = // .. your code to extract the path, for '/home/ganeshv/pyfiles/my.py' : path is '/home/ganeshv/pyfiles/'
        std::string theModule =  // .. your code to extract the module, for '/home/ganeshv/pyfiles/my.py' : module is 'my'
        // printf("importing [%s] from [%s]\n", theModule.c_str(), thePath.c_str());
        // first extract the file name and file path
        std::string code = "sys.path.append(\"" + thePath + "\")\n";
        // add the path
        PyRun_SimpleString(code.c_str());
        // Build the name object
        pName = PyString_FromString(theModule.c_str());
        if (pName == Py_None) return Py_None;
        // Load the module object
        pModule = PyImport_Import(pName);
        if (pModule == Py_None) {
            Py_DECREF(pName);
            return Py_None;
        }
        // pDict is a borrowed reference
        pDict = PyModule_GetDict(pModule);
        if (pDict == Py_None) {
            Py_DECREF(pModule);
            Py_DECREF(pName);
            return Py_None;
        }
        // pFunc is also a borrowed reference
        pFunc = PyDict_GetItemString(pDict, funcName.c_str());
        if (PyCallable_Check(pFunc)) {
            pValue = PyObject_CallObject(pFunc, arglist);
        } else {
            PyErr_Print();
        } // end if
        // Clean up
        Py_DECREF(pModule);
        Py_DECREF(pName);
        return pValue;
    } catch(...) {
        return Py_None;
    }
}

Saturday, September 15, 2012

The Growth of Online Computer Science Programs

Today’s contributor, Olivia Leonardi is a writer and researcher for a website that helps students decide between computer programming schools and programs. However, this post focuses on alternative avenues of learning web skills as they are increasingly viable and, many would argue, a better way to acquire these skills. Besides, some of the most influential figures in computer science like Alan Turing, a man discussed by this blog, did not have access to traditional programs and turned out alright.

The Growth of Online Computer Science Programs

As technology has become more prevalent in our society, numerous online education opportunities have been made available for aspiring computer scientists. While these programs offer a number of improvements over the traditional college experience, for instance the ability to be creative in a noncompetitive environment, many educational experts argue that there are drawbacks to this format as well. For this reason, each student is encouraged to consider many factors before deciding between web-based curricula or brick-and-mortar studies.

Students today can choose from a myriad of online computer science programs from traditional and elite American universities. Many online bachelor’s and master’s programs mimic the traditional curricular structure of college degree paths and allow students access to university resources. According to Education-Portal, the finest online CompSci programs are offered by the University of Illinois at Champaign-Urbana, UCLA and Florida State University.
However, many feel the confines of the ivory tower and inadequate in teaching students skills that require, above all else, creativity and thinking outside of the box.
If a student wishes to forgo the degree path entirely, they can still learn a particular skill related to computer science as many prominent colleges and universities offer free online tutorials and a growing number of open source universities have been springing up. Schools such as Stanford University, Massachusetts Institute of Technology and Carnegie-Mellon University offer complimentary courses that introduce code languages, complex operating systems, software applications and other CompSci-related fields. Non-university-related programs have also been explosively popular and give students access to communities of aspiring programmers to bounce ideas off of. For instance, Codecademy (which was launched one year ago) allows students to sign up for free classes and learn about various technological concepts at their own pace while boasting an online community of more than 100,000 students.

According to CBT Planet, online computer science programs offer several benefits to students. Most of these programs are structured to be flexible, and can provide the ideal educational platform for people with other commitments (such as full-time jobs or families). Cost is another factor; while online degree programs are not free, they tend to be much less expensive than traditional college tuition. Finally, the wide range of available programs allows each prospective student to find a program that best matches his or her experience and skill set. For this reason, e-learning can be an effective for novices hoping to learn the basics or experienced individuals who wish to learn a supplemental skill or competency.

However, writes Jacquie Berry of Education Training Info, web-based programs also have notable drawbacks. Independently structured courses, while convenient, can also work against students who do not exercise a high degree of self-discipline. Another disadvantage of learning from home is the lack of opportunities for face to face interaction with classmates, professors and other individuals one encounters on a college campus. This can lead to frustration and anxiety among students if they are unable to grasp certain concepts, and then have no one to consult face-to-face. Some programs have attempted to mitigate this deficit by developing courses with an interactive element; some include video tutorials from professors, forums that enable students to communicate with one another and various forms of multimedia. Still, studies show, many students prefer real-life interaction with teachers and students.

In order to get the most out of computer science education, each student must determine which educational factors are most important. Some thrive within the online setting (and greatly appreciate the flexible schedule these programs afford), while others prefer to learn within a more structured, face-to-face environment. Everyone has a different college experience – but careful consideration of where and how one wishes to earn a degree is the surest way to achieve career goals in the long-term.

---
Editor's Note:
The views expressed as solely of Olivia, who kindly agreed to share them in this blog. I would like mention that many on-line courses such as Coursera (https://www.coursera.org/) are also helpful for students who have been less fortunate of finding quality institute and teachers. ACM has recently published a white paper on the topic and is available at http://www.acm.org/education/WhitePaperOnlineFinal.pdf

Sunday, September 02, 2012

Where is my Nexus S?


Ok, this is funny. I got this email (see above for content) yesterday from no-reply@android.com (looking at the headers they seem quite genuine). But the fact is that I never purchased an Nexus S, and by no means it is available in India. I just hope that this is some kind of an error, and not something billed to my card :( Hopefully Google / Samsung can confirm that this is an email sent by error... until then fingers crossed!

Random thoughts: 24hours without Nokia Lumia


Last week, I had to visit some 'high security zone' where no communication device is allowed in the premises. So I decided to shutoff my Lumia for 24 hours (of the grid) and use another basic Nokia device: the 1280 (which is usually used by my parents), while commuting from and to this high security place. Before going into this place though, I had to hand over my Nokia 1280 to a colleague, who himself was carrying a Lumia 710. That Lumia device and the Nokia Drive were extremely useful to get to many places that day, but essentially for me, I was completely free from grid and my constant companion device. In the retrospect, I thoroughly enjoyed the day and felt no urge what-so-ever of my missing Lumia 800, partly because my colleague was anyway using one when it was needed.

During this off-grid time, it was fun observing how radically different the UI paradigms of Nokia Lumia and Nokia 1280 are. While Nokia 1280 is one of the most basic phones available (monochrome screen), Nokia Lumia 800 is one of the Nokia's flagship smartphones running Windows Phone OS. A while ago, I had written an article on Kosh (building a mobile user experience for myself, part I : http://tovganesh.blogspot.in/2012/01/kosh-building-mobile-user-experience.html ), where I had written about how unintuitive it is to use the most basic function expected form a phone : to make a phone call; on all of the smartphone interfaces available today. I have time and again noticed this with my parents: they never got around being comfortable using Android (even when I had put the contacts on the home screen), they are somewhat comfortable with using my Lumia 800 (probably because of the bigger screen, again pinned contacts are needed, the phone app still looks a bit unintuitive), but they are very comfortable and happy to use Nokia 1280. The phone does one thing extremely correct : make and receive phone calls. When making the phone call, you just need to press the numbers using the keypad and click the call button done. For Lumia, you need to open the phone app and then key in the numbers, so is true for any other smartphone OS. This in itself is a major roadblock. Take the other case: receive phone call. On Nokia 1280, you just need to press the receive button and bingo, start talking. On most smartphone (including Lumia), you have to use some kind of a swipe gesture. These gestures may be so intuitive and cool for most of us (they certainly are for me and I feel comfortable using them), but with my parents this is just a burden. For this is more complex than just clicking a button or lifting the receiver of the landline. In fact, my parents are much more comfortable with landline than the mobile phone. Surely there is a lot of scope for improving the user experience of mobile communication devices.

That brings me to the UI paradigm used in new Asha touch series phones (Asha 305 and Asha 311): the three screens and the drop down. One of these screens is actually the phone app, which is also accessible from the drop down menu. This is so close to what I had enlisted in the Kosh UI design : things like phone (and for that matter any other form of communication) should be easily accessible at any point in time of using the device. To some extent this is what Windows Phone with its Live Tiles help to bring in, but again stop short of integrating other elements like the ones available with the new Asha touch OS. (PS: Just for a thought, I think, the Smarterphone acquisition http://www.itwire.com/it-industry-news/strategy/52030-nokia-nabs-another-mobile-os-buys-smarterphone, played a great role is a substantial transformation of the S40 OS in just a very short period of time).