[Tutor] Python debugger
Hi there. I've just started using the python debugger and I wonder how I could have lived without it earlier. I just wonder if there is a smarter way to show what all the variables contain in any given point in the debugger. I'm using this approach: import sys f = sys._getframe() p f.f_locals.items() It's not exacly pretty in its output, but it's better than nothing. Can this be done in a smarter way? -- Kind regards Michael B. Arp Sørensen Programmer / BOFH Ride out and meet them. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python debugger
On Fri, Jan 9, 2009 at 3:27 AM, Michael Bernhard Arp Sørensen mich...@arpsorensen.dk wrote: Hi there. I've just started using the python debugger and I wonder how I could have lived without it earlier. I just wonder if there is a smarter way to show what all the variables contain in any given point in the debugger. I'm using this approach: import sys f = sys._getframe() p f.f_locals.items() It's not exacly pretty in its output, but it's better than nothing. Can this be done in a smarter way? 'a' will show the arguments to the current function; not the same but shorter. p locals().items() is probably the same as what you are doing. You might want to look at a graphical debugger such as winpdb. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python debugger
Hi. Thanks for the input. Your way of doing it is simpler and possible to use as an alias in pdb. I can't use a graphical debugger because i mostly code python over ssh on remote servers in my company. Thanks anyway. It was actually helpfull. :-) /Michael On Fri, Jan 9, 2009 at 12:38 PM, Kent Johnson ken...@tds.net wrote: On Fri, Jan 9, 2009 at 3:27 AM, Michael Bernhard Arp Sørensen mich...@arpsorensen.dk wrote: Hi there. I've just started using the python debugger and I wonder how I could have lived without it earlier. I just wonder if there is a smarter way to show what all the variables contain in any given point in the debugger. I'm using this approach: import sys f = sys._getframe() p f.f_locals.items() It's not exacly pretty in its output, but it's better than nothing. Can this be done in a smarter way? 'a' will show the arguments to the current function; not the same but shorter. p locals().items() is probably the same as what you are doing. You might want to look at a graphical debugger such as winpdb. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] casting string to integer in a list of lists
Hi Your, I work with genomic datasets as well and have recently only started working with python (so my advice is a bit naive) I would say although there may be different ways you can cast an integer or float type into your list of lists you may actually no need to do so with your starting file/list... ie: if for example you want to do stuff with the location chrX with start 160944034 and end 160944035 you could: for x in LoL: startstring=LoL[1] #this would be '160944034' startint=int(startstring) #this would be 160944034 now if you also use a counter you can iterate and do all sort of calculations for different genomic locations. I use the csv module and then append into a numpy array that takes type float, so you could work with that as well, cheers Dr Triantafyllos Gkikopoulos culpritNr1 ig2ar-s...@yahoo.co.uk 01/08/09 8:42 PM Hi All, Say I have this nice list of lists: LoL = [['chrX', '160944034', '160944035', 'gnfX.145.788', '63.60'], ['chrX', '161109992', '161109993', 'rs13484104', '63.60'], ['chrX', '161414112', '161414113', 'rs13484105', '63.60'], ['chrX', '161544071', '161544072', 'rs13484106', '63.60'], ['chrX', '162030736', '162030737', 'gnfX.146.867', '67.05'], ['chrX', '164171913', '164171914', 'gnfX.148.995', '70.45']] Now I want to cast the second and third columns from string to integer, like this LoL = [['chrX', 160944034, 160944035, 'gnfX.145.788', '63.60'], ['chrX', 161109992, 161109993, 'rs13484104', '63.60'], ['chrX', 161414112, 161414113, 'rs13484105', '63.60'], ['chrX', 161544071, 161544072, 'rs13484106', '63.60'], ['chrX', 162030736, 162030737, 'gnfX.146.867', '67.05'], ['chrX', 164171913, 164171914, 'gnfX.148.995', '70.45']] Is there any elegant way to do this? I can't assume that all lines will have the same number of elements. Thank you, Your Culprit -- View this message in context: http://www.nabble.com/casting-string-to-integer-in-a-list-of-lists-tp21359600p21359600.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor The University of Dundee is a registered Scottish charity, No: SC015096 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python debugger
On Fri, Jan 9, 2009 at 7:36 AM, Michael Bernhard Arp Sørensen mich...@arpsorensen.dk wrote: I can't use a graphical debugger because i mostly code python over ssh on remote servers in my company. Winpdb opens a socket connection between the debugger and debuggee. Perhaps it would run over an ssh tunnel... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] casting string to integer in a list of lists
Hello Trias and all, Glad to see that somebody recognized the BED genomic annotation format. Your naive approach is probably the first thing that one could try. It is sure to work. The problem is that your code becomes unnecessarily long/cumbersome: every time I would have to use a particular element I would have to cast it first. So, an alternative idea is to modify the entire list of lists and get done with once and for all. This could be done manually with a couple of FOR loops. It would be easy and somebody already provided a solution early on (thank you!). Being a relative newcomer to python, my question was if there was an ELEGANT way to do this casting, perhaps as a list comprehension operation. I wondered if the beauty of python could reach that far. I thank you all for your comments, culpritNr1 trias wrote: Hi Your, I work with genomic datasets as well and have recently only started working with python (so my advice is a bit naive) I would say although there may be different ways you can cast an integer or float type into your list of lists you may actually no need to do so with your starting file/list... ie: if for example you want to do stuff with the location chrX with start 160944034 and end 160944035 you could: for x in LoL: startstring=LoL[1] #this would be '160944034' startint=int(startstring) #this would be 160944034 now if you also use a counter you can iterate and do all sort of calculations for different genomic locations. I use the csv module and then append into a numpy array that takes type float, so you could work with that as well, cheers Dr Triantafyllos Gkikopoulos culpritNr1 ig2ar-s...@yahoo.co.uk 01/08/09 8:42 PM Hi All, Say I have this nice list of lists: LoL = [['chrX', '160944034', '160944035', 'gnfX.145.788', '63.60'], ['chrX', '161109992', '161109993', 'rs13484104', '63.60'], ['chrX', '161414112', '161414113', 'rs13484105', '63.60'], ['chrX', '161544071', '161544072', 'rs13484106', '63.60'], ['chrX', '162030736', '162030737', 'gnfX.146.867', '67.05'], ['chrX', '164171913', '164171914', 'gnfX.148.995', '70.45']] Now I want to cast the second and third columns from string to integer, like this LoL = [['chrX', 160944034, 160944035, 'gnfX.145.788', '63.60'], ['chrX', 161109992, 161109993, 'rs13484104', '63.60'], ['chrX', 161414112, 161414113, 'rs13484105', '63.60'], ['chrX', 161544071, 161544072, 'rs13484106', '63.60'], ['chrX', 162030736, 162030737, 'gnfX.146.867', '67.05'], ['chrX', 164171913, 164171914, 'gnfX.148.995', '70.45']] Is there any elegant way to do this? I can't assume that all lines will have the same number of elements. Thank you, Your Culprit -- View this message in context: http://www.nabble.com/casting-string-to-integer-in-a-list-of-lists-tp21359600p21359600.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor The University of Dundee is a registered Scottish charity, No: SC015096 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- View this message in context: http://www.nabble.com/casting-string-to-integer-in-a-list-of-lists-tp21359600p21373193.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] casting string to integer in a list of lists
Le Fri, 9 Jan 2009 06:20:26 -0800 (PST), culpritNr1 ig2ar-s...@yahoo.co.uk a écrit : Hello Trias and all, Glad to see that somebody recognized the BED genomic annotation format. Being a relative newcomer to python, my question was if there was an ELEGANT way to do this casting, perhaps as a list comprehension operation. I wondered if the beauty of python could reach that far. already given such a solution -- denis I thank you all for your comments, culpritNr1 -- la vida e estranya ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] casting string to integer in a list of lists
I am not an expert and don't know if this is considered 'elegant', but this is what I would try conv = [[j[0], int(j[1]), int(j[2])] + j[3:] for j in LoL] Hi Your, I work with genomic datasets as well and have recently only started working with python (so my advice is a bit naive) I would say although there may be different ways you can cast an integer or float type into your list of lists you may actually no need to do so with your starting file/list... ie: if for example you want to do stuff with the location chrX with start 160944034 and end 160944035 you could: for x in LoL: startstring=LoL[1] #this would be '160944034' startint=int(startstring) #this would be 160944034 now if you also use a counter you can iterate and do all sort of calculations for different genomic locations. I use the csv module and then append into a numpy array that takes type float, so you could work with that as well, cheers Dr Triantafyllos Gkikopoulos culpritNr1 ig2ar-s...@yahoo.co.uk 01/08/09 8:42 PM Hi All, Say I have this nice list of lists: LoL = [['chrX', '160944034', '160944035', 'gnfX.145.788', '63.60'], ['chrX', '161109992', '161109993', 'rs13484104', '63.60'], ['chrX', '161414112', '161414113', 'rs13484105', '63.60'], ['chrX', '161544071', '161544072', 'rs13484106', '63.60'], ['chrX', '162030736', '162030737', 'gnfX.146.867', '67.05'], ['chrX', '164171913', '164171914', 'gnfX.148.995', '70.45']] Now I want to cast the second and third columns from string to integer, like this LoL = [['chrX', 160944034, 160944035, 'gnfX.145.788', '63.60'], ['chrX', 161109992, 161109993, 'rs13484104', '63.60'], ['chrX', 161414112, 161414113, 'rs13484105', '63.60'], ['chrX', 161544071, 161544072, 'rs13484106', '63.60'], ['chrX', 162030736, 162030737, 'gnfX.146.867', '67.05'], ['chrX', 164171913, 164171914, 'gnfX.148.995', '70.45']] Is there any elegant way to do this? I can't assume that all lines will have the same number of elements. Thank you, Your Culprit -- View this message in context: http://www.nabble.com/casting-string-to-integer-in-a-list-of-lists-tp21359600p21359600.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor The University of Dundee is a registered Scottish charity, No: SC015096 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] What does OP stand for?
Hello, I'm new to this list, and am enjoying it. I just wonder what OP stands for, as I have seen quite a few mentions in the python lists Thanks Edu ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] casting string to integer in a list of lists
Hello Denis and All, Your solution does show elegance. To remind people, it's this one: lol = [[1,2,3],[4,5,6],[7,8,9]] new_lol = [[a,b**3,c] for [a,b,c] in lol] print lol print new_lol == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] [[1, 8, 3], [4, 125, 6], [7, 512, 9]] Now, as I said in my original post, I can't assume that all inner-lists will have the same number of elements. Common data looks like this LoL = [['chrX', '160944034', '160944035', 'gnfX.145.788', '63.60'], ['chrX', '161109992', '161109993', 'rs13484104', '63.60'], ['chrX', '161414112', '161414113', 'undetermined'], ['chrX', '161544071', '161544072', 'rs13484106', '63.60', 'RNA-BP', 'PLoS Biol 6(10): e255'], ['chrX', '162030736', '162030737', 'gnfX.146.867', '67.05'], ['chrX', '164171913', '164171914', 'gnfX.148.995', '70.45']] Do you still think that this problem can be elegantly tackled by list comprehensions? Thank you, culpritNr1 spir wrote: Le Thu, 8 Jan 2009 11:51:01 -0800 (PST), culpritNr1 ig2ar-s...@yahoo.co.uk a écrit : Hi All, Say I have this nice list of lists: LoL = [['chrX', '160944034', '160944035', 'gnfX.145.788', '63.60'], ['chrX', '161109992', '161109993', 'rs13484104', '63.60'], ['chrX', '161414112', '161414113', 'rs13484105', '63.60'], ['chrX', '161544071', '161544072', 'rs13484106', '63.60'], ['chrX', '162030736', '162030737', 'gnfX.146.867', '67.05'], ['chrX', '164171913', '164171914', 'gnfX.148.995', '70.45']] Now I want to cast the second and third columns from string to integer, like this LoL = [['chrX', 160944034, 160944035, 'gnfX.145.788', '63.60'], ['chrX', 161109992, 161109993, 'rs13484104', '63.60'], ['chrX', 161414112, 161414113, 'rs13484105', '63.60'], ['chrX', 161544071, 161544072, 'rs13484106', '63.60'], ['chrX', 162030736, 162030737, 'gnfX.146.867', '67.05'], ['chrX', 164171913, 164171914, 'gnfX.148.995', '70.45']] Is there any elegant way to do this? I can't assume that all lines will have the same number of elements. In you do not need checking whether specific items are castable (i.e. you really know what's in the list) you may use such a pattern: lol = [[1,2,3],[4,5,6],[7,8,9]] new_lol = [[a,b**3,c] for [a,b,c] in lol] print lol print new_lol == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] [[1, 8, 3], [4, 125, 6], [7, 512, 9]] denis Thank you, Your Culprit -- la vida e estranya ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- View this message in context: http://www.nabble.com/casting-string-to-integer-in-a-list-of-lists-tp21359600p21375207.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] casting string to integer in a list of lists
Thanks Daniel, That is EXACTLY what I was looking for. Actually, int() does not really work but this does: [ [line[0], eval(line[1]), eval(line[2])] + line[3:] for line in LoL] Again, thanks. culpritNr1 Daniel Sarmiento-2 wrote: I am not an expert and don't know if this is considered 'elegant', but this is what I would try conv = [[j[0], int(j[1]), int(j[2])] + j[3:] for j in LoL] Hi Your, I work with genomic datasets as well and have recently only started working with python (so my advice is a bit naive) I would say although there may be different ways you can cast an integer or float type into your list of lists you may actually no need to do so with your starting file/list... ie: if for example you want to do stuff with the location chrX with start 160944034 and end 160944035 you could: for x in LoL: startstring=LoL[1] #this would be '160944034' startint=int(startstring) #this would be 160944034 now if you also use a counter you can iterate and do all sort of calculations for different genomic locations. I use the csv module and then append into a numpy array that takes type float, so you could work with that as well, cheers Dr Triantafyllos Gkikopoulos culpritNr1 ig2ar-s...@yahoo.co.uk 01/08/09 8:42 PM Hi All, Say I have this nice list of lists: LoL = [['chrX', '160944034', '160944035', 'gnfX.145.788', '63.60'], ['chrX', '161109992', '161109993', 'rs13484104', '63.60'], ['chrX', '161414112', '161414113', 'rs13484105', '63.60'], ['chrX', '161544071', '161544072', 'rs13484106', '63.60'], ['chrX', '162030736', '162030737', 'gnfX.146.867', '67.05'], ['chrX', '164171913', '164171914', 'gnfX.148.995', '70.45']] Now I want to cast the second and third columns from string to integer, like this LoL = [['chrX', 160944034, 160944035, 'gnfX.145.788', '63.60'], ['chrX', 161109992, 161109993, 'rs13484104', '63.60'], ['chrX', 161414112, 161414113, 'rs13484105', '63.60'], ['chrX', 161544071, 161544072, 'rs13484106', '63.60'], ['chrX', 162030736, 162030737, 'gnfX.146.867', '67.05'], ['chrX', 164171913, 164171914, 'gnfX.148.995', '70.45']] Is there any elegant way to do this? I can't assume that all lines will have the same number of elements. Thank you, Your Culprit -- View this message in context: http://www.nabble.com/casting-string-to-integer-in-a-list-of-lists-tp21359600p21359600.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor The University of Dundee is a registered Scottish charity, No: SC015096 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- View this message in context: http://www.nabble.com/casting-string-to-integer-in-a-list-of-lists-tp21359600p21375430.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What does OP stand for?
Eduardo Vieira wrote: Hello, I'm new to this list, and am enjoying it. I just wonder what OP stands for, as I have seen quite a few mentions in the python lists Original Poster - ie whoever it was who first raised the question we're discussing now. In the case of this thread, you're the OP. TJG ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What does OP stand for?
Eduardo Vieira wrote: Hello, I'm new to this list, and am enjoying it. I just wonder what OP stands for, as I have seen quite a few mentions in the python lists Original Poster - ie whoever it was who first raised the question we're discussing now. In the case of this thread, you're the OP. TJG ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] casting string to integer in a list of lists
On Fri, Jan 9, 2009 at 11:23 AM, culpritNr1 ig2ar-s...@yahoo.co.uk wrote: That is EXACTLY what I was looking for. Actually, int() does not really work but this does: [ [line[0], eval(line[1]), eval(line[2])] + line[3:] for line in LoL] That's strange. What happened when you tried int() ? What version of Python are you using? Kent PS Please subscribe to the list ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] casting string to integer in a list of lists
Le Fri, 9 Jan 2009 08:10:27 -0800 (PST), culpritNr1 ig2ar-s...@yahoo.co.uk a écrit : Hello Denis and All, Your solution does show elegance. To remind people, it's this one: lol = [[1,2,3],[4,5,6],[7,8,9]] new_lol = [[a,b**3,c] for [a,b,c] in lol] print lol print new_lol == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] [[1, 8, 3], [4, 125, 6], [7, 512, 9]] Now, as I said in my original post, I can't assume that all inner-lists will have the same number of elements. Common data looks like this LoL = [['chrX', '160944034', '160944035', 'gnfX.145.788', '63.60'], ['chrX', '161109992', '161109993', 'rs13484104', '63.60'], ['chrX', '161414112', '161414113', 'undetermined'], ['chrX', '161544071', '161544072', 'rs13484106', '63.60', 'RNA-BP', 'PLoS Biol 6(10): e255'], ['chrX', '162030736', '162030737', 'gnfX.146.867', '67.05'], ['chrX', '164171913', '164171914', 'gnfX.148.995', '70.45']] Do you still think that this problem can be elegantly tackled by list comprehensions? The straightforward method is indeed to to loop with neccsary checks/trials embedded in loop. Now, there imo is more elegant method which is to define a custom int that integrates such tests. And then to call this custom conversion instead of built-in int. This lets open the possibility of a list comp expression. Below an example: def possible_cube(val): try: return val ** 3 except: return val lol = [[1,2,'x'],[4,'y',6],['z',8,9]] new_lol = [[possible_cube(val) for val in l] for l in lol] print lol print new_lol == [[1, 2, 'x'], [4, 'y', 6], ['z', 8, 9]] [[1, 8, 'x'], [64, 'y', 216], ['z', 512, 729]] This may be longer because there is a custom function call in each loop. Now, it's a question of taste and priority. denis -- la vida e estranya ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] casting string to integer in a list of lists
Forgot a detail def possible_cube(val): try: return val ** 3 except TypeError: return val denis ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] casting string to integer in a list of lists
A tuple of exceptions works, just like what we did above, and more, i.e., (IndexError, ValueError, TypeError, KeyError... Thank you, thank you, thank you! I'm sure it's been staring me in the face, but I never realized I could use a tuple of exception types - that's why I said it was a pain in the butt, trapping the exception and doing isinstance checks against it. How did I miss this? wow, you were really going out of your way. i believe the docs have some examples but cannot confirm this. Learn something new every day, no? even i do. oh, here's something else for the OP... i forgot to mention there is a useful string method called isdigit() that you can use on the string in question. it returns True if all the chars in the string are digits, meaning that it's likely safe to call int() on it. cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Core Python Programming, Prentice Hall, (c)2007,2001 Python Fundamentals, Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] casting string to integer in a list of lists
Hello Kent and All, Errata: int() does work. I think that in my test code a number such as '4.5' might have slipped in and then int() protested. eval() worked in all my attempts. So, thanks Daniel Sarmiento, your solution is correct as is. By the way, when I said 'cast' I really meant 'convert'. Sorry, my badness. Thanks for pointing that out. culpritNr1 Kent Johnson wrote: On Fri, Jan 9, 2009 at 11:23 AM, culpritNr1 ig2ar-s...@yahoo.co.uk wrote: That is EXACTLY what I was looking for. Actually, int() does not really work but this does: [ [line[0], eval(line[1]), eval(line[2])] + line[3:] for line in LoL] That's strange. What happened when you tried int() ? What version of Python are you using? Kent PS Please subscribe to the list ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- View this message in context: http://www.nabble.com/casting-string-to-integer-in-a-list-of-lists-tp21359600p21377706.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Where's Psyco now?
Hello. This is my first message to the list. In this article written in 2002 http://www.ibm.com/developerworks/library/l-psyco.html they talk about Psyco as a module that makes it possible to accelerate Python. Is it still a state-of-the-art module? I found it here also: http://pypi.python.org/pypi/psyco/1.6 Do you think it's useful, or it depends...? -- Vicent Giner-Bosch, Valencia, Spain ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Where's Psyco now?
On Fri, Jan 9, 2009 at 10:41 AM, Vicent vgi...@gmail.com wrote: Hello. This is my first message to the list. In this article written in 2002 http://www.ibm.com/developerworks/library/l-psyco.html they talk about Psyco as a module that makes it possible to accelerate Python. Is it still a state-of-the-art module? I found it here also: http://pypi.python.org/pypi/psyco/1.6 Do you think it's useful, or it depends...? Its present homepage is http://psyco.sourceforge.net/ , and yes, it is still useful and fairly current. However, it should be used judiciously; only bother with it if you're having a performance problem and would otherwise be tempted to rewrite the algorithm in C. Don't casually use it as an all-purpose accelerator, as that's not what it was built for. Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] website login
Hi everyone, I have a python question i would like to ask. Im trying to write a script to login to a website and mirror it. I need to submit a post request to /login.php which is fine, but how can i access other pages on the site. For example data = urllib.urlencode({username : myUser, password : myPass}) urllib.urlopen(http://www.example.com/login.php;, data) page = urllib.urlopen(http://www.example.com/page_on_site.html;) when look page it is just showing me login.php Thanks for any advice you can give. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] changing string in place
Dear all, I've been around and around with this and can't seem to conceptualize it properly. I've got a javascript object in a text file that I'd like to treat as json so that I can import it into a python program via simplejson.loads(); however, it's not proper json because it has new Date() object declarations in it. So I thought to pre-process it by translating the dates into ISO format, but RE is making me cross-eyed. example string: s = {title : Hebertot, Normandie, start : new Date(1203,10,7), description : Hardy's long name: Hebertot, Normandie. lt;brgt; lt;img src=\document.png\ style=\cursor: pointer\ onclick=\SimileAjax.WindowManager.cancelPopups();show_next('tab3');pager('035'); return false\/gt;pg.035: 1203-10-10 to 1203-11-18},{title : Newark-Upon-Trent, Nottinghamshire, start : new Date(1216,9,16), end : new Date(1216,9,18), description : Hardy's long name: Newark-Upon-Trent, Nottinghamshire. lt;brgt; } I can locate the dates with: jdate = re.compile('new Date\(\d{4},\d{1,2},\d{1,2}\)') and: jsdates = jdate.findall(s) I can get a regex pattern that groups the necessary elements: dateElems = re.compile('(\d{4}),(\d{1,2}),(\d{1,2})') But I can't seem to put the bits together with re.sub() (or str.replace() ?) in the right sort of loop. How can I return the string with _all_ the dates changed in place thus: {title : Hebertot, Normandie, start : 1203-11-07... etc. instead of {title : Hebertot, Normandie, start : new Date(1203,10,7)... etc. (js dates are 0 indexed, so Date(1203,10,7) is Nov. 7, 1203) I'm sure this is a simple matter, but I'm just not looking at it right. Jon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] changing string in place
Jon Crump wrote: Dear all, I've been around and around with this and can't seem to conceptualize it properly. I've got a javascript object in a text file that I'd like to treat as json so that I can import it into a python program via simplejson.loads(); however, it's not proper json because it has new Date() object declarations in it. So I thought to pre-process it by translating the dates into ISO format, but RE is making me cross-eyed. example string: s = {title : Hebertot, Normandie, start : new Date(1203,10,7), description : Hardy's long name: Hebertot, Normandie. lt;brgt; lt;img src=\document.png\ style=\cursor: pointer\ onclick=\SimileAjax.WindowManager.cancelPopups();show_next('tab3');pager('035'); return false\/gt;pg.035: 1203-10-10 to 1203-11-18},{title : Newark-Upon-Trent, Nottinghamshire, start : new Date(1216,9,16), end : new Date(1216,9,18), description : Hardy's long name: Newark-Upon-Trent, Nottinghamshire. lt;brgt; } I can locate the dates with: jdate = re.compile('new Date\(\d{4},\d{1,2},\d{1,2}\)') I think you're pretty close... # note the extra parens jdate = re.compile('new Date\((\d{4}),(\d{1,2}),(\d{1,2})\)') ... then ... print jdate.sub(r'\1-\2-\3', s) {title : Hebertot, Normandie, start : 1203-10-7, description : Hardy's long name: Hebertot, Normandie. lt;brgt; lt;img src=document.png style=cursor: pointer onclick=SimileAjax.WindowManager.cancelPopups();show_next('tab3');pager('035'); return false/gt;pg.035: 1203-10-10 to 1203-11-18},{title : Newark-Upon-Trent, Nottinghamshire, start : 1216-9-16, end : 1216-9-18, description : Hardy's long name: Newark-Upon-Trent, Nottinghamshire. lt;brgt; } You can also use named groups which *might* help make clearer what's happening above, something like: jdate = re.compile('new Date\((?PYear\d{4}),(?PMonth\d{1,2}),(?PDay\d{1,2})\)') print jdate.sub(r'\gYear-\gMonth-\gDay', s) More info here: http://docs.python.org/library/re.html#re.sub HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] website login
On Fri, Jan 9, 2009 at 6:53 PM, phr34kc0der phr34kc0...@gmx.co.uk wrote: Hi everyone, I have a python question i would like to ask. Im trying to write a script to login to a website and mirror it. I need to submit a post request to /login.php which is fine, but how can i access other pages on the site. See the 'Authentication' section on this page: http://personalpages.tds.net/~kent37/kk/00010.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] changing string in place
Jon Crump wrote: I'm still faced with the problem of the javascript months being 0 indexed. I have to add 1 to group \2 in order to get my acurate date-string. Obviously I can't do print jdate.sub(r'\1-\2+1-\3', s) because the first argument to sub() is a string. How can I act on \2 before it's substituted for the matched string? Ah, sorry I missed that part the first time through. The first argument to jdate.sub ('repl') can also be a function that accepts an re.match object and returns a string, so something like the following (*untested*) may be helpful: jdate = re.compile('new Date\((\d{4}),(\d{1,2}),(\d{1,2})\)') def repldate(match): y, m, d = map(int, match.groups()) return '%04d-%02d-%02d' % (y, m+1, d) print jdate.sub(repldate, s) HTH, Marty Thanks again, Jon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor