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

2012-10-03 Thread Daniel Klein
On Wednesday, October 3, 2012 5:40:12 PM UTC+1, Daniel Klein wrote:
> Thank you Steven! That was PRECISELY what I was looking for.

(And kwpolska!)
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2012-10-03 Thread Daniel Klein
Thank you Steven! That was PRECISELY what I was looking for.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2012-10-03 Thread Steven D'Aprano
On Wed, 03 Oct 2012 09:23:03 -0700, Daniel Klein wrote:

> 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]")

setattr(tempmes, language, row[1])



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2012-10-03 Thread Kwpolska
On Wed, Oct 3, 2012 at 6:23 PM, Daniel Klein  wrote:
> 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?
> --
> http://mail.python.org/mailman/listinfo/python-list

Easy.

tempmes = myissue.name
language = 'polish'
setattr(tempmes, language, row[1])
tempmes.save()

-- 
Kwpolska 
stop html mail  | always bottom-post
www.asciiribbon.org | www.netmeister.org/news/learn2quote.html
GPG KEY: 5EAAEA16
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2012-10-03 Thread Daniel Klein
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? 
-- 
http://mail.python.org/mailman/listinfo/python-list