Thursday 16 July 2009

String formatting with Python

Replacing a string:
$ python
>>> name = u'Lāsma'
>>> letter = 'Dear customer,\n Our customer service is the best.'
>>> letter = letter.replace('customer', name, 1) #replace only the first 'customer'
>>> print letter
Dear Lasma,
Our customer service is the best.
>>>


Dictionary based string formatting:
$ python
>>>name = u'Lāsma'
>>>sentence = 'My name is %(name)s"
>>>print sentence % vars()
My name is Lāsma
>>>

Thursday 9 July 2009

Down them all!

Use wget recursively to download all files from one web page with a single Linux command:
$ wget -r http://download.geonames.org/export/dump

Friday 3 July 2009

Wednesday 1 July 2009

How to union tables in PostgreSQL

This query unions two tables which contain the same columns thus works as table aggregate:
CREATE TABLE total_stats AS
SELECT order_nr
, transaction_date
, price_usd FROM january_stats
UNION
SELECT order_nr
, transaction_date
, price_usd FROM february_stats;


This query, however, will union tables where column structure not necessarily match:
CREATE TABLE total_stats AS
SELECT order_nr
, customer_id
, transaction_date
, price_usd FROM january_stats
UNION
SELECT order_nr
, (SELECT 'unknown' AS customer_id)
, transaction_date
, fare_usd AS price_usd FROM february_stats;


More then two tables can be united similarly.