Share the Knowledge
RSS icon Home icon
  • Errors when installing the Python ‘lxml’ library using pip on Ubuntu

    Posted on August 28th, 2011 webmaster No comments         

    I thought I’d post this here, this is the second time I ran into this issue on Ubuntu and forgot what I did the first time.

    If you get errors about things missing when you do ‘sudo pip install lxml’ on Ubuntu, you’ll probably need to install the following development packages (source):

    • sudo apt-get install python-dev
    • sudo apt-get install libxml2-dev
    • sudo apt-get install libxslt1-dev

    You may also want to do ‘sudo pip install lxml –upgrade’ if pip tells you that it’s already installed but you know it wasn’t properly installed.

  • How to configure the ‘logging’ module using dictionaries in Python 2.6

    Posted on August 21st, 2011 webmaster No comments         

    The logging.config module was updated in Python 2.7 and included a function called dictConfig() which takes a dictionary as an argument used to configure the logging module.

    I wanted to use this in my new project so I can keep all my configurations/settings in one Python file but we’re not ready to upgrade to Python 2.7 just yet.  The good news is you can just get the dictconfig module on its own and add it to your project.  I actually just took it from the Django 1.3 package and put it in a package within the project I’m working on.

    Here’s a sample configuration/usage for the ‘root’ logger:

    Configuration:

    LOG_SETTINGS = {
        'version': 1,
        'root': {
            'level': 'NOTSET',
            'handlers': ['console', 'file', 'smtp', 'mongodb'],
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'level': 'INFO',
                'formatter': 'detailed',
                'stream': 'ext://sys.stdout',
            },
            'file': {
                'class': 'logging.handlers.RotatingFileHandler',
                'level': 'INFO',
                'formatter': 'detailed',
                'filename': 'logs/MyProject.log',
                'mode': 'a',
                'maxBytes': 10485760,
                'backupCount': 5,
            },
            'smtp': {
                'class': 'logging.handlers.SMTPHandler',
                'level': 'ERROR',
                'formatter': 'email',
                'mailhost': 'localhost',
                'fromaddr': 'alerts@calazan.com',
                'toaddrs': ['admin@calazan.com', 'support@calazan.com'],
                'subject': '[My Project] Error encountered.',
            },
            'mongodb': {
                'class': 'log4mongo.handlers.MongoHandler',
                'level': 'DEBUG',
                'host': 'localhost',
                'port': 27017,
                'database_name': 'myproject',
                'collection': 'logs',
                'username': 'logger',
                'password': 'password',
            },
        },
        'formatters': {
            'detailed': {
                'format': '%(asctime)s %(module)-17s line:%(lineno)-4d ' \
                '%(levelname)-8s %(message)s',
            },
            'email': {
                'format': 'Timestamp: %(asctime)s\nModule: %(module)s\n' \
                'Line: %(lineno)d\nMessage: %(message)s',
            },
        },
    }
    
    

    Usage:

    
    import logging
    import dictconfig
    
    from settings import LOG_SETTINGS
    
    dictconfig.dictConfig(LOG_SETTINGS)
    
    logging.debug('This is a debug message!')
    logging.info('This is an info message!')
    logging.error('This is an error message!')
    
    
  • How to print the values of all the fields of an object in Java

    Posted on August 17th, 2011 webmaster No comments         

    I was just doing some Java coding and I needed to check the values of a bunch of fields of an object (mostly numbers).  Normally I’d just call and print each get() method if I just need to check a few fields, but the class I’m dealing with has over 30 fields and I need to check them all and there’s no way I’m doing over 30 print statements for each get() method!

    A quick Google search led me to this StackOverflow post which has exactly what I was looking for:

    ClassABC abc = new ClassABC();
    for (Field field : abc.getClass().getDeclaredFields()) {
        field.setAccessible(true);
        String name = field.getName();
        Object value = field.get(abc);
        System.out.printf("%s: %s%n", name, value);
    }
    
  • How to convert a string to a dictionary in Python

    Posted on August 9th, 2011 webmaster No comments         

    I just found out about this today while working on a Django app. I have some data stored in a database as a string but the structure is a dictionary and I wanted to retrieve it as a dictionary object.

    Instead of parsing this data to retrieve the keys and values myself, it turned out Python has a module called ast (Abstract Syntax Trees) that can take care of this, specifically the literal_eval() function:

    >>> import ast
    >>> data = "{'user': 'bob', 'age': 10, 'grades': ['A', 'F', 'C']}"
    >>> ast.literal_eval(data)
    {'age': 10, 'grades': ['A', 'F', 'C'], 'user': 'bob'}
    >>> user = ast.literal_eval(data)
    >>> user['age']
    10
    >>> user['grades']
    ['A', 'F', 'C']
    >>> user['user']
    'bob'
    

    Source: http://stackoverflow.com/questions/988228/converting-a-string-to-dictionary

  • GWT, Django, and JSON (with padding)

    Posted on August 8th, 2011 webmaster No comments         

    A couple of months ago I was working on a project where I needed to retrieve data in JSON format from a web service written in Python (using the Django web framework) under GWT.  The data is then loaded into GXT/Ext GWT grids and charts.

    After looking at a GXT example of loading JSON data to a grid, I thought this would be pretty straightforward, but of course I was wrong.  Since the Django app is on a different server and domain/subdomain, I learned something called “same origin policy“, preventing our two applications to communicate with each other.

    Fortunately, there’s an easy workaround to this using “JSON with padding” or JSONP.  Here’s one way to get this working:

    GWT side:

    1.  The JsonpRequestBuilder object has been added in GWT 2.0 making these types of cross site requests easier to implement.  Here’s a very well written post showing how to do this (yes, I’m lazy and he obviously knows a lot more about these than I do :D).

    Now, when you make a request to the web service on the Django side, it will automatically append the callback function in the request and the request may look something like this:

    GET /get_gpu_stats/?callback=__gwt__jsonp__.I18.onSuccess HTTP/1.1

    Django side:

    1.  In your Django view, one way to handle this is to inspect the request and see if there’s a parameter called “callback” and change the response so it looks like a function (this is the “padding” part) instead of a regular JSON response.  You can do something like this (forgot where I found this example from):

    def get_gpu_stats(request):
        .
        .
        .
        gpu_stats = {'gpu_stats': data}
        response = simplejson.dumps(gpu_stats)
    
        # Return as JSONP if the callback parameter is specified.
        callback = request.GET.get('callback')
        if callback:
            response = '%s(%s)' % (callback, response)
        return HttpResponse(response, mimetype='application/json')
    

    Yup, that’s it, they should now be able to exchange data!