views:

80

answers:

1

I wrote a nice little app that gets Yahoo weather info and posts it to Twitter. It worked flawlessly and now I want to rearrange the code into differently named files so it makes more sense. And that's when I hit some issues.

Previously, I had a Class in libtweather.py. It was my account. It allowed me to do accountName.parseFeed() and I'd get as output the parsed Yahoo weather. (__ini__ took the weather URL, twitter username and password as args)

This was accessed from my main script which created instances of the Class like this:
exec '%s = lw.twitterWeather("%s", "%s", "%s")' % (item[0], item[1], item[2], item[3]) It kept a list of all account names in a list which was passed as argument to the other functions.

Another function getWeather got weather by doing:

def getWeather(accountList): #account names passed as a list of strings
    for item in accountList:
        print item, ': ',
        item = eval(item)       
        print item.parseFeed(), '\n

I've decided now to move the getWeather function to the same file as the Class but the line item = eval(item)'s giving me problems because there are no instances created in that file. All of them are in the main script.

Now my question: Is there some way I could give those instances as arguments to the function? Or must I put the function into the Class? Even if I did that, I'd still need to do the item.parseFeed() for multiple items in the list so I'd still need the item = eval(item), no?

Thanks in advance. My app's a tad bit to post here in entirety, but I'll post more code if needed to understand better.

Update: I ended up running my libtweather.py to create instances when it's imported so that the functions inside it can access them (added the instance generating code at the bottom of the script). I'm sure there's a better way but it works for me currently and I'm OK with that.

+4  A: 

You should be using an explicit dict for storing these items. eval, exec, globals, locals, and vars are all horribly silly ways to do this poorly. Remember from the Zen of Python: "explicit is better than implicit."

feeds = {}
for item in whatever:
    feeds[item[0]] = lw.twitterWeather(*item[1:])

def getWeather(feeds, accountList):
    for item in accountList:
        print '%s: %s' % (item, feeds[item].parseFeed())
Aaron Gallagher
+1 for Zen refer
Odomontois
I never thought of a dict. This app was how I started learning Python and I only recently started realizing the amazingness of dicts. But if I have `getWeather` defined in the same file as the Class and I generate the instances in my main script (separate from Class file), can I pass this dict as argument to my function and have instances point at the right place (inside the Class file, the function can't have a `lw.whatever` because the `lw` never gets imported.) And thanks for the `*item[1:]`; didn't know you could do that. *Sorry but I'm still kind of new to all this :P*
vlad003
So I finally changed this. It didn't take as much changing to get my functions to use a dict instead of what I had before. Although I'm not using the same organization as my initial question (I'm creating instances inside my libtweather.py, not in main script), I tried out passing the dict to the `getWeather` function inside the `libtweather.py` from my main script and it worked. the instances were accepted without errors. So answer accepted. Thanks!
vlad003