Blog / Tech

How to print the values of all the fields of an object in Java

August 17, 2011

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 …

How to convert a string to a dictionary in Python

August 9, 2011

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:

>>> …

GWT, Django, and JSON (with padding)

August 8, 2011

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 …

Extra secure, automated, offsite backup with TrueCrypt and Dropbox

August 3, 2011

I love Dropbox, it really does simplify my life.

If you’ve never used Dropbox before, you should definitely check it out as it’s a very painless way to synchronize files from multiple computers and supports all major operating systems, including mobile platforms (it’s free for up to 2GB). Your data is stored offsite so if someone breaks in to your home, steals your computer and that external hard drive lying next to it where …

How to convert a list of dictionaries to CSV format in Python

July 26, 2011

I was working on something last night where I needed to dump JSON data to a text file. I figured it might be a good idea to give an option to export it to a CSV file with headers as well so I wrote this simple function to do so:

import csv

def export_dict_list_to_csv(data, filename):
    with open(filename, 'wb') as f:
        # Assuming that all dictionaries in the list have the same keys.
        headers = sorted([k …