Help me abstract this (and stop me from using eval)

Daniel Klein bringa at gmail.com
Wed Oct 3 12:23:03 EDT 2012


Hi!

I've got import scripts for a bunch of csv files into an sqlite database. I have one csv file per language. I don't write directly to the sqlite db; this is a django app and I'm creating items in my django models. 

My script (scripts, unfortunately) work just fine, but it feels beyond stupid to have one script per language--importgerman, importfrench etc. The problem is that I don't know how to abstract which language I'm currently writing to. Here's the central loop:

http://pastebin.com/NBT6feNB

This is for the Polish version of the script, of course, the one that gets fed pl.csv. For those unfamiliar with django, I need to first retrieve the message instance for issue name / long text / short text / category name. Hence the ugly tempmes stuff. 

So ideally I would tell the script which language I'm currently importing and based on that it would write to the appropriate text fields. But I don't know how to abstract this:

tempmes.polish = row[1]

Well, I do. Like this:

eval("tempmes." + language + " = row[1]")

But... eval is evil, no? There's got to be a better way? 



More information about the Python-list mailing list