PostgreSQL query result as Python dictionary

I use the psycopg2 module for using PostgreSQL databases in Python. When you execute an SQL query the result is given back to you as a list of tuples (each tuple being a database row). That’s the default behaviour but I think it’s more convenient to have a list of dictionaries where the column name is the key and the cell is the value. Getting dictionaries instead of tuples is achieved using ‘RealDictCursor‘. Here’s some code showing the difference.

Default behaviour

Rows as dictionaries instead of tuples

Technically you’re getting each row in the result as a ‘psycopg2.extras.realdictrow’. This makes no difference as it sub-classes the Python dictionary type.

More info

Also look at ‘DictCursor’, it’s slightly different because the result also behaves like a list so you can use row number as the key. I don’t use it since you’d need to know the row ordering in advance for it to be useful or I’m not sure why a tuple wouldn’t be enough… you decide! http://initd.org/psycopg/docs/extras.html#dictionary-like-cursor

Article on getting started with Python and Postgresql – Link

Get all eBay categories in PHP

Some one asked me how they can get all the categories for an eBay site in PHP using the web service. You need two things: a HTTP client and your eBay API credentials.

Using cURL as an HTTP client

Use the test tool to get your eBay API credentials and build the request

If you’re new to the test tool or eBay web services then this article will help you.

  1. Go to the test tool and change the environment from Sandbox to Production
  2. Select the Trading API
  3. Choose “GetCategories” from the calls drop down
  4. Choose “Full, generic” and choose “All fields” from the drop down
  5. Choose the site id (more info here)
  6. Submit

GetCategories

Install php-cli and run the script from command line

response

More Information

Nice PHP + cURL tutorial – http://codular.com/curl-with-php

Introduction to using the eBay API test tool – article

Official eBay documentation on the “GetCategories” api call – http://developer.ebay.com/DevZone/XML/docs/Reference/eBay/GetCategories.html