Re: Newbie Question
look into the csv module. (for comma-separated-value text files.) Tom Strickland wrote: I have a file that contains many lines, each of which consists of a string of comma-separated variables, mostly floats but some strings. Each line looks like an obvious tuple to me. How do I save each line of this file as a tuple rather than a string? Or, is that the right way to go? Thank you. Tom Strickland -- http://mail.python.org/mailman/listinfo/python-list
Better access to database search results
Usually when I access db search results it's something like this: cursor.execute(select A1,A2,A3,A4 from B where C) for (a1,a2,a3,a4) in cursor.fetchall(): stuff() But sometimes the point at which I use the data returned is not with the search, and so having the ability to access the results as a dictionary is more useful. I know that with MySQLdb I can open the connection and have it return a dictionary, but when only one search out of all the requests needs to be that way, it seems too much hassle to get a new cursor just for that. Also, as a web developer that uses python, my designers cognate name.attribute much easier than name['attribute'] and so I came up with this function to help us both out. Let me know what you think. Could this be done simpler? def convert_cursor(cursor): '''Converts a cursor object with a result set in tuples to a list of dicts, where the members of the keys are the field names, and the values are the field values.''' fields = map(lambda x: x[0], cursor.description) class DictObj(dict): ''' DictObj is a normal dictionary that allows you to access its members via ``var[key]`` as well as ``var.key``. ''' def __init__(self,dic): dict.__init__(self,dic) self.__dict__ = self return [DictObj(zip(fields,x)) for x in cursor.fetchall()] -- http://mail.python.org/mailman/listinfo/python-list
Re: Better access to database search results
Simon Brunning wrote: On Apr 12, 2005 4:32 PM, Gabriel Cooper [EMAIL PROTECTED] wrote: Usually when I access db search results it's something like this: cursor.execute("select A1,A2,A3,A4 from B where C") for (a1,a2,a3,a4) in cursor.fetchall(): stuff() But sometimes the point at which I use the data returned is not with the search, and so having the ability to access the results as a dictionary is more useful. Let me know what you think. Could this be done simpler? snip Have you come across dtuple? (http://www.lyra.org/greg/python/dtuple.py). It's really easy to use - http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81252. Hm. that's interesting, it's pretty much the same idea, only I do all the processing up front and return a complete variable that's generally indistinguishable from a list of dictionaries. This method (at least in the ASPN example) appears to prefer that you process each record as you come to it (which is undoubtedly more scalable). For my purposes, where as little programming logic as possible is required in the "view" section of code, I'm willing to accept the idea that I won't be using my function on immense amounts of data. To explain, I develop websites using Snakeskin (http://snakeskin-tools.sf.net/). It separates design and logic by first processing a python object with specific event triggers and then loads the requested template to accompany it. The python object does the logic, the template does the display. e.g. def page_process(self, ctx): c = ctx.request.db.cursor() c.execute("select a,b,c ") ctx.locals.some_var = c.fetchall() # anything in ctx.locals is available as a var in the template then in the template: html body ... ss-for expr="some_var" iter="i" expand="record" Print out A: ss-value expr="record.a"/ Print out B: ss-value expr="record['b']"/ ... /ss-for ... /body /html -- http://mail.python.org/mailman/listinfo/python-list
Re: Pseudocode in the wikipedia
Ron_Adam wrote: To me := could mean to create a copy of an object... or should it be =: ? Or how about :=) to mean is equal and :=( to mean it's not. Then there is ;=), to indicate 'True', and ':=O' to indicate 'False' Not to mention (_ | _) for asserts! -- http://mail.python.org/mailman/listinfo/python-list
Re: FW: Python help group
I would recommend looking into sorting like objects with the __getitem__ method. http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/b2d0580792b94a7/a707054ec3302a6e has a lively discussion on this topic. Search google for "__getitem__" and "sort" for more information. Leeds, Mark wrote: Can someone help me with below ? Its not my question but I will forward any answers to my friend who I am sending this for. Mark -Original Message- From: Tan, Heap Ho Sent: Tuesday, March 22, 2005 1:11 PM To: Leeds, Mark Subject: Python help group I want to do a sort on a list of objects based on a similar attributes in each object for example time of creation of this object. Is there a simple way to do this? Thank -Original Message- From: Leeds, Mark Sent: Monday, March 21, 2005 5:35 PM To: Tan, Heap Ho Cc: Leeds, Mark Subject: crontab -- __ gabriel.cooper@mediapulse.com internet developer 865.675.4455 x32 800.380.4514 www.mediapulse.com __ -- http://mail.python.org/mailman/listinfo/python-list
Re: update images inside a mysql database
Jonas Meurer wrote: def i_update(image, imgid): image = %s % (image) sql_exec = UPDATE Images SET Image='%s' WHERE ImgID = '%s' % (image, imgid) o = open(/tmp/file.jpg, w) o.write(image) o.close() db_connect.cursor.execute(sql_exec) I've never tried extensively to use images inside a database (too slow for most of my uses), but I thought I'd drop in to point out that you should, for security reasons, be using place holders on your sql. It might just fix your image problem as well, but I don't know. Also, converting a binary image into a string doesn't seem like it would be wise, but like I said, I've never tried it. At any rate, your function would look like this: def i_update(image, imgid): image = %s % (image) o = open(/tmp/file.jpg, w) o.write(image) o.close() db_connect.cursor.execute(UPDATE Images SET Image=%s WHERE ImgID=%s, (image, imgid)) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python UPS / FedEx Shipping Module
Tom Willis wrote: On one hand you are helping them indirectly sell their services, On the other, you are sort of competing with them, so, those companies have plenty of reason to screw with you by changing the specs, lot's of hoops to jump through to contiuously be certified. Well before we make it out to be more than it is, what I have here is merely a module for getting rate requests. Basically we wanted a UPS/FedEx module to price-check shipping costs for eCommerce websites. The other types of requests could be implemented with no problem, but that's all that has been done so far. Gabriel. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python UPS / FedEx Shipping Module
Tom Willis wrote: Are the modules just accessing the published apis for their webservices? I'm just wondering because I used to work for a logistics mgmt company that paid money to be a strategic partner with FedEx/UPS/Airborn etc so that they could information on how to return rates/print labels/generate edi's/calculate arrival times etc. I'd get a good laugh out of it suddenly being freely available now. Yes, they use the free API's that have always been freely available. They're just *Impossible* to find on either of UPS's OR FedEx's websites. It took me no less than an hour and likely more just to find the documentation pages on each site. As for putting them up on the web, I should be able to get them up by the end of the week. Gabriel. -- http://mail.python.org/mailman/listinfo/python-list
Python UPS / FedEx Shipping Module
I've made UPS and FedEx shipping rate request modules in python using XML. Is there an interest in putting this on the web? -- http://mail.python.org/mailman/listinfo/python-list
Re: bytecode obfuscation
snacktime wrote: Also, I'm curious how much demand their is for this application in the Python world. The application replaces online credit card processors(Verisign, Authorizenet) by providing a platform that connects directly to the bank networks for credit card processing, and also provides other features such as recurring billing, reporting, etc.. Everything except the libraries that actually connect to the bank networks would be open source, and those libraries aren't something that you would even want to touch anyways. Personally, I would avoid running any software that processed my users' credit cards that wasn't open source. Not to say I wouldn't use it, but it would have to come from a known-reliable source with their full backing to imply the security of their name. e.g. If it was a bytecode library downloadable directly from bankofamerica.com. The risk of devious programming is too high to bother. So I pay Verisign or Authorizenet one cent on the dollar. In exchange, my clients' identities aren't being stolen. Hope this help, Gabriel. -- http://mail.python.org/mailman/listinfo/python-list
Re: Remove HTML tags (except anchor tag) from a string using regular expressions
Max M wrote: If it's not to learn, and you simply want it to work, try out this library: http://zope.org/Members/chrisw/StripOGram/readme stripogram.html2safehtml('''first last''',valid_tags=('i','a','br')) 'first last' stripogram.html2safehtml('''first last''',valid_tags=('i','a','br')) 'first first ' keeping in mind that bare and are invalid HTML (should be gt; and lt;), why'd it leave the greater than and why are there two first's ? -- http://mail.python.org/mailman/listinfo/python-list
Finding a script's home directory?
In one of my python programs has a data file I need to load. My solution was to say: if os.path.exists(os.path.join(os.getcwd(), config.xml)): self.cfgfile = os.path.join(os.getcwd(), config.xml) Which works fine... as long as you're *in* the script's home directory when you run it (as in, run it as: ./startApp.py as opposed to ./myApp/startApp.py). If I run it from an alternate directory the program looks for the config.xml file in my current directory not the app's home directory. So how do I get the script's home directory? -- http://mail.python.org/mailman/listinfo/python-list
Re: Time Difference
GMane Python wrote: Hello I was wondering if there is an existing function that would let me determine the difference in time. For a more robust solution, checkout Python's profile module. http://docs.python.org/lib/profile.html -- http://mail.python.org/mailman/listinfo/python-list