..bits & bytes teleported

Saturday Nov 08, 2008

Due to necessity, off-late I have had to increasingly use Vi in text mode over a SSH connection on a Unix box. I have already picked up some Vi skills, ones I didn't know before, or to be put in a better way- never bothered to. I am using Regular Expressions more than I ever did.

Today morning, I just read the chapter on: The Basic Tools, from The Pragmatic Programmer and in the section on 'Power Editing', the authors talk about your personal toolkit's editor of choice.

Just reading this Slashdot article http://ask.slashdot.org/article.pl?sid=08/11/06/206213&tid=185

All falling in place. The Universe is conspiring (for the better) to hone my text mode Unix skills. Amen.

Saturday Aug 09, 2008

Over the weekend- I had the urge to see what all commands are available for working with Python- so, I typed 'py' and pressed a TAB:

pycentral                pygettext                python
py_compilefiles          pygettext2.5             python2.5
pydoc                    pysupport-movemodules    pyversions
pydoc2.5                 pysupport-parseversions  

Using pydoc:

pydoc is a documentation generator, well described at http://docs.python.org/lib/module-pydoc.html . Here are my 2 cents, (decorated with screenshots) :-)

  $ pydoc -p 9090
  pydoc server ready at http://localhost:9090

starts a web server to serve documentation for Python modules, and can be accessed remotely using the (hostname, port):


You can stop the server using a ctrl + c

pydoc -g brings up a Tk-basd GUI which you can use to search for module documentation:

pyversions

pyversions with its various switches can be used to print information about installed, supported, default runtimes. For eg.

# print default Python version to be used

amit@ubuntu804-book:~$ pyversions -d 
python2.5

# print installed Python versions

amit@ubuntu804-book:~$ pyversions -i
python2.4 python2.5

# print supported Python versions
amit@ubuntu804-book:~$ pyversions -s
python2.4 python2.5

You can also get only the version numbers by adding a '-v' switch as in:

amit@ubuntu804-book:~$ pyversions -v -d
2.5

pyversions can also be used to parses the information of the PythonVersion fields in the package control file. ( I would appreciate some insight into this)

Some other miscellaneous utitlituies :

Footnotes

  • As you must have noticed, there are same utilities with version information and without version information like- python and python2.5, pydoc and pydoc2.5. Actually, in both cases python and pydoc is actually linked to /usr/bin/python2.5 and /usr/bin/pydoc2.5:

    lrwxrwxrwx 1 root root 9 2008-07-02 23:15 /usr/bin/python -> python2.5
    lrwxrwxrwx 1 root root 8 2008-07-02 23:15 /usr/bin/pydoc -> pydoc2.5
    
  • In Debian flavors, the file /usr/share/python/debian_defaults contains information about the Python versions on the system. For eg.
    amit@ubuntu804-book:~$ cat /usr/share/python/debian_defaults 
    [DEFAULT]
    # the default python version
    default-version = python2.5
    
    # all supported python versions
    supported-versions = python2.4, python2.5
    
    # formerly supported python versions
    old-versions = python2.3
    
    # unsupported versions, including older versions
    unsupported-versions = python2.3
    
    
    Refer: http://www.debian.org/doc/packaging-manuals/python-policy/
Happy times with Python!

Tuesday Jul 22, 2008

Appenders, Loggers and Layout are the main components of the Apache log4cxx API which work together to expose a logging API to be used in C++ programs. (http://logging.apache.org/log4cxx/index.html)

To design a simple C++ application which logs to a file, you will have to: (http://logging.apache.org/log4cxx/apidocs/index.html)

1. Create a layout (SimpleLayout)

2. Create an appender (FileAppender) with the above layout

3. Create a logger and add the above appender

4. use the macros to log a message


#include <log4cxx/logstring.h>
#include <stdlib.h>
#include <log4cxx/logger.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/logger.h>
#include <log4cxx/fileappender.h>
#include <log4cxx/simplelayout.h>


#include <iostream.h>

using namespace std;

using namespace log4cxx;
using namespace log4cxx::helpers;


int main() {
    //setlocale(LC_ALL, "");
    int result = EXIT_SUCCESS;
    try {
        // Create a basic configuration

        //        BasicConfigurator::configure();

        // Create a SimpleLayout

        log4cxx::LayoutPtr layout(new log4cxx::SimpleLayout());

        // Create a new appender with the created layout
        // Here we create a 'appender' to output to a FILE, instead of the 'console'.
        // Here it outputs the logs to a file 'log.txt'

        log4cxx::FileAppenderPtr appender(new log4cxx::FileAppender(layout, "log.txt", true));

        // Create a new Logger with the name: 'MyLogger'

        LoggerPtr mylogger = Logger::getLogger("MyLogger");

        // Set Level for the new logger created

        mylogger->setLevel(log4cxx::Level::getInfo());

        // Add the appender to the 'logger' object created

        mylogger->addAppender(appender);

        // Logging request via macro

        LOG4CXX_INFO(mylogger, "Trying out the Apache log4cxx API");



    } catch (std::exception& ) {

        result = EXIT_FAILURE;
    }

    return result;
}

Here is the 'log.txt' file contents:
INFO - Trying out the Apache log4cxx API