Lefora Free Forum
34 views

Any python wizards? Solved..!

Page 1
posts 1–5 of 5
Iguana - member
299 posts




Oh well seems the way, in writing this out I actually solved it... So as it may help others or I never know when I'll forget this. ;)
 
OK got playing and I'm creating dynamic variables like..


for each in self.items.keys():
self.__dict__[each] = self.items[each]

Now this works fine in a function I can call function.foo where foo = (The evaluation of)self.__dict__[each] and it will return the value. Now having stepped through it with pdb I'm definitely missing something, as there is no var foo. But as I don't know what foo is (Well I do as I created a global.list) I tried to reiterate over my global.list and want to populate a dictionary, but I'm hitting a brick wall, I've tried eval I've tried doing similar to self.__dict__ nothing is helping.

in Psuedo-code


for i in global.list
               x = funtion.(eval(i))

I just keep getting told load instance has no attribute eval with out brackets and with syntax error. So finally getting close.. I can return <__main__.load instance at hex>.foo using str(function)+str(foo) and narrows the problem down to 
unsupported operand type(s) for +: 'instance' and 'str'

So how do I get around this eval isn't working for me using str hasn't helped as it returns the instance as a string but I want to create the string to evaluate it.

Solution:--
Create a string then eval the string like so


part ="self.function."
compiled = part+str(each)
print eval(compiled)

Though if anyone can see a way around using eval though I would appreciate it as I'm not sure it is the way to go.

__________________
I've always used the new hardware that's out, a cellulose tube filled with carbon crystals working over a flexible cellulose base
Monitor Lizard - admin
548 posts

Wish I could be more help.  I did start learning python at one point, but got sidetracked and probably forgotten most of what I'd learned.

__________________
www.suseunbound.com I'm a PC and I run on Linux
Iguana - member
299 posts




Thanks bdquick

Doesn't matter too much as I did solve my initial problem but eval is not preferred as it can have security implications. Guess I'll have to find out what I need to parse out of the string so nothing it can get to is malicious.Wish there was some kinda of module that did no system calls. i.e something similar to htmlentities.

At the moment I'm not even sure what or how.

As for forgetting was why I finally posted this here wink creating the dynamic vars was tricky wasn't till I found self.__dict__ I cracked this one.


__________________
I've always used the new hardware that's out, a cellulose tube filled with carbon crystals working over a flexible cellulose base
Iguana - member
299 posts

OK so asked else where and the answer is..

No need for the eval instead I can use getattr(class, str) so it now looks like..

for str in subclass():
 getattr(class, str)



Much better...

__________________
I've always used the new hardware that's out, a cellulose tube filled with carbon crystals working over a flexible cellulose base
Monitor Lizard - admin
805 posts

Glad you figured it out. The only variable foo around here is in the goat pen. I don't think that would have helped. Although if you want to call for it, you may have all you want.grin

__________________
All social problems have a technical solution. That solution may or may not be socially acceptable.
Page 1
posts 1–5 of 5

This Topic Is Locked To Guest Posts

It's been a while since this topic was active, if you'd like to get it going again, please post as a registered member

join now