Re: Manipulating MySQL Sets
On Sat, Dec 12, 2009 at 6:35 PM, Carsten Haese carsten.ha...@gmail.comwrote: The traceback helpfully shows us that colValue is a 1-tuple whose zeroth entry, colValue[0], is an actual bona-fide Python Set object. Such objects aren't indexable, because sets are unordered. That still doesn't tell us where colValue is coming from, though, so why it is a Python Set object when you expected it to be a string remains an unsolved mystery. Who said I was expecting a string? I don't know what I'm expecting! I need to be able to parse this thing, whatever it is. You say it's a Python Set object. How do I parse it? Googling has been disappointing. TIA, V -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
On Sun, Dec 13, 2009 at 12:48 PM, Victor Subervi victorsube...@gmail.com wrote: Who said I was expecting a string? I don't know what I'm expecting! I need to be able to parse this thing, whatever it is. You say it's a Python Set object. How do I parse it? Googling has been disappointing. You can walk thought a set object like any iterable object like for x in myset: #do something like parse it. If you want to slice it, convert it to a list: mylist = list(myset) Or read about sets: http://docs.python.org/library/sets.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
Victor Subervi wrote: On Sat, Dec 12, 2009 at 6:35 PM, Carsten Haese carsten.ha...@gmail.com mailto:carsten.ha...@gmail.com wrote: The traceback helpfully shows us that colValue is a 1-tuple whose zeroth entry, colValue[0], is an actual bona-fide Python Set object. Such objects aren't indexable, because sets are unordered. That still doesn't tell us where colValue is coming from, though, so why it is a Python Set object when you expected it to be a string remains an unsolved mystery. Who said I was expecting a string? You did, indirectly, since you were expecting the value to be the contents of a MySQL SET column, which as far as I can determine should be returned as a string. (Recall that I showed a complete example that demonstrates this.) The fact that you don't get a string is something that you ought to be interested in investigating. I don't know what I'm expecting! That statement is the most succinct summary of the root cause of all your problems. If you don't know what to expect, how could you possibly write code that produces what you expect? (Don't answer this question. It's a rhetorical question.) I need to be able to parse this thing, whatever it is. You say it's a Python Set object. How do I parse it? Googling has been disappointing. What do you mean by parse? What is the end result you are hoping to achieve by parsing a Set object? -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
On Sun, Dec 13, 2009 at 11:36 AM, Carsten Haese carsten.ha...@gmail.comwrote: I don't know what I'm expecting! That statement is the most succinct summary of the root cause of all your problems. If you don't know what to expect, how could you possibly write code that produces what you expect? (Don't answer this question. It's a rhetorical question.) LOL! Yeah. Not entirely true, obviously, or even mostly so...but enough to be humorous ;) I need to be able to parse this thing, whatever it is. You say it's a Python Set object. How do I parse it? Googling has been disappointing. What do you mean by parse? What is the end result you are hoping to achieve by parsing a Set object? I need to get at the individual elements of the set (that which is between the single quotes). TIA, V -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
On Sun, 13 Dec 2009, Victor Subervi wrote: On Sun, Dec 13, 2009 at 11:36 AM, Carsten Haese carsten.ha...@gmail.com wrote: I don't know what I'm expecting! That statement is the most succinct summary of the root cause of all your problems. If you don't know what to expect, how could you possibly write code that produces what you expect? (Don't answer this question. It's a rhetorical question.) http://twitter.com/rpjday/status/6576145809 rday -- Robert P. J. Day Waterloo, Ontario, CANADA Linux Consulting, Training and Kernel Pedantry. Web page: http://crashcourse.ca Twitter: http://twitter.com/rpjday -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
On Sun, Dec 13, 2009 at 11:56 AM, Robert P. J. Day rpj...@crashcourse.cawrote: On Sun, 13 Dec 2009, Victor Subervi wrote: On Sun, Dec 13, 2009 at 11:36 AM, Carsten Haese carsten.ha...@gmail.com wrote: I don't know what I'm expecting! That statement is the most succinct summary of the root cause of all your problems. If you don't know what to expect, how could you possibly write code that produces what you expect? (Don't answer this question. It's a rhetorical question.) http://twitter.com/rpjday/status/6576145809 Indeed. Specification, however, is as much art as it is science. Are you a better programmer today than your first day programming? Have you learned how to specify better in the process? More rhetorical questions ;) V -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
On Sun, 13 Dec 2009, Victor Subervi wrote: On Sun, Dec 13, 2009 at 11:56 AM, Robert P. J. Day rpj...@crashcourse.ca wrote: On Sun, 13 Dec 2009, Victor Subervi wrote: On Sun, Dec 13, 2009 at 11:36 AM, Carsten Haese carsten.ha...@gmail.com wrote: I don't know what I'm expecting! That statement is the most succinct summary of the root cause of all your problems. If you don't know what to expect, how could you possibly write code that produces what you expect? (Don't answer this question. It's a rhetorical question.) http://twitter.com/rpjday/status/6576145809 Indeed. Specification, however, is as much art as it is science. Are you a better programmer today than your first day programming? Have you learned how to specify better in the process? More rhetorical questions ;) it's sunday, i have beer and there's an NFL game on soon. do not ask tough questions. that is all. rday -- Robert P. J. Day Waterloo, Ontario, CANADA Linux Consulting, Training and Kernel Pedantry. Web page: http://crashcourse.ca Twitter: http://twitter.com/rpjday -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
Victor Subervi wrote: I need to get at the individual elements of the set (that which is between the single quotes). It's not quite clear what you mean by get at the individual elements, so I'll just show you a couple of things you can do to a set, and maybe one of them comes close to what you need: from sets import Set aSet = Set(['Small', 'Extra-small', 'Medium']) Do something for each element: for element in aSet: ... print element ... Small Extra-small Medium Convert the set to list: list(aSet) ['Small', 'Extra-small', 'Medium'] Test membership in the set: 'Small' in aSet True 'Banana' in aSet False HTH, -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
On Sun, Dec 13, 2009 at 11:26 AM, Sebastian Bassi sba...@clubdelarazon.orgwrote: On Sun, Dec 13, 2009 at 12:48 PM, Victor Subervi victorsube...@gmail.com wrote: Who said I was expecting a string? I don't know what I'm expecting! I need to be able to parse this thing, whatever it is. You say it's a Python Set object. How do I parse it? Googling has been disappointing. You can walk thought a set object like any iterable object like for x in myset: #do something like parse it. If you want to slice it, convert it to a list: mylist = list(myset) Or read about sets: http://docs.python.org/library/sets.html Both the following solutions threw the same error: newCol = list(colValue[0]) print newCol[0:20] and for x in colValue[0]: print x /var/www/html/angrynates.com/cart/display.py 98 raise 99 cursor.close() 100 bottom() 101 102 display() display = function display /var/www/html/angrynates.com/cart/display.py in display() 50 #newCol = list(colValue[0]) 51 #print newCol[0:20] 52 for x in colValue[0]: 53 print x 54 print 'XXX' x undefined, colValue = (datetime.date(2009, 10, 22),) TypeError: iteration over non-sequence args = ('iteration over non-sequence',) V -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
On Sun, Dec 13, 2009 at 2:10 PM, Carsten Haese carsten.ha...@gmail.com wrote: from sets import Set aSet = Set(['Small', 'Extra-small', 'Medium']) You don't need to import Set since it is built in now: a=set([1,2,2,3,4,5]) a set([1, 2, 3, 4, 5]) (I have Python 2.6.2 but I think that this is available from 2.4 or 2.5). -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
On Sun, Dec 13, 2009 at 12:10 PM, Carsten Haese carsten.ha...@gmail.comwrote: Victor Subervi wrote: I need to get at the individual elements of the set (that which is between the single quotes). It's not quite clear what you mean by get at the individual elements, so I'll just show you a couple of things you can do to a set, and maybe one of them comes close to what you need: from sets import Set aSet = Set(['Small', 'Extra-small', 'Medium']) Do something for each element: for element in aSet: ... print element ... Small Extra-small Medium Convert the set to list: list(aSet) ['Small', 'Extra-small', 'Medium'] Test membership in the set: 'Small' in aSet True 'Banana' in aSet False Carsten, thank you (once again ;) for your patience. I didn't know I had to import something. Thanks again, V -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
On Sun, Dec 13, 2009 at 12:39 PM, Victor Subervi victorsube...@gmail.comwrote: PS: I have another field that's a datetime with the same problem. How do I parse and otherwise work with that? TIA, V -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
Victor Subervi wrote: On Sun, Dec 13, 2009 at 11:26 AM, Sebastian Bassi sba...@clubdelarazon.org mailto:sba...@clubdelarazon.org wrote: On Sun, Dec 13, 2009 at 12:48 PM, Victor Subervi victorsube...@gmail.com mailto:victorsube...@gmail.com wrote: Who said I was expecting a string? I don't know what I'm expecting! I need to be able to parse this thing, whatever it is. You say it's a Python Set object. How do I parse it? Googling has been disappointing. You can walk thought a set object like any iterable object like for x in myset: #do something like parse it. If you want to slice it, convert it to a list: mylist = list(myset) Or read about sets: http://docs.python.org/library/sets.html Both the following solutions threw the same error: newCol = list(colValue[0]) print newCol[0:20] and for x in colValue[0]: print x /var/www/html/angrynates.com/cart/display.py http://angrynates.com/cart/display.py 98 raise 99 cursor.close() 100 bottom() 101 102 display() display = function display /var/www/html/angrynates.com/cart/display.py http://angrynates.com/cart/display.py in display() 50 #newCol = list(colValue[0]) 51 #print newCol[0:20] 52 for x in colValue[0]: 53 print x 54 print 'XXX' x undefined, colValue = (datetime.date(2009, 10, 22),) TypeError: iteration over non-sequence args = ('iteration over non-sequence',) That tells me that, in this case, colValue[0] is an instance of datetime, not Set. -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
On Sun, Dec 13, 2009 at 12:40 PM, Victor Subervi victorsube...@gmail.comwrote: On Sun, Dec 13, 2009 at 12:39 PM, Victor Subervi victorsube...@gmail.comwrote: PS: I have another field that's a datetime with the same problem. How do I parse and otherwise work with that? I have yet another problem with this. Once I've determined it's a Set or a datetime, I do this: for itm in colValue[0]: try: color, number = string.split(itm, ':') html += option name='%s'%s/option % (itm, color) except: html += option name='%s'%s/option % (itm, itm) The reason is I've entered my colors into my table as nameOfColor:colorNumber and I want to break them out for presentation purposes. However, the try never executes, only the except, and that shouldn't be. I can slice the itm. What gives? TIA, V -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
Victor Subervi wrote: On Sun, Dec 13, 2009 at 12:39 PM, Victor Subervi victorsube...@gmail.com mailto:victorsube...@gmail.com wrote: PS: I have another field that's a datetime with the same problem. How do I parse and otherwise work with that? Look at the documentation for the datetime class. -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
Victor Subervi wrote: On Sun, Dec 13, 2009 at 12:40 PM, Victor Subervi victorsube...@gmail.com mailto:victorsube...@gmail.com wrote: On Sun, Dec 13, 2009 at 12:39 PM, Victor Subervi victorsube...@gmail.com mailto:victorsube...@gmail.com wrote: PS: I have another field that's a datetime with the same problem. How do I parse and otherwise work with that? I have yet another problem with this. Once I've determined it's a Set or a datetime, I do this: for itm in colValue[0]: try: color, number = string.split(itm, ':') html += option name='%s'%s/option % (itm, color) except: html += option name='%s'%s/option % (itm, itm) The reason is I've entered my colors into my table as nameOfColor:colorNumber and I want to break them out for presentation purposes. However, the try never executes, only the except, and that shouldn't be. I can slice the itm. What gives? 1. Print repr(itm) so that you can check what it actually is. Is it what you expected? 2. DON'T USE BARE EXCEPTS! -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
Victor Subervi wrote: On Sun, Dec 13, 2009 at 12:10 PM, Carsten Haese carsten.ha...@gmail.comwrote: Victor Subervi wrote: I need to get at the individual elements of the set (that which is between the single quotes). It's not quite clear what you mean by get at the individual elements, so I'll just show you a couple of things you can do to a set, and maybe one of them comes close to what you need: from sets import Set aSet = Set(['Small', 'Extra-small', 'Medium']) Do something for each element: for element in aSet: ... print element ... Small Extra-small Medium Convert the set to list: list(aSet) ['Small', 'Extra-small', 'Medium'] Test membership in the set: 'Small' in aSet True 'Banana' in aSet False Carsten, thank you (once again ;) for your patience. I didn't know I had to import something. Thanks again, V Have you bothered to find the help file for your particular version of Python? In Windows, it's a .chm file, a compiled help file, but I'm sure the same information is available on other distributions.. Anyway, sets was a module introduced in Python 2.3, and deprecated in Python 2.6, where it's replaced by the built-in type set. So the answer of whether you need to import or not depends on what version you're running. In Python 2.5, it's (untested) import sets data = sets.Set(mylist) In Python 2.6 it's data = set(mylist) The name datetime is both a module and a class. import datetime #import the module obj = datetime.datetime() #use the class within the module you just imported You can find the same information online, at least for version 2.6: Sometimes I'll use google to find stuff specifically on python.org, typing the following at the google serarch prompt:: datetime site:python.org the first link in the results page is: http://docs.python.org/library/datetime.html which is the online version of the 2.6.4 help Other useful links for the 2.6.4 documentation: http://docs.python.org/modindex.html to search for modules http://docs.python.org/genindex.html to search for anything Each of these pages has a quick search option. For example, I found the following page: http://docs.python.org/library/stdtypes.html#set DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
Victor Subervi wrote: Hi; What type does python consider a MySQL Set?? Let's take a look: import MySQLdb conn = MySQLdb.connect(db=carsten, user=blah, passwd=blah) cur = conn.cursor() cur.execute( ... create table pizza ( ... id integer, ... toppings set('cheese','sauce','peperoni','mushrooms') ... ) ... ) 0L cur.execute( ... insert into pizza values(1, 'cheese,sauce,peperoni') ... ) 1L cur.execute(select * from pizza) 1L rows = cur.fetchall() toppings = rows[0][1] print toppings cheese,sauce,peperoni print type(toppings) type 'str' Looks like a string to me. -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
On Sat, Dec 12, 2009 at 5:11 PM, Carsten Haese carsten.ha...@gmail.comwrote: Victor Subervi wrote: Hi; What type does python consider a MySQL Set?? Let's take a look: import MySQLdb conn = MySQLdb.connect(db=carsten, user=blah, passwd=blah) cur = conn.cursor() cur.execute( ... create table pizza ( ... id integer, ... toppings set('cheese','sauce','peperoni','mushrooms') ... ) ... ) 0L cur.execute( ... insert into pizza values(1, 'cheese,sauce,peperoni') ... ) 1L cur.execute(select * from pizza) 1L rows = cur.fetchall() toppings = rows[0][1] print toppings cheese,sauce,peperoni print type(toppings) type 'str' Looks like a string to me. Yep, sure does, but it isn't. Again: if isinstance(colValue[0], (str, int, long, float, long, complex, unicode, list, buffer, xrange, tuple)): pass else: print 'XXX' and those strings printed triple-X. It ain't no string. Besides, if it were a string, I'd be able to slice it, wouldn't I? Can't slice it either. V -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
On Sat, Dec 12, 2009 at 6:07 PM, Victor Subervi victorsube...@gmail.comwrote: On Sat, Dec 12, 2009 at 5:11 PM, Carsten Haese carsten.ha...@gmail.comwrote: Victor Subervi wrote: Hi; What type does python consider a MySQL Set?? Let's take a look: import MySQLdb conn = MySQLdb.connect(db=carsten, user=blah, passwd=blah) cur = conn.cursor() cur.execute( ... create table pizza ( ... id integer, ... toppings set('cheese','sauce','peperoni','mushrooms') ... ) ... ) 0L cur.execute( ... insert into pizza values(1, 'cheese,sauce,peperoni') ... ) 1L cur.execute(select * from pizza) 1L rows = cur.fetchall() toppings = rows[0][1] print toppings cheese,sauce,peperoni print type(toppings) type 'str' Looks like a string to me. Yep, sure does, but it isn't. Again: if isinstance(colValue[0], (str, int, long, float, long, complex, unicode, list, buffer, xrange, tuple)): pass else: print 'XXX' and those strings printed triple-X. It ain't no string. Besides, if it were a string, I'd be able to slice it, wouldn't I? Can't slice it either. V PS: Changed the code to this: elif col[:3] != 'pic': if isinstance(colValue[0], (str, int, long, float, long, complex, unicode, list, buffer, xrange, tuple)): pass else: print 'XXX' if col == 'sizes': # One of those lovely sets print colValue[0][0] throws this lovely error: /var/www/html/angrynates.com/cart/display.py 96 raise 97 cursor.close() 98 bottom() 99 100 display() display = function display /var/www/html/angrynates.com/cart/display.py in display() 50 print 'XXX' 51 if col == 'sizes': 52 print colValue[0][0] 53 #ourValue = string.split(colValue[0][6:-3], ',') 54 # print ourValue colValue = (Set(['Small', 'Extra-small', 'Medium']),) TypeError: unindexable object args = ('unindexable object',) V -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
Victor Subervi wrote: Yep, sure does, but it isn't. Again: if isinstance(colValue[0], (str, int, long, float, long, complex, unicode, list, buffer, xrange, tuple)): pass else: print 'XXX' and those strings printed triple-X. It ain't no string. Besides, if it were a string, I'd be able to slice it, wouldn't I? Can't slice it either. Well, then colValue[0] is not a string. This in turn probably means that colValue[0] does not represent the contents of a SET-type column. However, I can't tell you what it actually is, because you're once again not providing enough information. We'd need to see where colValue is coming from to find out what colValue[0] is. -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
Victor Subervi wrote: On Sat, Dec 12, 2009 at 5:11 PM, Carsten Haese carsten.ha...@gmail.com mailto:carsten.ha...@gmail.com wrote: Victor Subervi wrote: Hi; What type does python consider a MySQL Set?? Let's take a look: [snip] Looks like a string to me. Yep, sure does, but it isn't. Again: if isinstance(colValue[0], (str, int, long, float, long, complex, unicode, list, buffer, xrange, tuple)): pass else: print 'XXX' and those strings printed triple-X. It ain't no string. Besides, if it were a string, I'd be able to slice it, wouldn't I? Can't slice it either. If you want to know what type colValue[0] is, print type(colValue[0]). -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating MySQL Sets
Victor Subervi wrote: PS: Changed the code to this: elif col[:3] != 'pic': if isinstance(colValue[0], (str, int, long, float, long, complex, unicode, list, buffer, xrange, tuple)): pass else: print 'XXX' if col == 'sizes': # One of those lovely sets print colValue[0][0] throws this lovely error: /var/www/html/angrynates.com/cart/display.py http://angrynates.com/cart/display.py 96 raise 97 cursor.close() 98 bottom() 99 100 display() display = function display /var/www/html/angrynates.com/cart/display.py http://angrynates.com/cart/display.py in display() 50 print 'XXX' 51 if col == 'sizes': 52 print colValue[0][0] 53 #ourValue = string.split(colValue[0][6:-3], ',') 54 # print ourValue colValue = (Set(['Small', 'Extra-small', 'Medium']),) TypeError: unindexable object args = ('unindexable object',) The traceback helpfully shows us that colValue is a 1-tuple whose zeroth entry, colValue[0], is an actual bona-fide Python Set object. Such objects aren't indexable, because sets are unordered. That still doesn't tell us where colValue is coming from, though, so why it is a Python Set object when you expected it to be a string remains an unsolved mystery. -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list