Raw string fu
r'\' File stdin, line 1 r'\' ^ SyntaxError: EOL while scanning single-quoted string r'\\' '' Does that seem wrong to anybody else? Shouldn't the first one be syntactically correct? -jag -- http://mail.python.org/mailman/listinfo/python-list
Lambda evaluation
So this part makes total sense to me: d = {} for x in [1,2,3]: ... d[x] = lambda y: y*x ... d[1](3) 9 Because x in the lambda definition isn't evaluated until the lambda is executed, at which point x is 3. Is there a way to specifically hard code into that lambda definition the contemporary value of an external variable? In other words, is there a way to rewrite the line d[x] = lambda y: y*x so that it is always the case that d[1](3) = 3? Thanks! -jag -- http://mail.python.org/mailman/listinfo/python-list
Re: Lambda evaluation
That's a damned elegant solution -- thank you... However in trying to simplify my problem for presentation here, I think I oversimplified a bit too much. Try this one: d = {} for x in [1,2,3]: ... d[x] = lambda *args: args[0]*x ... d[1](3) 9 The lambda is going to have to take arbitrary arguments, so I can't specify x=x before the arbitrary arguments (otherwise, it gets overridden) and I can't specify it afterward (syntax error). :-/ Thanks! -jag On Thu, 2005-10-06 at 16:27 -0400, Jp Calderone wrote: On Thu, 06 Oct 2005 16:18:15 -0400, Joshua Ginsberg [EMAIL PROTECTED] wrote: So this part makes total sense to me: d = {} for x in [1,2,3]: ... d[x] = lambda y: y*x ... d[1](3) 9 Because x in the lambda definition isn't evaluated until the lambda is executed, at which point x is 3. Is there a way to specifically hard code into that lambda definition the contemporary value of an external variable? In other words, is there a way to rewrite the line d[x] = lambda y: y*x so that it is always the case that d[1](3) = 3? There are several ways, but this one involves the least additional typing: d = {} for x in 1, 2, 3: ... d[x] = lambda y, x=x: y * x ... d[1](3) 3 Who needs closures, anyway? :) Jp -- http://mail.python.org/mailman/listinfo/python-list
Lambda with copy.deepcopy()
Howdy -- I have a class that has an attribute that is a dictionary that contains an object that has a kword argument that is a lambda. Confused yet? Simplified example: import copy class Foo: def __init__(self, fn=None): self.fn = fn class Bar: d = {'foobar': Foo(fn=lambda x: x*x)} def cp(self): self.xerox = copy.deepcopy(self.d) When I execute: b = Bar() b.cp() Using Python version: Python 2.3 (#1, Sep 13 2003, 00:49:11) [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin I get: Traceback (most recent call last): File stdin, line 1, in ? File stdin, line 5, in cp File /System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/copy.py, line 179, in deepcopy y = copier(x, memo) File /System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/copy.py, line 270, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File /System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/copy.py, line 179, in deepcopy y = copier(x, memo) File /System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/copy.py, line 307, in _deepcopy_inst state = deepcopy(state, memo) File /System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/copy.py, line 179, in deepcopy y = copier(x, memo) File /System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/copy.py, line 270, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File /System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/copy.py, line 206, in deepcopy y = _reconstruct(x, rv, 1, memo) File /System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/copy.py, line 338, in _reconstruct y = callable(*args) File /System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/copy_reg.py, line 92, in __newobj__ return cls.__new__(cls, *args) TypeError: function() takes at least 2 arguments (0 given) I've googled for deepcopy and lambda and found somebody else asking the same question on a LUG somewhere, but they gave no advice and nobody else seems to have run into this. Any ideas on what the problem is/how to get around it? Thanks! -jag -- http://mail.python.org/mailman/listinfo/python-list
Iterators from urllib2
I'm a bit baffled by something... In a script I wrote, I have defined a function that runs urllib2.urlopen() on a urllib2.Request object and returns the file-like object. The code that calls this function attempts to build a csv.DictReader object based on that file-like object, but an error is thrown saying the file-like object is not an iterator. I could have sworn urllib2.urlopen returned an iterator, so I tested: Python 2.3 (#1, Sep 13 2003, 00:49:11) [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin Type help, copyright, credits or license for more information. import urllib2 r = urllib2.Request('http://www.google.com') ifs = urllib2.urlopen(r) dir(ifs) ['__doc__', '__init__', '__iter__', '__module__', '__repr__', 'close', 'fileno', 'fp', 'geturl', 'headers', 'info', 'next', 'read', 'readline', 'readlines', 'url'] Yep. But what about in my code? I modify my code to print dir(ifs) before creating the DictReader... ['__doc__', '__init__', '__module__', '__repr__', 'close', 'fp', 'geturl', 'headers', 'info', 'read', 'readline', 'url'] Traceback (most recent call last): File CSVParser.py, line 144, in ? print parseQHost(circuits[cktname], cktname) File CSVParser.py, line 126, in parseQHost r = csv.DictReader(ifs, fieldlist) File /System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/csv.py, line 100, in __init__ self.reader = reader(f, dialect, *args) TypeError: argument 1 must be an iterator Whoa! Where did the __iter__, readlines, and next attributes go? Ideas? -jag -- http://mail.python.org/mailman/listinfo/python-list
Weird...
{'a':1,'b':'2','c':[3,4]}.keys() ['a', 'c', 'b'] How come? :-) -jag Python 2.3.3 (#1, May 7 2004, 10:31:40) [GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2 inline: Pasted Graphic.tiffJoshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131-- http://mail.python.org/mailman/listinfo/python-list
Re: [Pythonmac-SIG] Fwd: PPC OSX vs. x86 Linux
Well, I compiled a fresh version of Python 2.3.5 from python.org to test the datetime theory... and I'm still getting 150sec execution times. :-/ I'm gonna test the string vs. strop now... -jag inline: Pasted Graphic.tiffJoshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131 On Apr 10, 2005, at 4:19 PM, Joshua Ginsberg wrote: I'll give that a shot. I'm interested to hear that -- I'm only using a single use of time.strptime in the code, but I somehow am skeptical that would create a 25-fold performance hit. :-) -jag Pasted Graphic.tiff Joshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131 On Apr 10, 2005, at 4:14 PM, Bob Ippolito wrote: On Apr 10, 2005, at 2:46 PM, Joshua Ginsberg wrote: I writing some python code to do some analysis of my mail logs. I took a 10,000 line snippet from them (the files are about 5-6 million usually) to test my code with. I'm developing it on a Powerbook G4 1.2GHz with 1.25GB of RAM and the Apple distributed Python* and I tested my code on the 10,000 line snippet. It took 2 minutes and 10 seconds to process that snippet. Way too slow -- I'd be looking at about 20 hours to process a single daily log file. Just for fun, I copied the same code and the same log snippet to a dual-proc P3 500MHz machine running Fedora Core 2* with 1GB of RAM and tested it there. This machine provides web services and domain control for my network, so it's moderately utilized. The same code took six seconds to execute. Granted I've got the GUI and all of that bogging down my Mac. However, I had nothing else fighting for CPU cycles and 700MB of RAM free when my testing was done. Even still, what would account for such a wide, wide, wide variation in the time required to process the data file? The code is 90% regular expressions and string finds. * versions are: Python 2.3 (#1, Sep 13 2003, 00:49:11) [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin and Python 2.3.3 (#1, May 7 2004, 10:31:40) [GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2 Try it with a newer version of Python on Mac OS X. I had a similar problem, and it turned out to be Python 2.3.0's fault. Specifically, the implementation of the datetime module's parser was really, really, really stupid and slow in early versions of Python 2.3. -bob -- http://mail.python.org/mailman/listinfo/python-list
Re: Https Form Page
It all really, really, really depends on your script processing the form. My suggestion is to use the openssl s_client program, manually submit your data, and see what your server is responding with. -jag inline: Pasted Graphic.tiffJoshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131 On Apr 9, 2005, at 12:04 AM, Hasan D wrote: I cant figure out, always gives me the form page. Everything looks ok but ? On Apr 9, 2005 2:07 AM, Joshua Ginsberg [EMAIL PROTECTED] wrote: If url is your formpage and your formdata is correct, it should work. Do you need to have a cookie set? Is it 304 redirecting you? -jag Joshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131 On Apr 8, 2005, at 4:51 PM, Hasan D wrote: Thank you for fast reply. Your program works fine. But when i print the https_response it gives me the form page. I want to send the form and print the result page. On Apr 9, 2005 12:19 AM, Joshua Ginsberg [EMAIL PROTECTED] wrote: Try: import urllib2, urllib formdata = urllib.urlencode({'a':1,'b':2}) request = urllib2.Request(url) request.add_data(formdata) opener = urllib2.build_opener(urllib2.HTTPSHandler()) urllib2.install_opener(opener) fs = urllib2.urlopen(request) https_response = fs.read() https_headers = fs.info().headers https_mimetype = fs.info().type -jag Joshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131 On Apr 8, 2005, at 2:13 PM, Hasan D wrote: I'm new on this httplib and urllib. Actually I dont know what should i use. I want to fill the form in a https page , and return the result . I write a test code but always gives errors. I cant find any good example about this on the net. What should I do about this ? import urlparse,urllib,httplib,string,htmllib,formatter #port=443 target=https://www.abc.com/; params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) http=httplib.HTTP(https://www.abc.com/,443) data='Name=xAdress=x' headers = {Content-type: application/x-www-form-urlencoded,Accept: text/plain} print Sending Data On +target+...\n http.putrequest(POST,target+/xxx.asp?q=7b=11,params) response = http.getresponse() print response.status, response.reason http.send(data) code,msg,headers = http.getreply() print HTTP Code : ,str(code) print HTTP Connection : ,msg print HTTP headers : \n,headers,\n HTML=http.getfile().read() MyParser=htmllib.HTMLParser(formatter.NullFormatter()) MyParser.feed(HTML) # Print all the anchors from the results page print MyParser.anchorlist -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
PPC OSX vs. x86 Linux
Hello -- I writing some python code to do some analysis of my mail logs. I took a 10,000 line snippet from them (the files are about 5-6 million usually) to test my code with. I'm developing it on a Powerbook G4 1.2GHz with 1.25GB of RAM and the Apple distributed Python* and I tested my code on the 10,000 line snippet. It took 2 minutes and 10 seconds to process that snippet. Way too slow -- I'd be looking at about 20 hours to process a single daily log file. Just for fun, I copied the same code and the same log snippet to a dual-proc P3 500MHz machine running Fedora Core 2* with 1GB of RAM and tested it there. This machine provides web services and domain control for my network, so it's moderately utilized. The same code took six seconds to execute. Granted I've got the GUI and all of that bogging down my Mac. However, I had nothing else fighting for CPU cycles and 700MB of RAM free when my testing was done. Even still, what would account for such a wide, wide, wide variation in the time required to process the data file? The code is 90% regular expressions and string finds. Theories? Thanks! -jag * versions are: Python 2.3 (#1, Sep 13 2003, 00:49:11) [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin and Python 2.3.3 (#1, May 7 2004, 10:31:40) [GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2 inline: Pasted Graphic.tiffJoshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131-- http://mail.python.org/mailman/listinfo/python-list
Re: redirect stdout
Use *NIX magic. Make a named pipe to a python program that pushes stdin to syslog and when you execute your program, redirect stdout to the named pipe. -jag inline: Pasted Graphic.tiffJoshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131 On Apr 8, 2005, at 10:52 AM, Neal Becker wrote: I'd like to build a module that would redirect stdout to send it to a logging module. I want to be able to use a python module that expects to print results using print or sys.stdout.write() and without modifying that module, be able to redirect it's stdout to a logger which will send the messages via datagrams to a server. Any ideas? -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Equivalent string.find method for a list of strings
Try: filter(lambda x: x.find(searchstring) != -1, lines) inline: Pasted Graphic.tiffJoshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131 On Apr 8, 2005, at 12:17 PM, jeremit0 wrote: I have read a text file using the command lines = myfile.readlines() and now I want to seach those lines for a particular string. I was hoping there was a way to find that string in a similar way as searching simply a simple string. I want to do something like lines.find.('my particular string') Is there a module that already exists that allows me to do this or will I have to created my own method? Thanks, Jeremy -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Equivalent string.find method for a list of strings
try: filter(lambda x: lines[x].find(searchstring) != -1, range(len(lines))) That will return a list with the indices of every line containing a hit for your search string. -jag inline: Pasted Graphic.tiffJoshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131 On Apr 8, 2005, at 1:52 PM, Jeremy Conlin wrote: Joshua Ginsberg wrote: Try: filter(lambda x: x.find(searchstring) != -1, lines) I really like this option, but what I want to know where in the file this line exists (i.e. line 42). Can I somehow access this line and the lines immediately following? Thanks, Jeremy -- http://mail.python.org/mailman/listinfo/python-list
Re: Https Form Page
Try: import urllib2, urllib formdata = urllib.urlencode({'a':1,'b':2}) request = urllib2.Request(url) request.add_data(formdata) opener = urllib2.build_opener(urllib2.HTTPSHandler()) urllib2.install_opener(opener) fs = urllib2.urlopen(request) https_response = fs.read() https_headers = fs.info().headers https_mimetype = fs.info().type -jag inline: Pasted Graphic.tiffJoshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131 On Apr 8, 2005, at 2:13 PM, Hasan D wrote: I'm new on this httplib and urllib. Actually I dont know what should i use. I want to fill the form in a https page , and return the result . I write a test code but always gives errors. I cant find any good example about this on the net. What should I do about this ? import urlparse,urllib,httplib,string,htmllib,formatter #port=443 target=https://www.abc.com/; params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) http=httplib.HTTP(https://www.abc.com/,443) data='Name=xAdress=x' headers = {Content-type: application/x-www-form-urlencoded,Accept: text/plain} print Sending Data On +target+...\n http.putrequest(POST,target+/xxx.asp?q=7b=11,params) response = http.getresponse() print response.status, response.reason http.send(data) code,msg,headers = http.getreply() print HTTP Code : ,str(code) print HTTP Connection : ,msg print HTTP headers : \n,headers,\n HTML=http.getfile().read() MyParser=htmllib.HTMLParser(formatter.NullFormatter()) MyParser.feed(HTML) # Print all the anchors from the results page print MyParser.anchorlist -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling
If you use except: without any specific error class, it will be a catchall for any error class (including Warning and its derivatives). Otherwise you can use inheritance to refer to a group of exceptions (e.g. except Exception: would catch ValueError, since the ValueError class inherits from the generic Exception class). -jag inline: Pasted Graphic.tiffJoshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131 On Apr 8, 2005, at 3:29 PM, SuperJared wrote: I'm new to Python, well versed in PHP and a bit of Perl. I've written a simple backup utility that FTPs from one server to another. I'd like to implement exception handling on the FTP should someting go wrong. This is basically what I have so far: try: ftp = FTP(ftp_host, ftp_user, ftp_pass) ftp.storbinary('STOR ' + filename, pointer) ftp.close() except: # do something with an error message here...? I'd like to use 'all_errors' but I don't know how! Will I have to create a handler for each other exception? -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list