Stackless Python

May 7, 2008

From stackless python’s website
Stackless Python is an enhanced version of the Python programming language. It allows programmers to reap the benefits of thread-based programming without the performance and complexity problems associated with conventional threads. The microthreads that Stackless adds to Python are a cheap and lightweight convenience which can if used properly, give the following benefits:

    Improved program structure.
    More readable code.
    Increased programmer productivity.

Go give it a try!🙂


Anti-aliased fonts in emacs

February 25, 2008

The blocky fonts in emacs is not something any of us would care about (it’s the code that matters), but if (emacs == religion) for you, it’s worth making some effort for the sake of ur eyes. Actually i happened to see emacs in one my fren’s mac, which has anti-aliasing enabled. So wanted to have in my linux too..

Standard procedures are here. For ubuntu, the easier way. The good thing is, emacs started to show unicode characters (Nepali too) after this hack.

emacs.png


Day of the week

February 25, 2008

Well, you can always use mktime() or localtime(), but also conside this elegant code by Tomohiki Sakamoto. I don’t think any shorter is possible..😉


/* 0=sunday */
int dayofweek(int y, int m, int d){
int t[] = {0,3,2,5,0,3,5,1,4,6,2,4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}


C++ assertions

February 20, 2008

A very simple feature of C++ called assertions can be used can in applications that want to safeguard itself from unwanted side-affects such as for e.g., reading/writing values beyond its boundary. Any condition that wants to be checked can be used in assert. The syntax of assert() is:
assert (some_conditional_expression);
for e.g., assert( 1 != 1) [will fail*]
assert( sizeof(int) != sizeof(int) ) [this one too]

The codes below are just a toys, BEWARE! heh.. ~:-D.

Assertions in C++ can be used by a simple statement called ‘assert (some criteria)’. Here’s a quick example.

#include “iostream”
#include “assert.h”

const int Z = 3;
//prime numbers
int prime[Z] = {2, 3, 5};

int main( )
{
int idx = 5;

assert(idx < Z); //will not go past this line because idx is greater than Z
//on my machine it nicely dumps core file*
//Before trying this example, you could disable coredumps if you are using unix-like OS
assert(idx = 0);

//2. comment out the topmost assert statement and see what happens below.
//this is a classic problem in testing where you check for reading/writing beyond the size of an array.
for (int i=0; i < idx; i++)
{
std::cout << &primes[i] < ” << primes[i] << ‘\n’; //if one follows #2, it’ll print garbage value past ‘5’ (the third value in prime[])
// this garbage could be any value that resides next to your prime[]arrays
// memory address and usually _it’s_undefinable_)
}

return 0;
}

One type of assertion-like mechanism used in pre-medievel C++ versions was something like this:

int boundary_23_error_func()
{
return 23; //23, then has to be documented somewhere for devs to be able to make some
//sense out of it and notify the upcalls about it in a meaningful way
//(e.g., a graphics program, a GUI or a webserver etcetera…)
}

int main( )
{

…….

if (idx > Z)
{
return some_boundary_error_code; // or, return boundary_23_error_func();
}

…..
}

I don’t think there’s anything wrong with using this kind of error handling code but when there are many thousand LOC, it quickly becomes a mess and using assert(some_bool_expression) anywhere you feel there should be a error condition check, is _much_^2 easier. Beware, though that it’s not a holy grail of error recovery. You don’t want system to fail just because it coredumped! Meaning, it should not be used in critical infrastructure that requires guaranteed uptime like maybe spaceships modules, aircraft etcetera.

* – on my machine it dumps core. core of a particular process in Unix-like OS contains that process’ memory image before it (abruptly) finishes execution. For more detail refer to core(5).


Scheme song

February 11, 2008

I found this while perusing through the world wide web… Should be familiar to anyone involved with lisp/scheme

I’ve tried to keep the formatting arguably at the expense of readability, If this is hard to read, head over to the place where I found it: http://www.hack.org/mc/texts/scheme-song.scm

Posted by Shriram Krishnamurthi to comp.lang.scheme on January 17,
1996, for Scheme’s twentieth birthday:

                           ((I m a g i n e)
                         (shriram@cs.rice.edu)
                   (((Imagine there's no FORTRAN)
                       (It's easy if you try)
               (No SML below us) (Above us only Y)
              (Imagine all              the people)
             (Living for                their Chez))
          ((Imagine there's          no memory leaks)
                                 (It isn't hard to do)
                                  (Nothing to malloc(3)
                                        or free(3) for)
                                   (And no (void *) too)
                                 (Imagine all the people)
                                  (Living in parentheses))
                               ((You may say I'm a Schemer)
                                 (But I'm not the only one)
                             (I hope someday you'll join us)
                                   (And the world will be as
                            (lambda (f) (lambda (x) (f x)))))
                              ((Imagine those   continuations)
                             (I wonder              if you can)
                       (No need for              C or pointers)
                   (A brotherhood                        of Dan)
                    (Imagine all                      the people)
                    (GCing all                          the world))
               ((You may say                          I'm a Schemer)
              (But I'm not                              the only one)
         (I hope someday                                you'll join us)
        (And the world                                        will be as
    (lambda (f)                                     (lambda (x) (f x)))))))

'shriram

Enjoy
Prasanna Gautam


FreeBSD docos

February 9, 2008

Some really cool “must read” documentation on FreeBSD – the freebsd handbook and Greg Lehey’s "The Complete FreeBSD" and online version is also available.


Database in C++? Use a wrapper

January 27, 2008

Often the database itself provides an API for its access in C/C++. But it often consists of using confusing callback functions (callbacks are fun though) and the process differs from database to database. Solution? Use a wrapper..

This wrapper seems particularly interesting.. http://www.alhem.net/project/mysql/index.html

Download the wrapper source code, compile and install.

# make
# make install

Using the database using this wrapper is as easy as:

#include <mysql/mysql.h>
#include <libmysqlwrapped.h>

int main(){
StderrLog log;
Database db(“localhost”, “dbuser”, “dbpasswd”, “mydb”, &log);
if (!db.Connected()){
printf(“Database not connected – exiting\n”);
exit(-1);
}
Query q(db);
q.get_result(“select * from player”);
while (q.fetch_row()){
long num = q.getval();
std::string name = q.getstr();
printf(“#%ld: %s\n”, num, name.c_str());
}
q.free_result();
}

Good thing is, the same code can also be used for SQLite (my fav) and ODBC too (although with different wrapper and minor modifications). Another advantage is, the wrapper is cross platform, works on both win & *nix.