[Tutor] dynamically executing a statement
Hello all, I am writing a function in which (in its simplified form) I am trying to return a list of a specified attribute, given a list of objects. It is best if I write some hypothetical code to explain: class foo: def __init__(self, name, data): self.name = name self.data = data def getAttrs(fooObjs, attr): return map(lambda item: eval('%s.%s' % (item, attr)), fooObjs) f = foo('oscar', 'green') g = foo('bert', 'yellow') e = foo('ernie', 'orange') list = [f, e, g] getNames(list, 'name') Traceback (most recent call last): File interactive input, line 1, in ? File interactive input, line 2, in getNames File interactive input, line 2, in lambda File string, line 1 __main__.foo instance at 0x00F358F0.name ^ SyntaxError: invalid syntax It seems to me like the issue is that Python is converting my object to a string representation before attempting to evaluate the expression. However, when I looked at the documenation [http://docs.python.org/lib/typesseq-strings.html] (thanks from earlier post today), I couldn't find anything that would allow python to reference the object itself in a string. Is there a way (or a different method entirely) to do this? Your help is very much appreciated, Emily ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] storing dict objects in in a database through SQLObject
Hello all, I am experiementing in storing Python objects in a SQLite databse using SQLOjbect. I want to store dicts and tuples in the databse, but as far as I can tell the only way to do this is to create a PickleCol. Is there some other better way to store this data? EnumCol? Would a different database interface support this idea better? Many thanks, Emily ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] storing dict objects in in a database through SQLObject
The question is one of balance: are you more concerned with easily getting your dicts and tuples back intact, or with executing queries on the *contents* of those dicts and tuples? Ideally, I'd like to be able to search the data in these dicts. Is there a way to do this? If not, PickleCol seems to be the way to go... To Kent: What is in the dicts and tuples? I am trying to store several different classes of objects into the database using SQLObject, some of which have dicts as attributes. The classes may be extended and the dicts store variable amounts of entries. I want this framework to be extensible as possible, but perhaps I am taking the wrong approach. The object classes were designed with their manipulation in mind, in addition to simply database storage. Is there a better design you can suggest? Emily ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How can I add my folder to pythonpath?
That is close but not quite right. The correct import will be from mymodules import mymodule1 Kent My apologies for misleading instructions. Emily Laszlo Antal wrote: Hi, This is how my directory looks myprogram(this is the main folder for my program) | myprogram.py mymodules (this is where I store my modules) | mymodule1.py mymodule2.py I would like to import from mymodules folder my modules1.py, mymodules2.py into myprogram.py. How can I add mymodules folder to pythonpath from myprogram.py.? So if I copy myprogram folder into an other pc than myprogram.py can take care of adding mymodules folder to pythonpath. Thank you in advance Laszlo Antal ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How can I add my folder to pythonpath?
If I understand what you're asking (forgive me if I misunderstood), you need to create a file in the mymodules directory and call it __init__.py (even if you don't put anything in this file). This tells Python that it is a package containing modules. Then, in your program myprogram.py, you can say import mymodule1 mymodule1.function_name('foo') Then it shouldn't matter what computer you are on; the script should run. HTH, emily Laszlo Antal wrote: Hi, This is how my directory looks myprogram(this is the main folder for my program) | myprogram.py mymodules (this is where I store my modules) | mymodule1.py mymodule2.py I would like to import from mymodules folder my modules1.py, mymodules2.py into myprogram.py. How can I add mymodules folder to pythonpath from myprogram.py.? So if I copy myprogram folder into an other pc than myprogram.py can take care of adding mymodules folder to pythonpath. Thank you in advance Laszlo Antal ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] for loops over multiple lists of the same length
I feel like there should be a better way to do this process: Can you please help? (This is trivial example code I created off the top of my head, but the same concept that I am trying to do elsewhere.) class Person(object): def __init__(self, first_name, age, fav_color): self.first_name = first_name self.age = age self.fav_color = fav_color first_names = ['emily', 'john', 'jeremy', 'juanita'] ages = [6, 34, 1, 19] colors = ['blue', 'orange', 'green', 'yellow'] ageIter = ages.iter() colorIter = colors.iter() people = [Person(name, ageIter.next(), colorIter.next()) for name in first_names] print people any suggestions, please? Emily ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Interfaces in Python
Hi friends, I am learning Python and relating to my knowledge of Java... What is (Is there?) the equivalent of Java interfaces in Python? How could I write my own? Emily ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] making a python program part of xhtml
Hola everyone, I'm working on creating a webpage in which a user can submit data into fields to be held in a database (there are other details, but this is the gist of the idea), and I need to use python. I am unfamiliar with manipulating data and web apps as a whole (and new to python), so I have been encountering much unfamiliar terrain. I _think_ I want to somehow embed the python into the page, but I'm really not sure how to do it. After googling I found some programs that generate xhtml from python, but I don't think that is what I want, or is it? Your help is appreciated! Emily ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor