Re: Question about using python as a scripting language
Hi, > I was wondering how I can read > commands from the XML file and then execute them in the game. ... > I just need some way of > being able to read from the file what function the program needs to > call next. Any help is appreciated. One thing you could do is use the eval or compile methods. These functions let you run arbitray code passed into them as a string. So, for instance, you can write: my_list = eval('[1,2,3,4]') and my_list will then be assigned the list [1,2,3,4], moreover: eval("my_list.%s" % "reverse()") or ... even further .. :-) command = "reverse" eval("my_list.%s()" % "reverse") will reverse my_list Is that something like what you're looking for? -steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Question about using python as a scripting language
Delaney, Timothy (Tim) wrote: > This is just asking for trouble. > > my_list = eval('import shutil; shutil.rmtree('/')') Hah .. wow. And in related news: you still shouldn't be taking candy from strangers. Point well taken. Thanks for flagging that one. -steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Pre-defining an action to take when an expected error occurs
> if the Excel sheet has 10 rows with data in them, the > statement "range(sh.nrows)" should build the list of numbers [0, > 1,...9]. It should, but it doesn't do that. What it does is buld a list > from [0, 1...20] or more or a little less, but the point is that it > always grabs empy rows after the last row containing data. Why is that? Just a stab in the dark, but maybe there's some rogue whitespace in some cell that's in a rowyou think is empty? You could try to just select out a (small) region of your data, copy it and paste it into a new spreadhseet to see if you're still getting the problem. -steve -- http://mail.python.org/mailman/listinfo/python-list
Re: matplotlib
> This site and webpage in particular doesn't open. I tried that too > before posting my question. The page (http://matplotlib.sourceforge.net/tutorial.html) may not be up now, but will most certainly be etched in Google's mind for some time to come, via Google Cache: http://64.233.161.104/search?q=cache:w1ZcsnHEX3gJ:matplotlib.sourceforge.net/tutorial.html+http://matplotlib.sourceforge.net/tutorial.html&hl=en&gl=us&ct=clnk&cd=1 Or, if you have some real gumption, you can check out matplotlib from SVN, the tutorial is in the htdocs folder (htdocs/tutorial.html.template) and rebuild the entire site (which is most likely overkill .. but hey, who can say). SVN checkout instructions are here: http://sourceforge.net/svn/?group_id=80706 HTH, -steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Programming Idiomatic Code
I don't know about idiomatic, but here's a quick list of somethings I'd change. 1) In general, I don't think it's a good idea for a called function to blow out the system on error. So, in this example, I wouldn't have raised errors be caught in the same function only to call os.exit. I'd either deal with the exception in some sane way, or have it bubble up to the caller to have it then decide what it should do in the face of the error. > def read_id_file(filename): > """ reads a file and generates a list of ids from it""" > ids = [ ] > try: > id_file = open(filename, "r") > for l in id_file: > ids.append( l.strip("\n") ) > id_file.close() > except e: > print e > os._exit(99) > return ids Maybe use a list comprehension here (try/except clauses excluded in reference to previous comment @ top) def read_id_file(filename): id_file = open(filename, 'r') ids = [line.strip() for line in id_file.xreadlines()] id_file.close() return ids If you're shooting for only Python 2.5, you can use ``with`` (see http://docs.python.org/tut/node10.html#cleanup-with) to make that even more concise. > def generate_count(id_list): > """ takes a list of ids and returns a dictionary of cities with > associated counts""" > city_count = {} > for i in id_list: > url = "%s%s" %(base_url,i) > req = urllib2.Request(url) > try: > xml = parse(urllib2.urlopen(url)).getroot() > city = xml.findtext('user/city') > except e: > print e.reason > os._exit(99) > try: > city_count[city] += 1 > except: > city_count[city] = 1 > return city_count I maybe wouldn't bail on the error raised when the ``parse`` function is called ... I'd rather skip the error and try the next one (but that's just my preference, not sure if it's good/bad style) Also, instead of doing this: > try: >city_count[city] += 1 > except: >city_count[city] = 1 I like using the dict.get() methods ... which would make that a one liner: city_count[city] = city_count.get(city, 0) + 1 Now I gotta get back to work, too ... good luck getting that job! ;-) -steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Python for web...
It's also worthwhile to note that the apress django book listed is also accessible free online here: http://www.djangobook.com/en/1.0/ -steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting up and running with Python on a Mac
> I want to do a fair bit of scientific / > numerical computing, so it would seem that SAGE ot the Enthought > Python distribution would seem to be the most relevant - I'd > appreciate your guidance on getting Python to run on a Mac with a > particular focus on these two distributions. As already mentioned, you can use the already installed Python that comes with Leopard (2.5.1), or you can download the latest from python.org (2.5.2). Either way, another way do get a scientific environment up and running is by getting the "Scipy Superpack" installer. It's a frontend that downloads and installs the latest scipy/numpy/matplotlib/ipython combo (from their SVN repos). The author updates the builds every so often, so you can for the most part run with the latest updates: http://macinscience.org/?page_id=6 If you're using the Python that comes with Leopard, it will be very straightforward and you'll have nothing else to do aside from running the Superpack installer. If you want to use the latest Python from Python.org, there will be some trickery, which includes: (i) getting the correct (newer) python to come first in your path, (ii) installing setuptools for the new python (iii) insuring that the new setuptools easy_install is "the one" in your path (iv) install the latest OS X wxPython build (from their website). It's all fairly straightforward once you account for the aforementioned gotchas. As far as the enthought installer goes, I wasn't aware that it was ready for showtime on OS X ... do you know otherwise? HTH, -steve -- http://mail.python.org/mailman/listinfo/python-list