Methods on file-like objects can only used once on one object?

2011-08-23 Thread Yingjie Lin
Hi Python users,

I just realize that my post yesterday shouldn't be specifically for mechanize. 
It should be a general question for file-like objects.

 f = open('my_file.txt')
 print f.readlines()
( prints a list of strings
 print f.readlines()
[]

There are quite a few methods for file-like objects that can only be used once 
on one object. If I prefer to use some of these methods on one object, one 
after another, like:

f.readlines()
f.read()
...

What should I do? Thank you.

- Yingjie







-- 
http://mail.python.org/mailman/listinfo/python-list


Help on instance of closeable_response in module Mechanize

2011-08-22 Thread Yingjie Lin
Hi Python users,

I have a question about the instance of closeable_response in module Mechanize.

from mechanize import ParseResponse, urlopen
url = http://wwwsearch.sourceforge.net/mechanize/example.html;
r = urlopen(url)
forms = ParseResponse(r, backwards_compat=False)
html_lines = r.read()

If I call ParseResponse() before r.read(), then lforms would be a list 
containing one form 
instance, and html_lines would be an empty string. If I call r.read() first, 
then html_lines 
would be the HTML source code of the page, but forms would be an empty list. 

Therefore, I have to open the url twice, once for each function, like this:

r = urlopen(url)
forms = ParseResponse(r, backwards_compat=False)
r = urlopen(url)
html_lines = r.read()

I believe this shouldn't be necessary. What is the proper way of doing it? 
Thank you.

- Yingjie







-- 
http://mail.python.org/mailman/listinfo/python-list


HTML client sctript

2011-08-19 Thread Yingjie Lin
Hi Python users,

I am maintaining a website written with Python CGI scripts. To make sure the 
website is working well, 
I would like to have a script which automatically uses this website and 
checks it's output everyday. It
would be better if this script runs from the clients' side.

Could any one suggest any Python modules, articles, tutorials, ect. that might 
be helpful?

Thank you.

- Yingjie







-- 
http://mail.python.org/mailman/listinfo/python-list


try... except with unknown error types

2011-08-19 Thread Yingjie Lin
Hi Python users,

I have been using try...except statements in the situations where I can expect 
a certain type of errors might occur. 
But  sometimes I don't exactly know the possible error types, or sometimes I 
just can't spell the error types correctly. 
For example, 

try:
response = urlopen(urljoin(uri1, uri2))
except urllib2.HTTPError:
print URL does not exist!

Though urllib2.HTTPError is the error type reported by Python, Python doesn't 
recognize it as an error type name. 
I tried using HTTPError alone too, but that's not recognized either.

Does anyone know what error type I should put after the except statement? or 
even better: is there a way not to specify
the error types? Thank you.


- Yingjie







-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HTML client sctript

2011-08-19 Thread Yingjie Lin
Hi John and Chris,

Thanks for the help. 

I looked at both and think that mechanize suits my needs better, 
since it simply needs a python script and doesn't depend on Firefox.

Yingjie 

 From: John Gordon gor...@panix.com
 Mechanize seems like what you want.  It's built on top of 
 urllib2.http://wwwsearch.sourceforge.net/mechanize/
 

 From: Chris Rebert c...@rebertia.com
 Selenium:http://seleniumhq.org/
 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: try... except with unknown error types

2011-08-19 Thread Yingjie Lin
Hi Zero,

I see! This is very helpful. Thank you.

- Yingjie







On Aug 19, 2011, at 3:30 PM, Zero Piraeus wrote:

 :
 
 On 19 August 2011 15:09, Yingjie Lin yingjie@mssm.edu wrote:
 
 I have been using try...except statements in the situations where I can 
 expect a certain type of errors might occur.
 But  sometimes I don't exactly know the possible error types, or sometimes I 
 just can't spell the error types correctly.
 For example,
 
 try:
response = urlopen(urljoin(uri1, uri2))
 except urllib2.HTTPError:
print URL does not exist!
 
 Though urllib2.HTTPError is the error type reported by Python, Python 
 doesn't recognize it as an error type name.
 I tried using HTTPError alone too, but that's not recognized either.
 
 Does anyone know what error type I should put after the except statement? or 
 even better: is there a way not to specify
 the error types? Thank you.
 
 You should always specify the error type, so that your error-handling
 code won't attempt to handle an error it didn't anticipate and cause
 even more problems.
 
 In this case, I think it's just that you haven't imported HTTPError
 into your namespace - if you do, it works:
 
 from urllib2 import urlopen, HTTPError
 try:
 ...   response = urlopen(http://google.com/invalid;)
 ... except HTTPError:
 ...   print URL does not exist!
 ...
 URL does not exist!
 
 
 Alternatively:
 
 import urllib2
 try:
 ...   response = urllib2.urlopen(http://google.com/invalid;)
 ... except urllib2.HTTPError:
 ...   print URL does not exist!
 ...
 URL does not exist!
 
 
 A careful look at the difference between these two ought to make it
 clear what's going on.
 
 -[]z.

-- 
http://mail.python.org/mailman/listinfo/python-list


pairwise combination of two lists

2011-08-17 Thread Yingjie Lin
Hi Python users,

I have two lists: 

li1 = ['a', 'b']
li2 = ['1', '2']

and I wish to obtain a list like this

li3 = ['a1', 'a2', 'b1', 'b2']

Is there a handy and efficient function to do this, especially when li1 and li2 
are long lists.
I found zip() but it only gives [('a', '1'), ('b', '2')],  not exactly what I 
am looking for.

Thank you.


- Yingjie







-- 
http://mail.python.org/mailman/listinfo/python-list


python cgi webpage won't redirect before background children processes finish

2011-02-22 Thread Yingjie Lin
Hi all,

I have a python cgi script which looks like this:

[CODE STARTING HERE]
open('x')
print 'Content-Type:nbsp;text/html\n\n'
..
print 'meta http-equiv=refresh content=15;url=%s' % myURL
..
### after printing the webpage
os.system('python myfile.py')
logfile.write('END OF SCRIPT')
logfile.close()
[CODE ENDING]

Question: I want this cgi webpage to automatically redirect to myURL after 15 
seconds. 
As soon as the execution of this script finishes, the corresponding job 
disappears from 
ps command output, the webpage is displayed and the log file is written. 
However, the 
webpage doesn't redirect after 15 seconds unless the background child process 
'python myfile.py' finishes by then.

Is this problem caused by initialing the children job through os.system() ? 
Does any one 
know what I need to change to make the redirection work before background 
children processes finish?

Thank you very much!

Yingjie
-- 
http://mail.python.org/mailman/listinfo/python-list