November 22, 2011

IronPython, PetaPoco, and .Net 4 'dynamic'

Recently I was playing around with IronPython and PetaPoco (a .Net micro-ORM). PetaPoco allows the use of the 'dynamic' keyword when querying data. Unfortunately, the dynamic keyword is not available in IronPython. However, since IronPython makes use of the DLR, its types are already dynamic instances. Therefore, all I needed to do was provide a Python type instead of the dynamic keyword (I used 'object'). The end result is an interesting combination of Oracle, PetaPoco, .Net 4, and IronPython.

import clr
import csv


import Oracle.DataAccess.Client
import PetaPoco

conString = "user id=USER;data source=DB;password=PASS"
provider = Oracle.DataAccess.Client.OracleClientFactory()
db = PetaPoco.Database(conString, provider)

query = "select * from my_table"
result = db.Fetch[object](query)

csvFile = csv.writer(open('out.csv', 'wb'))

if result.Count > 0:
hdrs = [header.Key for header in result[0]]

for rec in result:
vals = [col.Value for col in rec]

No comments: