Re: Cookbook 2nd ed Credits

2005-01-05 Thread Dan Perl

"Alex Martelli" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Premshree Pillai <[EMAIL PROTECTED]> wrote:
>> Btw, is there a comprehensive list of ALL contributors put up anywhere?
>
> Not yet -- do you think I should put it up on my website?

Updating the status of the recipes on the web site would be nice.

Dan

>
> Alex 


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


Re: Cookbook 2nd ed Credits

2005-01-05 Thread Dan Perl

"Alex Martelli" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Dan Perl <[EMAIL PROTECTED]> wrote:
>
>> "Alex Martelli" <[EMAIL PROTECTED]> wrote in message
>> news:[EMAIL PROTECTED]
>> > Premshree Pillai <[EMAIL PROTECTED]> wrote:
>> >> Btw, is there a comprehensive list of ALL contributors put up 
>> >> anywhere?
>> >
>> > Not yet -- do you think I should put it up on my website?
>>
>> Updating the status of the recipes on the web site would be nice.
>
> Please take this up with Activestate guys -- I have no say on what
> happens on the website they own, nor any special status there.

I was under the impression that the status on the website reflects whether 
the recipes are approved or rejected for the book (with a few more states 
added: new, deferred, pending).  I gather now from your reply that there is 
no connection between that status and the book.

Okay then, I am adding my vote to posting the list of all the contributors. 
I still think that the ActiveState website is an appropriate place to do 
that, as they also have the recipes.  Actually, what is the exact 
relationship between the recipes on the website and the recipes published in 
the book?

If not on the ActiveState website, then posting the list to another website 
would be good too.  Even if the list is not absolutely final and may be 
changed later (a disclaimer to that effect would suffice).  I have two 
recipes submitted (for which I got permission requests) and I am curious.

BTW, I sent an email to [EMAIL PROTECTED] earlier today, before 
I even saw this thread in c.l.p.  I haven't received a reply from them yet.

Dan

>
> Alex 


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


Re: how to write a tutorial

2005-01-23 Thread Dan Perl

"Xah Lee" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> the beginning two paragraphs should be deleted. Nobody gives a shit
> except a few smug academicians where the author wrote it for pleasing
> himself. For 99% of readers, it is incomprehensible and irrelevant.
>
> the first paragraph of 9.1 "A Word About Terminology" is epitome of
> masturbation. The entire 9.1 is not necessary.
>
> Large part of 9.2 "Python Scopes and Name Spaces" is again
> masturbatory.

This is a perfect description for your own postings.  Why don't you follow 
your own advice? 


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


Re: Python 2.1 - 2.4 differences

2005-01-24 Thread Dan Perl
I'm not sure whether it's worth learning python from a book on 2.1 because 
of the changes that were made especially in 2.2 (see 
http://www.python.org/doc/2.2.1/whatsnew/, 
http://www.python.org/2.2.1/descrintro.html).

I don't know of a specific e-book on Python although there are several good 
on-line tutorials.  But if getting published books is difficult in your 
country, then I would heartily recommend Safari Books Online 
(http://www.safaribooksonline.com/).  I have been using it for a few months 
and I find it to be very useful.

Dan

"BOOGIEMAN" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>I found some e-book about Python 2.1, I want to print it but just to check
> first if sintax of Python 2.1 is same as 2.4 ? Also does anybody know 
> where
> can I download any newer Python related e-book, because there isn't any
> published yet in my country. 


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


Re: [perl-python] 20050125 standard modules

2005-01-25 Thread Dan Perl
I sent the following feedback message to Yahoo! Groups about this abusive 
use of their service.

Feel free to also report this as an abuse of the Yahoo! Groups service until 
the problem is resolved.

Dan

---
As the owner of the perl-python group, p0lyglut (aka Xah Lee), has added two 
newsgroups to the mailing list of the group: comp.lang.perl.misc and 
comp.lang.python.

First of all, I don't think anyone could have given Xah Lee an authorization 
on behalf of the newsgroups.  I am not sure, but that is probably a 
violation of the Terms of Service in itself.  I will leave this matter up to 
you to investigate.

The daily messages of the perl-python group are unwanted by the two 
newsgroups.  Just take a look at the follow-ups in the newsgroups that the 
perl-python postings have generated.

I hope that not only the newsgroups will be removed from the perl-python 
group but that such an abuse will also be prevented in the future.

Thanks,

Dan Perl 


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


Re: [perl-python] 20050125 standard modules

2005-01-25 Thread Dan Perl
I was wrong.  He is just crossposting to the newsgroups without having them 
as members of the group.

I wish there was a good way like that to stop these daily postings!

Dan

"Dan Perl" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>I sent the following feedback message to Yahoo! Groups about this abusive 
>use of their service.
>
> Feel free to also report this as an abuse of the Yahoo! Groups service 
> until the problem is resolved.
>
> Dan
>
> ---
> As the owner of the perl-python group, p0lyglut (aka Xah Lee), has added 
> two newsgroups to the mailing list of the group: comp.lang.perl.misc and 
> comp.lang.python.
>
> First of all, I don't think anyone could have given Xah Lee an 
> authorization on behalf of the newsgroups.  I am not sure, but that is 
> probably a violation of the Terms of Service in itself.  I will leave this 
> matter up to you to investigate.
>
> The daily messages of the perl-python group are unwanted by the two 
> newsgroups.  Just take a look at the follow-ups in the newsgroups that the 
> perl-python postings have generated.
>
> I hope that not only the newsgroups will be removed from the perl-python 
> group but that such an abuse will also be prevented in the future.
>
> Thanks,
>
> Dan Perl
> 


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


Re: 20050126 find replace strings in file

2005-01-26 Thread Dan Perl

<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> I guess there is no way to check if the file opened fine?  What if the
> filesystem or file is locked for this user/session.  Pretty puny
> language if it cannot tell you that it cannot do what you tell it to.
> ..
> Same for the close.  Is there no way check a good close?

An exception (IOError) is raised when a file operation fails.  The open() 
and and close() statements should be enclosed in a try-except statement.

Please, take Xah Lee's postings with more than just a grain of salt.  As a 
matter of fact, you are better off ignoring them than trying to learn from 
them.  He is an egomaniac who is learning python and who thinks that he can 
already teach others.  Personally I doubt he will ever learn python well 
enough to teach others.  Just look at his English.  He has a similar list 
with a-word-a-day just like the perl-python a-lesson-a-day and he seems 
indeed to know a lot of words.  But his grammar is horrendous (there's a 
word for you, Xah Lee!).  And according to his own website he's been living 
in North America for more than 15 years!  English is a second language for 
me too but you won't see me writing something like "this groups is for 
Perlers who wants to learn Python".

It may be unfair to pick on his English, but it is the best way I can make 
the point to someone who does not know python that Xah Lee does not know 
what he's talking about.  I find Xah Lee annoying and I could just ignore 
him, but I am concerned that some people may actually take his statements 
seriously and learn from them.  I can't imagine why or how, but there are 
actually 26 members in the perl-python Yahoo! group who have registered to 
get these bogus lessons sent to them daily!

Dan 


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


Re: exclude binary files from os.walk

2005-01-26 Thread Dan Perl

"rbt" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Is there an easy way to exclude binary files (I'm working on Windows XP) 
> from the file list returned by os.walk()?
>
> Also, when reading files and you're unsure as to whether or not they are 
> ascii or binary, I've always thought it safer to 'rb' on the read, is this 
> correct... and if so, what's the reasoning behind this? Again all of this 
> pertains to files on Windows XP and Python 2.4

Please clarify: is your question about identifying binary (non-ascii) files 
or about using os.walk? 


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


Re: Talking to the wind

2005-01-27 Thread Dan Perl

"Terry Reedy" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>
> Hey all, I have seen no evidence that XL even reads the responses that 
> have been directed thereto.  The above is like
>
> /dev/null,
> Why don't you ever answer the messages I keep sending to you?
>
> Terry J. Reedy

According to his own website, he suspects himself of having a schizoid 
personality.  This is how Britannica Online describes "schizoid":
"In this disorder there is a disinclination to mix with others, the 
individual appearing aloof, withdrawn, indifferent, unresponsive, and 
disinterested. Such a person prefers solitary to gregarious pursuits, 
involvement with things rather than with people, and often appears 
humourless or dull."

Does that explain things for you?  I really think that nothing we say gets 
through to him.

Dan 


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


Re: How to test that an exception is raised ?

2005-01-28 Thread Dan Perl

"StepH" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> But i've prob with the 1st test : test_badFile.
> When I run the test, unittest say that no error is Raised.
> But when I run the mps2xml module with a bad file as arg., the exception 
> is
> well Raised.

I assume you don't actually see the exception (you don't see the stack 
traceback printed as an output) but you just see your own message 
"FileError".  That means that the exception was raised and caught by your 
try-except statement.

> What i'm doing wrong ?
>
> It is because the exception in catched in the mps2xml module ?

With the above mentioned assumption, that must be it.  Once the exception is 
caught, it is caught and no other code invoking mps2xml.mps2xml (including 
the assertRaises in your test) will see the exception.

Your mps2xml.mps2xml function should return a value indicating success or 
failure or should re-raise the exception.  You need a mechanism to let 
invoking code know that it was successful or it failed.  With a return code, 
you should test that and not the exception.  The assert you use should work 
if you re-raise the exception.

And BTW, it is a bad idea to use a generic "except" statement like you do. 
That catches ALL the exceptions and you will never know if a different 
exception than IOError was raised.  You should catch only IOError 
specifically or any other exception that you may expect.  You can add then 
an extra "except" statement catching all the other exceptions, but only if 
it is essential for your application to handle all exceptions gracefully 
instead of just exiting with a stack traceback.

> Thanks for your helps.
>
> StepH.
>
> 


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


Re: How to test that an exception is raised ?

2005-01-28 Thread Dan Perl

"StepH" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>
> Do you say that it's not possible to test (using unittest) if an exception
> is well raised if the tested code catch it ?
> How to solve this paradoxe ?  How to automaticaly test such code ?

Then you need a side-effect in catching the exception.  A return code would 
be such a side-effect.  Setting a state variable that can be checked by the 
invoking code would be another such side-effect.

> 1./ Cheking error-return value if not a good idea for me, cause this make
> the code less clear.  I don't want a code where i've to test each function
> return value...
> 2./ If I re-raise the exception, then how to not show the traceback.  My
> user will became crazy if they view such error msg.

But then how will the user know if the call did what it is supposed to do? 
And if the user mis-used the function and used it for a file that does not 
exist, how is he/she supposed to know what the problem was and why nothing 
is coming out of calling your function?

> It seems that there is 2 school here.  I've read a lot on python, and 
> somed
> 'guru' Python say that it's maybe better to use exception even for case 
> you
> can "predict", no ?  Or i'm completly wrong ?

The IOError is an exception that you can predict and I was saying that you 
should catch it, but with a "except IOError:" statement instead of the 
"except:" statement that you used. 


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


Re: How to test that an exception is raised ?

2005-01-28 Thread Dan Perl

"StepH" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> So why the assertRaises function in unittest ?  My goal is to test if an 
> exception is well raised when a bad filename is passed to the mps2xml 
> function.

It is for functions in which the exception is raised and not caught.  You 
can use the exception mechanism to let the code invoking your function know 
that something "exceptional" has happened.  It means that you "throw" that 
exception to the invoking code which has to catch it.  Maybe the terminology 
is poorly chosen, but assertRaises does not check whether an exception is 
raised inside the function but only that an exception is raised (or 
"thrown", as described by other languages) from inside the function to 
outside.  A function does not "throw" an exception if the exception is 
caught inside the function.  Is that clearer now?

In java for instance, functions are actually declared to "throw" an 
exception if any operation inside the function may throw that exception and 
the exception is not caught inside the function.  If you implement a 
function A that invokes another function B and B throws an exception of type 
C, then you must catch the exception C in A or you have to declare A that it 
"throws C".

Hope this helps.

Dan 


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


Re: what's OOP's jargons and complexities?

2005-01-28 Thread Dan Perl

"Xah Lee" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> public class test {
> public static void main(String[] args) {
> String a = new String("a string");
> String b = new String("another one");
> StringBuffer c = new StringBuffer(40);
> c.append(a); c.append(b);
> System.out.println(c.toString());
> }
> }

Actually, it can be as simple as:
public class test {
public static void main(String[] args) {
String c = new String("a string"+" another one");
System.out.println(c);
}
}

I will not get into your "history" of the "OOP hype". 


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


Re: what's OOP's jargons and complexities?

2005-01-28 Thread Dan Perl

"Dan Perl" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Actually, it can be as simple as:
> public class test {
>public static void main(String[] args) {
>String c = new String("a string"+" another one");
>System.out.println(c);
>}
> }

I forgot.  It can be even simpler:

public class test {
public static void main(String[] args) {
System.out.println("a string"+" another one");
}
} 


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


Re: what's OOP's jargons and complexities?

2005-01-28 Thread Dan Perl

"PA" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>
> On Jan 29, 2005, at 01:09, Martin Ambuhl wrote:
>
>> Xah Lee wrote his usual masturbatory crap:
>
> Well... I have to admit that I tremendously enjoyed such "masturbatory 
> crap" (sic).
>
> Eagerly looking toward the next installment.

At first I thought you were being sarcastic.  I doubt that now.  Iindulge my 
curiosity please and tell us what you enjoy about it. 


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


Re: naive doc question

2005-01-29 Thread Dan Perl

"Michael Hartl" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> i.e., http://docs.python.org/lib/typesmapping.html
>

If you look on the index page of the Python Library Reference 
(http://docs.python.org/lib/genindex.html), you will find "dictionary 
object", which will take you exactly to the page above. 


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


Re: [perl-python] sending email

2005-01-29 Thread Dan Perl
I recommend the example in the Python Library Reference as a better example: 
http://www.python.org/doc/lib/SMTP-example.html.  You can also find the 
entire description of the smtplib module in the same section 
(http://www.python.org/doc/lib/module-smtplib.html).

Xah Lee's example is missing:
- The login() call needed by SMTP hosts that require authentication.
- The 'From:' and 'To:' addresses in the header of the email.  They
  are not mandatory, but you would probably use them. 


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


Re: Regarding exception handling

2005-01-30 Thread Dan Perl

"Aggelos I. Orfanakos" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Thanks. This should now be OK:
>
> #try:
> #try:
> #s = ... # socket opens
> #
> ## various code ...
> #except socket.error, x:
> ## exception handling
> #finally:
> #s.close() # socket closes
>

Why the nested try's?  If I understand your intention correctly this should 
do the same thing:

try:
 s = ... # socket opens
 # various code ...
except socket.error, x:
 # exception handling
s.close() # socket closes

You would need the try-finally statement only if you expect it to handle 
other exceptions than socket.error or if the socket.error is re-raised in 
the "except" clause.  Otherwise, the try-finally statement is redundant 
because no socket.error are getting out of the try-except statement.

Dan 


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


Re: Regarding exception handling

2005-01-30 Thread Dan Perl

"Bryan" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> IMO, that is not the reason for the try/finally statement and it is not 
> redundant.  the try/finally statement guarantees the resource is closed 
> and the try/finally statement only gets executed if and only if the 
> opening of the resource doesn't raise an exception.  it has nothing to do 
> with exception handling.

But IMO, try-finally is meant to be used only if the block in the try clause 
may raise exceptions.  Here is an example that shows what I meant:
s = ... # socket opens
try:
a = 1
finally:
s.close()

works perfectly the same as:
s = ... # socket opens
a = 1
s.close()

The try-finally statement does not "handle" the exception, but it makes 
sense only if exceptions are possible.  There is no point in enclosing "a = 
1" in any kind of try statement.

According to a following posting from Angelos he did expect some other 
exceptions than socket.error and hence the nested try's.  To my defence 
though, I put in a disclaimer for that case in my initial posting.

> in the previous 2 examples s = ... was placed inside the try/finally, but 
> if an exception occures and s doesn't get bound to an object, then 
> s.close() in both examples will raise a NameError on s.

That is a very good point.  I missed it.

Dan 


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


Re: Regarding exception handling

2005-01-30 Thread Dan Perl

"Aggelos I. Orfanakos" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Good point, but with your way, if "s = ... # socket opens" fails, then
> nothing will catch it. What I usually do is what I wrote above (place
> it below the 2nd try), and when attempting to close it, first use an if
> like: "if locals().has_key('s'):".

Then how about:
try:
s = ... # socket opens
try:
# various code ...
finally:
s.close() # socket closes
except socket.error, x:
# exception handling

This handles all the socket exceptions in one shot.  The only problem is 
that different problems may have to be handled differently.  For instance, 
you may need to handle a transmission that failed in the middle differently 
than a failure to open the socket.

Dan


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


Re: How do you do arrays

2005-02-01 Thread Dan Perl
A solution that I haven't seen mentioned by other postings in the thread is 
to implement the array as a dictionary:

iMatrix = {}
for index in range(majorlop1):
k = random.choice(listvalues) + 1
iMatrix[index] = k

Mind you, a dictionary does not behave *exactly* like an array.  For 
instance, in your example, you may later do a "del iMatrix[2]" and then you 
wouldn't really be able to use iMatrix like an array anymore.  But, 
depending on your application, a dictionary may be perfectly suitable.

Hope this helps.

Dan

"Thomas Bunce" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>I am new at Pyton and I am learning from book not classes
> so please forgive my being slow
>
> The below does not work I get an Error of  File
> "Matrix[index] = k
> NameError: name 'iMatrix' is not defined"
>
> while  index < majorlop1:
>   index = index + 1
>   k = random.choice(listvalues) + 1
>   iMatrix[index] = k
>
> The book statement of
> array(typecode, initializer) does not make sence
> to me how it henerates ore relaes to the org name
> for the array.
>
> Thank You
> Tom 


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


Re: [perl-python] string pattern matching

2005-02-01 Thread Dan Perl
Perhaps someone will write a program to automatically follow up on every 
[perl-python] posting?  The follow-up could just contain a statement like 
the one Daniel mentions.  Obviously the program would be written in python. 
;-)

Any suggestions on how to implement such a program?  How would it detect a 
new posting?  How would it send the follow-up?

Anyway, I agree with Daniel and I think that would not only warn newcomers 
to the group, but it would also allow many of us to move on without worrying 
about the effect that the perl-python postings may have on these newcomers.

BTW, I think Daniel's suggestion for the statement sounds pretty fair and 
pretty impartial.  Maybe it should just not use terms like "blatant".  And I 
would include something like "we support everyone's freedom to post to this 
newsgroup but we feel that python and perl beginners need to be warned about 
the quality of these tutorials".

Dan

"Daniel Fackrell" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Perhaps a message to the effect of "These messages are specifically 
> disowned
> by the groups to which they are posted, have historically been riddled 
> with
> blatant errors, and are assumed to continue in the same quality." should 
> be
> posted as a follow-up to each of these messages by XL in order to avoid
> having to spend the time to find the inaccuracies in each one 
> individually.
>
> Considering the response so far, a list of frequent posters and
> not-so-frequent posters who will vouch for the accuracy of the disclaimer
> might even be added.
>
> Daniel Fackrell
>
>
> 


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


Re: how to write a tutorial

2005-02-02 Thread Dan Perl

"Xah Lee" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> i've noticed that in Python official doc
> http://python.org/doc/lib/module-re.html
> and also How-To doc
> http://www.amk.ca/python/howto/regex/
>
> both mentions the book "Mastering Regular Expressions" by Jeffrey
> Friedl.
>
> I suggest it be dropped in both places. The mentioning of this book in
> the Perl/Python community is mostly a fawning behavior and confession
> that the author is among "in the know". This book and its mentioning is
> a cultish behavior among OpenSource morons.
>
> Most of the time, its mentioning as a reference is irrelevant. 99% uses
> of regex are the simplest uses. Spending time to read this book of
> specialization, from economy perspective, is a waste of time.
> Xah

We should drop the olympics too.  99% of  people don't practice any of those 
sports at a competitive level.  Who the hell does pole vaulting or throws a 
javelin?

Sorry, maybe I should have posted this in the "next Xah Lee post contest" 
thread.

Dan 


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


advice needed for simple python web app

2005-02-03 Thread Dan Perl
I have a pretty simple python script and I would like to turn it into a web 
application but web apps are a domain I know very little about.  I know that 
Twisted, CherryPy, Plone and Zope exist but not exactly what they do.  I 
need only advice on a direction to take.  For instance a suggestion on which 
application/framework to use would be great.

Basically, what I'm looking for is a web server that accepts an HTTP request 
and invokes a python script.  But I would like a "pythonic" solution so a 
web server like Apache is a solution that I would like to avoid.  The server 
should also be as simple as possible to administrate.

I would be even interested in building a very simple web app server if there 
is a framework that has all the basic components and there is good 
documentation for how to use it.

Thanks,

Dan 


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


Re: advice needed for simple python web app

2005-02-03 Thread Dan Perl

"Paul Rubin" <http://[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> "Dan Perl" <[EMAIL PROTECTED]> writes:
>> Basically, what I'm looking for is a web server that accepts an HTTP
>> request and invokes a python script.  But I would like a "pythonic"
>> solution so a web server like Apache is a solution that I would like
>> to avoid.  The server should also be as simple as possible to
>> administrate.
>
> CGI and CGIHTTPServer (or whatever it's called) is conceptually the
>> simplest.  What does your app do?

The application is just something I'm playing with to learn a little bit on 
web apps.  It uses an HTML form to send an email.  The form takes inputs 
like the From:, To: and Subject: fields and a text field.

I found the "Internet Protocols and Support" chapter in the Library 
Reference that covers also the CGIHTTPServer.  It's definitely something I 
will have to read.  Thanks.

Dan 


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


Re: advice needed for simple python web app

2005-02-03 Thread Dan Perl

"M.E.Farmer" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>I am no web expert but have recently used cherrypy to 'webify' a
> script. It is very easy to get going and has its own server or can be
> run behind Apache.
> The only real problem I see is that the docs are still a little lite
> for the new 2.0 series ,but they do have a newsgroup where the author
> still answers questions.
> Cherrypy2 is fairly logical and most of it is covered in the examples
> on there website.
> I can not speak for the other packages,have not used them yet ;)
> hth,
> M.E.Farmer

Thanks.  I am no web expert either so I appreciate advice coming from 
someone who was in a similar situation.

Twisted and CherryPy seemed to me to be the main choices based on what I 
understand from their front pages with my limited knowledge on web apps. 
Twisted feels more "developed" but also more complex at the same time.  I 
wanted opinions before I invest the time in studying either one of them. 
Your opinion helps.

Dan 


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


Re: advice needed for simple python web app

2005-02-03 Thread Dan Perl

"Paul Rubin" <http://[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> "Dan Perl" <[EMAIL PROTECTED]> writes:
>> The application is just something I'm playing with to learn a little bit 
>> on
>> web apps.  It uses an HTML form to send an email.  The form takes inputs
>> like the From:, To: and Subject: fields and a text field.
>
> Be careful of exposing that script to the internet.  Spammers will
> exploit it.

Do you mean publishing the script for other people to copy it or exposing 
the web app so that other people may access it?  Don't worry anyway, I won't 
do either and I'm running 2 firewalls on my PC.  It would be really naive to 
expose the web app, wouldn't it? 


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


Re: advice needed for simple python web app

2005-02-03 Thread Dan Perl

"Michele Simionato" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Dan Perl:
>> The application is just something I'm playing with to learn a little
> bit on
>> web apps.  It uses an HTML form to send an email.  The form takes
> inputs
>> like the From:, To: and Subject: fields and a text field.
>
> It is difficult to beat CGI + CGIHTTPServer for conceptual simplificity
> and easy of use: however, Quixote comes close and it has a *much*
> better support for forms.

So Quixote is another framework I will have to look into.  Best of all, 
though, after adding Quixote to the list I decided I have enough words to do 
a search.  There were a few interesting finds, but one single link that 
points to the best of them is: 
http://www.python.org/moin/WebProgrammingShootOut 


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


Re: advice needed for simple python web app

2005-02-03 Thread Dan Perl

"Brian Beck" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> From my experience, this appears to be the order from low-level to 
> high-level interfaces:
>
> 1. mod_python: As complex as you need it to be, since you can control 
> anything about the request & response process. But more up-front work and 
> decisions to make than CherryPy.
> 2. mod_python with Vampire: Directs you toward a specific publishing 
> framework that is similar to CherryPy.
> 3. CherryPy: Like Vampire but simpler and doesn't require mod_python. The 
> simplest blend between low-level server interface and ease-of-publishing.
> 4. Twisted: Seems like this is a big package to work with, not sure how 
> easy it makes publishing once you get started-- better that someone else 
> comment on this.
> 5. Zope: The most complex solution, doesn't necessarily make the 'easy 
> things easy.' But covers all fronts in terms of what a user would ever 
> need.
> 6. Zope with Plone: Adds a ton of publishing functionality. Has many ways 
> to extend, but none of them are fun. You'll have to learn such complex 
> APIs that Python will rarely help you.

This is exactly the kind of summary that I think should be in a 
WebProgrammingShootOut (see another one of my postings in this thread) but I 
failed to find such a summary.  Thanks, Brian!  Anyone can add to the list?

BTW, there are other people who seem to have been also confused by the wide 
spectrum of choices for this problem: 
http://pyre.third-bit.com/hippoblog/archives/58.html 


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


Re: advice needed for simple python web app

2005-02-04 Thread Dan Perl

"Paul Rubin"  wrote in message 
news:[EMAIL PROTECTED]
> If you're just trying to get a conceptual understanding of web
> programming, I suggest you start with cgi.
>
> Next you might want to read Philip and Alex's Guide to Web Publishing:
>
>  http://philip.greenspun.com/panda/
>
> It's out of date and the examples use Tcl and Oracle instead of
> Python, but it's still a good explanation of how database-driven web
> sites work.  The rest (like languages frameworks) is just a matter of
> picking some implementation parameters.

This matches pretty much what I've decided to do.  I'll start with cgi and 
CGIHTTPServer because I'll learn more from that and then move to a 
framework, quite likely CherryPy, although by that time I may change my 
choice.  Philip Greenspun's book looks good and I'll have to go through it. 
Thanks for the advice. 


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


Re: [perl-python] get web page programatically

2005-02-04 Thread Dan Perl

"Xah Lee" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
# note the line
# from  import 
# it reads the library and import the function name
# to see available functions in a module one can use "dir"
# import urllib; print dir(urllib)

After about a month, this tutorial has finally reached the syntax of the 
"import" statement!

And word of advice to python beginners, "print dir(urllib)" is not very 
useful in the sense mentioned here (it prints all the names defined in the 
module with no explanations, and those names are not only functions, BTW). 
But "help(urllib)" is much more useful.  Even with such a simple script, 
this tutorial still managed to give some bad advice. 


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


Re: How to read POSTed data

2005-02-05 Thread Dan Perl

"Håkan Persson" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hi.
>
> I am trying to set up a simple HTTP-server but I have problems reading 
> data that is beeing POSTed.
>
> class httpServer(BaseHTTPServer.BaseHTTPRequestHandler):
>def do_POST(self):
>input = self.rfile.read()
>
> The self.rfile.read() will hang on the
> data = self._sock.recv(recv_size)
> line in the read() function in socket.py.
>
> Is there another way to read the data?

I'm not an expert on this subject, but I am in the process of looking at the 
code in BaseHTTPServer and other related modules.  What I see used is 
normally self.rfile.readline() and that makes more sense to me (intuitively) 
than read().  Have you tried that? 


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


CGI POST problem was: How to read POSTed data

2005-02-05 Thread Dan Perl
I am piggybacking on Hakan's original posting because I am addressing the 
same group of people (those with good knowledge in the standard web 
programming modules), on a related topic.  However, my question is 
independent of Hakan's.

I have trouble getting a simple CGI script to work because it gets an empty 
cgi.FieldStorage form.  I have looked into and tried to debug the internals 
of the standard modules that are used but I cannot figure it out: how is a 
multipart POST request parsed by CGIHTTPServer?  BTW, I am using pyton 2.4.

I found the parsing of the header in the rfc822 module used by 
BaseHTTPServer.BaseHTTPRequestHandler (through the mimetools module), but 
that stops after the header (at the first empty line).  Where is the parsing 
done for the POST data following the header?  I will appreciate any pointers 
that will help me debug this problem.

As a side note, I found other old reports of problems with cgi handling POST 
requests, reports that don't seem to have had a resolution.  There is even a 
bug reported just a few days ago (1112856) that is exactly about multipart 
post requests.  If I understand the bug report correctly though, it is only 
on the latest version in CVS and it states that what is in the 2.4 release 
works.  All this tells me that it could be a "fragile" part in the standard 
library.  So it could be even a bug in the standard library, but for now I 
am assuming that I'm doing something wrong.

Thanks,

Dan 


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


Re: sos!

2005-02-05 Thread Dan Perl

"jordan2856977" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> hellow everybody! I'm from china. I'm a beginner of python. in china,
> python is not a fashionable language, so it's difficult to find some
> books about python. finally,I find a book named "python how to
> program" wrote by H.M.Deitel . who can tell me where can I find some
> interesting source code about python. otherwise, I will fell boring
> about study!

The Python Cookbook is a good source of examples that other posters haven't 
mentioned yet: http://aspn.activestate.com/ASPN/Cookbook/Python/

It is especially good because it contains simple, short recipes that 
illustrate a specific use of python and these recipes are organized in 
categories.

Dan 


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


Re: sos!

2005-02-05 Thread Dan Perl

<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>
> Dan Perl wrote:
>> [...]
> Aren't you in the wrong newsgroup? :-)

Aren't you funny? 


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


Re: CGI POST problem was: How to read POSTed data

2005-02-05 Thread Dan Perl

"M.E.Farmer" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> Dan,
> I was wondering how you were coming with your project.
> I had wondered if i had missed something going the CherryPy route
> instead of CGI. Now I see that you have had a bit of a snag , sorry to
> hear that.
> I am glad you are at least learning new things, 'cause if you had used
> CherryPy2 you would have be done by now :P
> M.E.Farmer

I chose to go first through the cgi module and later CherryPy exactly 
because I thought I will learn more this way.  And I guess I am learning 
more. 


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


Re: sos!

2005-02-05 Thread Dan Perl

"jordan2856977" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> "Dan Perl" <[EMAIL PROTECTED]> wrote in message 
> news:<[EMAIL PROTECTED]>...
>> <[EMAIL PROTECTED]> wrote in message
>> news:[EMAIL PROTECTED]
>> >
>> > Dan Perl wrote:
>> >> [...]
>> > Aren't you in the wrong newsgroup? :-)
>>
>> Aren't you funny?
> i think i have solve
> the problem thank you

Jordan, neither my posting nor the posting I followed up to were referring 
to you.  mensanator was trying to be funny about my last name and I followed 
up with a comeback. 


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


Re: CGI POST problem was: How to read POSTed data

2005-02-05 Thread Dan Perl

<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Dan Perl wrote:
>
>> how is a multipart POST request parsed by CGIHTTPServer?
>
> It isn't; the input stream containing the multipart/form-data content
> is passed to the CGI script, which can choose to parse it or not using
> any code it has to hand - which could be the 'cgi' module, but not
> necessarily.
>
>> Where is the parsing done for the POST data following the header?
>
> If you are using the 'cgi' module, then cgi.parse_multipart.

Thanks, at least I was right not to find that in the CGIHTTPServer and 
BaseHTTPServer code.  So I have to use that instead of FieldStorage?  I was 
expecting FieldStorage to encapsulate all that for all the cases, POST with 
multipart/form-data being just a special case.

I took a brief look at cgi.parse_multipart and I still have to figure out 
how to provide the fp argument.  Any code examples anywhere?  All the 
examples I have found were using only FieldStorage:
http://www.cs.virginia.edu/~lab2q/lesson_7/

http://www.devshed.com/index2.php?option=content&task=view&id=198&pop=1&page=0&hide_js=1
http://gnosis.cx/publish/programming/feature_5min_python.html
http://mail.python.org/pipermail/edu-sig/2001-June/001368.html

BTW, I also tried the example in the last link and that doesn't work either, 
with similar results/problems as my script.  I think all those examples are 
a few years old, has something changed since then?

>> As a side note, I found other old reports of problems with cgi
>> handling POST  requests, reports that don't seem to have had a
>> resolution.
>
> (in particular?)

I made the comment and now I have to back that up:
http://mail.python.org/pipermail/python-list/2002-February/thread.html#88686
http://mail.python.org/pipermail/python-list/2002-September/thread.html#124109

> FWIW, for interface-style and multipart-POST-file-upload-speed reasons
> I wrote an alternative to cgi, form.py
> (http://www.doxdesk.com/software/py/form.html). But I haven't found
> cgi's POST reading to be buggy in general.

I was quite careful in calling the code "fragile" and I am not normally the 
kind of person to mince words.  I would have said buggy if I meant that or I 
could have used even worse words.  But even the rationale behind the patch 
that caused the bug I mentioned ("Remove dependencies on (deprecated) rfc822 
and mimetools modules, replacing with email.") and even your calling this 
part of the standard library "a bit crufty in places due to age" support my 
view that this code needs work.

Besides, I am not happy at all with the idea of having to use 
cgi.parse_multipart instead of FieldStorage.  It seems a lot more low level 
than I would like to even if it offers more control.  I looked at your 
form.py and you seem to address that.  Sorry though, I will probably not use 
it.  Once I learn enough from the cgi module I will just move on to using a 
framework like CherryPy.

Dan 


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


Re: Confused with methods

2005-02-06 Thread Dan Perl

"jfj" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>I don't understand.
> We can take a function and attach it to an object, and then call it
> as an instance method as long as it has at least one argument:
>
> #
> class A:
>   pass
>
> def foo(x):
>   print x
>
> A.foo = foo
> a=A()
> a.foo()
> #
>
> However this is not possible for another instance method:
>
> 
> class A:
>  pass
>
> class B:
>  def foo(x,y)
>  print x,y
>
> b=B()
> A.foo = b.foo
> a=A()
>
> # error!!!
> a.foo()
> ##
>
> Python complains that 'foo() takes exactly 2 arguments (1 given)'.
> But by calling "b.foo(1)" we prove that it is indeed a function which 
> takes
> exactly one argument.
>
> Isn't that inconsistent?

You called b.foo(1) but a.foo().  Note one argument in the first call and no 
arguments in the second call.  Would you have called a.foo(1), you would 
have gotten the same result as with b.foo(1).  I suppose that was just a 
small omission on your part, but what are you trying to do anyway?  It's a 
very strange use of instance methods. 


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


Re: CGI POST problem was: How to read POSTed data

2005-02-06 Thread Dan Perl

<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Dan Perl wrote:
>
>> how is a multipart POST request parsed by CGIHTTPServer?
> [...]
> CGIHTTPServer, on the other hand, I have never really trusted. I would
> suspect that fella.

It turns out I was wrong thinking that the POST requests I was handling were 
multipart.  Blame that on my limited knowledge of HTTP.  At least the 
content-type header in the request doesn't say that they are multipart.  I 
checked with the an Ethereal sniffer and my Firefox browser on Windows sends 
a request with 'content-type: application/x-www-form-urlencoded' and the 
form data in the body of the request, after the headers.  I'll assume for 
now that this is a valid HTTP POST request as I believe that Firefox is 
responsible for it alone and that it is not determined by my HTML form.

Pierre Quentel's suggestion for an implementation of do_POST in the "How to 
read POSTed data" seems to handle requests of this kind, although I didn't 
personally try it.  But the run_cgi method in CGIHTTPServer expects the form 
data to be only in the POST header line, in the path of the URL, like in a 
GET request.  I cannot find form data being parsed any other way if the 
content-type is x-www-form-urlencoded.  Am I missing something?  Also, I 
don't know if this means anything, but shouldn't CGIHTTPServer use the cgi 
module if cgi has all those useful parsing utilities?

Dan 


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


Re: How to read POSTed data

2005-02-06 Thread Dan Perl

"Pierre Quentel" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Here is an example of how to get the POST data :
>
> #def do_POST(self):
> #ctype, pdict = 
> cgi.parse_header(self.headers.getheader('content-type'))
> #length = int(self.headers.getheader('content-length'))
> #if ctype == 'multipart/form-data':
> #self.body = cgi.parse_multipart(self.rfile, pdict)
> #elif ctype == 'application/x-www-form-urlencoded':
> #qs = self.rfile.read(length)
> #self.body = cgi.parse_qs(qs, keep_blank_values=1)
> #else:
> #self.body = {}   # Unknown content-type
> ## throw away additional data [see bug #427345]
> #while select.select([self.rfile._sock], [], [], 0)[0]:
> #if not self.rfile._sock.recv(1):
> #break
> #self.handle_data()
>
> where handle_data() is the method where you will process the data received
>
> The part related to bug #427345 is copied from CGIHTTPServer
>
> For an example of use you can take a look at the CustomHTTPServer in 
> Karrigell (http://karrigell.sourceforge.net)

Pierre, I am repeating some questions I already stated in another thread, 
'CGI POST problem', but do you have any opinions on how CGIHTTPServer's 
do_POST handles requests?  It looks to me like it always expects form data 
to be part of the POST command header, in the path of the URL, just like a 
GET request.  Am I understanding the code incorrectly?  It would also make 
sense to me that CGIHTTPServer should use the cgi module like you do in your 
example, but it doesn't use cgi.  Any opinions on that?  Is there a history 
there?  I don't know enough about HTTP, especially about its history, but 
was this a change in the HTTP specification at some point?

Thanks,

Dan 


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


Re: How to read POSTed data

2005-02-06 Thread Dan Perl

"Pierre Quentel" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>
>> Pierre, I am repeating some questions I already stated in another thread, 
>> 'CGI POST problem', but do you have any opinions on how CGIHTTPServer's 
>> do_POST handles requests?  It looks to me like it always expects form 
>> data to be part of the POST command header, in the path of the URL, just 
>> like a GET request.  Am I understanding the code incorrectly?
>
> The code in CGIHTTPServer is not very easy to understand, but it does
> read the request body, as many bytes as indicated in the Content-Length
> header. See line 262 (in the Python 2.4 distribution) or 250 in Python 2.3 
> (this is the Windows version) :
>
> data = self.rfile.read(nbytes)
>
> Then this data is sent to the standard input of the CGI script. If this 
> script is a Python program using the cgi module, it usually creates a 
> cgi.FieldStorage() instance : upon creation, the standard input is read 
> (in self.read_urlencoded() for instance) and the string collected is 
> processed to produce a dictionary-like object, with keys matching the form 
> field names
>
> This is compliant with the CGI specification (HTTP doesn't say anything 
> about the management of data sent by POST requests). The code I sent is an 
> alternative to CGI, leaving the management of this data (available in 
> self.body) to a method of the RequestHandler instance

Thanks, Pierre, this got me much further but I hit another stumbling block. 
I can see now that CGIHTTPServer writes all the header lines into os.environ 
and creates a subprocess for the script with os.popen2 or os.popen3 (it's 
Windows), passing the form data to the new process through sys.stdin (I 
believe it is sys.stdin although the Library Reference descriptions of 
popen2 and popen3 say that would be sys.stdout).  But I don't see any of 
those headers updated in the os.environ of the cgi script's process.  Is the 
parent's os.environ passed to the subprocesses created with popen2/popen3 on 
Windows?

cgi.FieldStorage.read_urlencoded needs the content-length that should be 
passed through os.environ.

Dan 


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


Re: Python versus Perl ?

2005-02-06 Thread Dan Perl

"Alex Martelli" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> snacktime <[EMAIL PROTECTED]> wrote:
>
>> The one drawback coming from the perl world is that you don't have as
>> many options when it comes stuff like application frameworks, and some
>
> URK -- _my_ feeling is that we have entirely *too many* options for
> stuff like web application frameworks, GUI toolkits, XML processing, ...

Alex's comment resonates so much with me right now that I propose it for 
QOTW. 


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


Re: How to read POSTed data

2005-02-06 Thread Dan Perl

"Dan Perl" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Thanks, Pierre, this got me much further but I hit another stumbling 
> block. I can see now that CGIHTTPServer writes all the header lines into 
> os.environ and creates a subprocess for the script with os.popen2 or 
> os.popen3 (it's Windows), passing the form data to the new process through 
> sys.stdin (I believe it is sys.stdin although the Library Reference 
> descriptions of popen2 and popen3 say that would be sys.stdout).  But I 
> don't see any of those headers updated in the os.environ of the cgi 
> script's process.  Is the parent's os.environ passed to the subprocesses 
> created with popen2/popen3 on Windows?
>
> cgi.FieldStorage.read_urlencoded needs the content-length that should be 
> passed through os.environ.

I was about to report a bug but then I found bug report "[ 1100235 ] Scripts 
started with CGIHTTPServer: missing cgi environment" which is actually 
caused by "[ 1110478 ] os.environ.update doesn't work".  They are fixed with 
a patch in 1110478.  I still have to try it.  I do like the alternate 
solution proposed by the originator of 1100235 though.  I also thought that 
CGIHTTPServer should use the new subprocess.Popen class instead of popen2, 
popen3 and popen4. 


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


Re: CGI POST problem was: How to read POSTed data

2005-02-06 Thread Dan Perl

<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> CGIHTTPServer, on the other hand, I have never really trusted. I would
> suspect that fella.

CGIHTTPServer wasn't the culprit after all, it was os.py.  See bug report 
"[ 1100235 ] Scripts started with CGIHTTPServer: missing cgi environment" 
which is actually caused by "[ 1110478 ] os.environ.update doesn't work". 
They are fixed with a patch in 1110478.  I applied the patch in os.py and my 
web app is working now. 


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


Re: CGI POST problem was: How to read POSTed data

2005-02-07 Thread Dan Perl

"M.E.Farmer" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Sweet!
> Glad you fixed it,  and documented it all!
> Thanks for the followups.
> Now the next poor soul to stumble in can get the right fix.
> Never know when it could be me ;)

Thanks for the comments.  I did indeed post the info on the bug to let other 
people know about it.

Anyway, this was more of a learning experience than I intended it to be. 
And now it's time for me to move on and to get into CherryPy. 


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


Re: Python on WWW - beginners question: what to choose?

2005-02-07 Thread Dan Perl

"Almad" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hello,
>
> I'm currently starting with Python as a language for writing websites. I
> must say that I'm overwhelmed by the number of choices I have (mod_python
> with publisher, Vampire, mps, Spyce), cgi, fcgi, Webware, Zope,
> Plone, ...).
>
> Isn't there any article in style "this suite use this style of programming
> and is good for this type of applications"?

Have you looked at these links?
http://www.python.org/pycon/papers/framework/web.html
http://pyre.third-bit.com/pyweb/index.html
You may find also this interesting as a list to several links related to 
this topic:
http://www.python.org/moin/WebProgramming

Dan 


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


Re: python code with indention

2005-02-07 Thread Dan Perl

"Xah Lee" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> is it possible to write python code without any indentation?

I read just today in a tutorial that "Python on the other hand, does not 
even allow changes in code's indentation".  Maybe the author of that 
tutorial can help? 


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


Re: newbie question

2005-02-09 Thread Dan Perl

<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hello All,
> What is the python equivalent of the following statement?
>
> while (n--)

Like other posters said, you should give more details with your question. 
What do you mean by equivalent?  The following is *functionally* equivalent:
for n in range(start, 0, -1):

Of course, there is always:
while n:
...
n -= 1

But unfortunately, no, I don't think there is an equivalent in python that 
has the conciseness of the C statement.  The pre- and post-increment 
and -decrement in C/C++/Java are very powerful and I miss them in python. 


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


Re: newbie question

2005-02-09 Thread Dan Perl

"Cameron Laird" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> In article <[EMAIL PROTECTED]>,
> Dan Perl <[EMAIL PROTECTED]> wrote:
> .
> .
> .
>>has the conciseness of the C statement.  The pre- and post-increment
>>and -decrement in C/C++/Java are very powerful and I miss them in python.
>
> Me, too.
>
> Which is, I suspect, evidence for the incompleteness of our Pythonhood.
> As Peter Hansen already hinted in this thread, an appetite for the
> increment and related operators probably is a symptom that there's an
> opportunity nearby to use an iterator or string method or such.  C++
> and Java wish they had it so good.

I can't say that is not part of the reason, but the example in the OP is a 
clear illustration of cases where something like an increment/decrement 
operator would be very useful.  OTOH, I was thinking of saying in my 
previous posting that I prefer
for n in range(start, 0, -1):
to
n = start
while (n--)
I think that the first form is more readable, although that may be just me. 
I would actually even prefer the 'for' statement in C to the 'while' 
statement:
for (n=start; n<=0; n--) 


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


Re: newbie question

2005-02-10 Thread Dan Perl

"Peter Hansen" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Dan Perl wrote:
>> OTOH, I was thinking of saying in my previous posting that I prefer
>> for n in range(start, 0, -1):
>> to
>> n = start
>> while (n--)
>> I think that the first form is more readable, although that may be just 
>> me. I would actually even prefer the 'for' statement in C to the 'while' 
>> statement:
>> for (n=start; n<=0; n--)
>
> I'm not sure if it's just picking nits, but I'd like to
> point out that neither of your alternatives is actually
> equivalent to the while (n--) form...  nor was Jeff
> Shannon's attempt (in that case it leaves the loop with
> n equal to 0, not -1).

You're right in your nit picking.  I have to go back to using some C/C++ and 
Java also, I don't want to forget them completely. 


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


Re: more os.walk() issues... probably user error

2005-02-16 Thread Dan Perl

"rbt" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> rbt wrote:
>> This function is intended to remove unwanted files and dirs from 
>> os.walk(). It will return correctly *IF* I leave the 'for fs in 
>> fs_objects' statement out (basically leave out the entire purpose of the 
>> function).
>>
>> It's odd, when the program goes into that statment... even when only a 
>> 'pass', and nothing else is present, nothing is returned. Why is that? 
>> I'm testing Python 2.4 on Linux x86 and WinXP. Results are the same on 
>> either platform.
>>
>> def build_clean_list(self, path):
>>
>> file_skip_list = ['search_results.txt']
>> dir_skip_list = ['dev', 'proc', 'Temporary Internet Files']
>>
>> fs_objects = os.walk(path, topdown=True)
>> ##  for fs in fs_objects:
>> ##
>> ##for f in fs[2]:
>> ##if f in file_skip_list:
>> ##print f
>> ##fs[2].remove(f)
>> ##
>> ##for d in fs[1]:
>> ##if d in dir_skip_list:
>> ##print d
>> ##fs[1].remove(d)
>>
>> return fs_objects
>>
>>
>
> Just to clarify, it's wrong of me to say that 'nothing is returned'... in 
> either case, this is what is returned:
>
> Here's what was returned and its type:
> 
> 
> 
> 
>
> But, I can't iterate over the returned object when I descend into the for 
> statement I mentioned above.

What do you mean by not being able to iterate over the returned object? 
What kind of error are you getting?  Have you tried to debug the code?

BTW, os.walk indeed returns a generator.  You should familiarize yourself 
with generators and iterators if you haven't done so yet. 


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


Re: more os.walk() issues... probably user error

2005-02-16 Thread Dan Perl

"rbt" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> def build_clean_list(self, path):
>
> file_skip_list = ['search_results.txt']
> dir_skip_list = ['dev', 'proc', 'Temporary Internet Files']
>
> fs_objects = os.walk(path, topdown=True)
> ##  for fs in fs_objects:
> ##
> ##for f in fs[2]:
> ##if f in file_skip_list:
> ##print f
> ##fs[2].remove(f)
> ##
> ##for d in fs[1]:
> ##if d in dir_skip_list:
> ##print d
> ##fs[1].remove(d)
>
> return fs_objects

Rather as an aside, the idiom for using os.walk is
for dirpath, dirnames, dirfiles in os.walk(path):
for f in dirnames:
if f in file_skip_list:
print f
filenames.remove(f)
if d in dir_skip_list:
print d
dirnames.remove(f)

More crucially for your code, returning the generator object after having 
iterated all the way through it will not do you any good.  The generator has 
an internal state that puts it at "the end of the iteration" so you cannot 
use it to iterate again. 


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


Re: os.walk()

2005-02-17 Thread Dan Perl

"rbt" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Could someone demonstrate the correct/proper way to use os.walk() to skip 
> certain files and folders while walking a specified path? I've read the 
> module docs and googled to no avail and posted here about other os.walk 
> issues, but I think I need to back up to the basics or find another tool 
> as this isn't going anywhere fast... I've tried this:
>
> for root, dirs, files in os.walk(path, topdown=True):
>
> file_skip_list = ['file1', 'file2']
> dir_skip_list = ['dir1', 'dir2']
>
> for f in files:
> if f in file_skip_list
> files.remove(f)
>
> for d in dirs:
> if d in dir_skip_list:
> dirs.remove(d)

I think the problem here is that you are removing elements from a list while 
traversing it.  Try to use a copy for the traversal, like this:
 for f in files[:]:
 if f in file_skip_list
 files.remove(f)

 for d in dirs[:]:
 if d in dir_skip_list:
 dirs.remove(d)

> And This:
>
> files = [f for f in files if f not in file_skip_list]
> dirs = [d for d in dirs if dir not in dir_skip_list]

This is not doing what you want because it just creates new lists and it 
doesn't modify the existing lists that the os.walk generator is using. 


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


inheritance problem with 2 cooperative methods

2004-12-02 Thread Dan Perl
Here is a problem I am having trouble with and I hope someone in this group 
will suggest a solution.  First, some code that works.  3 classes that are 
derived from each other (A->B->C), each one implementing only 2 methods, 
__init__ and setConfig.
---
#!/usr/bin/python
class A (object):
   def __init__(self):
  super(A, self).__init__()
  self.x = 0
   def setConfig(self, config):
  self.x += config['x']

class B (A):
   def __init__(self):
  super(B, self).__init__()
  self.y = 0
   def setConfig(self, config):
  super(B, self).setConfig(config)
  self.y += config['y']

class C (B):
   def __init__(self):
  super(C, self).__init__()
  self.z = 0
   def setConfig(self, config):
  super(C, self).setConfig(config)
  self.z += config['z']

config = {'x':1, 'y':2, 'z':3}
alpha = A()
alpha.setConfig(config)
print alpha.x
beta = B()
beta.setConfig(config)
print beta.x, beta.y
beta.setConfig(config)
print beta.x, beta.y
gamma = C()
gamma.setConfig(config)
print gamma.x, gamma.y, gamma.z
gamma.setConfig(config)
print gamma.x, gamma.y, gamma.z
--

The output from that code is:
1
1 2
2 4
1 2 3
2 4 6

So far, so good!  But let's assume that I want to change the __init__ 
methods so that they take a configuration as an argument so the objects are 
created and configured in one step, like this:
alpha = A(config)

How can the code be changed to implement this?  It is tempting to modify the 
__init__ methods like this:
class A (object):
   def __init__(self, config):
  super(A, self).__init__(config)
  self.x = 0
  A.setConfig(self, config)

However, if implemented this way, the output is:
1
2 2
3 4
3 4 3
4 6 6

This shows that setConfig gets called more than once because both __init__ 
and setConfig are cooperative methods.

I have been thinking about this for a day now and I cannot find a good 
solution.  I imagine this would be a common problem and that there would be 
a recipe somewhere, but I couldn't find one (I looked in the Python 
Cookbook).  I've thought of creating 2 separate methods instead of 
setConfig, one that does the "local" configuration and another one that 
takes care of invoking super.  But I don't like the idea of creating 2 
methods in each class and I haven't been able to think of a way to solve the 
problem with just one change in the base class that would be inherited by 
all the subclasses.

Anyone has any ideas?  Thanks.

Dan 


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


Re: inheritance problem with 2 cooperative methods

2004-12-02 Thread Dan Perl
Thank you very much, Greg, that does the job!  Somehow I couldn't see it and 
I needed someone to point out to me.

Dan

"Greg Ewing" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Dan Perl wrote:
>> So far, so good!  But let's assume that I want to change the __init__ 
>> methods so that they take a configuration as an argument so the objects 
>> are created and configured in one step, like this:
>> alpha = A(config)
>
> One way would be to make the setConfig call only
> in the root class, and perform the initialisation
> that it depends on *before* making the super call
> in each __init__ method, i.e.
>
> class A (object):
>def __init__(self, config):
>   self.x = 0
>   self.setConfig(config)
>
> class B (A):
>def __init__(self, config):
>   self.y = 0
>   super(B, self).__init__(config)
>
> class C (B):
>def __init__(self, config):
>   self.z = 0
>   super(C, self).__init__(config)
>
> This works here because each of the initialisation
> operations is self-contained. It might not work so well
> in real life if some of the base class state needs to be
> initialised before the subclass initialisation can be
> performed. However, it's worth considering -- I came
> across the same sort of problem several times in
> PyGUI, and I usually managed to solve it by carefully
> arranging initialisations before and after the super
> call.
>
> If you can't use that solution, I would suggest you
> keep the __init__ and setConfig operations separate,
> and live with having to call setConfig after creating
> an object. Factory functions could be provided if
> you were doing this a lot.
>
> -- 
> Greg Ewing, Computer Science Dept,
> University of Canterbury, Christchurch, New Zealand
> http://www.cosc.canterbury.ac.nz/~greg
> 


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


Re: inheritance problem with 2 cooperative methods

2004-12-03 Thread Dan Perl
This is almost the same code as Greg's with the only difference being that 
test for configuration having been done.  But the test is unnecessary.  I 
don't see how setConfig could be invoked in the super of the base class (A), 
so such a test would be relevant only in subclasses, if they DO invoke 
setConfig.  But it's better if they just don't invoke it, which makes for a 
much cleaner solution.  Thanks anyway.

BTW, you named the attribute configinitialized in one place and configSet in 
the other place.  Which proves that the test is redundant, because it does 
work anyway as is.

I had a similar solution, where I was invoking setConfig only if the class 
of self is the same as the class where __init__ is defined, something like 
this:
class A (object):
def __init__(self, config):
   self.x = 0
   super(A, self).__init__()
   if A == self.__class__:
 self.setConfig(config)

I didn't like it though because it has to be done like this in every 
subclass's __init__.  And it's a problem that I didn't realize at the time 
if a subclass just does not need to override __init__ (then the 
configuration is just not set).

Dan

"David Fraser" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>
> What about using an attribute in the base class to remember whether 
> initial config has been done? This seems to work:
>
> #!/usr/bin/python
> class A (object):
>def __init__(self, config):
>   self.x = 0
>   self.configinitialized = False
>   super(A, self).__init__()
>   if not self.configinitialized:
> self.setConfig(config)
>def setConfig(self, config):
>   self.x += config['x']
>   self.configset = True
>
> class B (A):
>def __init__(self, config):
>   self.y = 0
>   super(B, self).__init__(config)
>def setConfig(self, config):
>   super(B, self).setConfig(config)
>   self.y += config['y']
>
> class C (B):
>def __init__(self, config):
>   self.z = 0
>   super(C, self).__init__(config)
>def setConfig(self, config):
>   super(C, self).setConfig(config)
>   self.z += config['z']
>
> config = {'x':1, 'y':2, 'z':3}
> alpha = A(config)
> print alpha.x
> beta = B(config)
> print beta.x, beta.y
> beta.setConfig(config)
> print beta.x, beta.y
> gamma = C(config)
> print gamma.x, gamma.y, gamma.z
> gamma.setConfig(config)
> print gamma.x, gamma.y, gamma.z 


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


Re: Need help on program!!!

2004-12-03 Thread Dan Perl
That was also my impression.  Even the description of the problem looks like 
it's just copied from the assignment, so probably didn't even take the time 
to restate the problem in his own words.

But we're speculating.  Either way, this is not a normal request: "I need a 
program to do this, plz help!"  To the OP: you can't seriously expect us to 
actually write the program for you, what kind of help are you looking for?

Dan

"Miklós P" <[EMAIL PROTECTED]> 
wrote in message news:[EMAIL PROTECTED]
>
> Seems very much like homework to me... ;)  And that's something you are
> supposed to do on your own..
>
> M. 


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


Re: Need help on program!!!

2004-12-03 Thread Dan Perl

"Max M" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hopefully his teacher doesn't know about Google, or he can be expelled 
> from school for using it.

"Hopefully" for whom?  For us, who may have to work with him someday or use 
a product that he developed?  Most of us here have been students (or still 
are) and may sympathize with the OP, but personally I have been also a TA so 
I have seen the other side and that's where my sympathy lies now.

Dan 


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


Re: Need help on program!!!

2004-12-03 Thread Dan Perl

"Miklós P" <[EMAIL PROTECTED]> 
wrote in message news:[EMAIL PROTECTED]
>
> "Dan Perl" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>>
>> "Hopefully" for whom?  For us, who may have to work with him someday or
> use
>> a product that he developed?  Most of us here have been students (or 
>> still
>> are) and may sympathize with the OP, but personally I have been also a TA
> so
>> I have seen the other side and that's where my sympathy lies now.
>
> To me, it's my benignity towards the OP rather than my sympathy to the
> TAs...
> That's why I find the greedy guy's offer immoral.

I'm not sure what you mean by "benignity" here, but I think I agree with 
you.  Sympathy for TAs is not really my reason for how I feel towards 
someone cheating on their assignment.  I was just looking for something to 
oppose sympathy for a student (assuming that sympathy was the only possible 
reason to give him the solution).  I guess that is not the best argument I 
could have made.

Dan

> Best regards,
>  Miklós
>
> 


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


Re: dictionary initialization

2004-12-09 Thread Dan Perl
<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hi Dan
>
> I must confess that upon rereading my words, there is some irony there
> (but not really sarcasm, is there?).  However, I *really* tried to keep
> my tone, well, professional.  I realise I didn't do a good job and
> apologise.  I hope that's ok.
>
> Keep well
> Caleb
>

It's water under the bridge.  I admire though the fact that you sent this 
message.

Dan 


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


Re: Persistent objects

2004-12-12 Thread Dan Perl

"Paul Rubin"  wrote in message 
news:[EMAIL PROTECTED]
> I've had this recurring half-baked desire for long enough that I
> thought I'd post about it, even though I don't have any concrete
> proposals and the whole idea is fraught with hazards.
>
> Basically I wish there was a way to have persistent in-memory objects
> in a Python app, maybe a multi-process one.  So you could have a
> persistent dictionary d, and if you say
>   d[x] = Frob(foo=9, bar=23)
> that creates a Frob instance and stores it in d[x].  Then if you
> exit the app and restart it later, there'd be a way to bring d back
> into the process and have that Frob instance be there.

I haven't used it myself, but this sounds to me a lot like metakit 
(http://www.equi4.com/metakit.html).  Have you considered that and does it 
fit your needs?

Dan 


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


Re: from string to raw string

2004-12-15 Thread Dan Perl
This is not what I meant.  My posting was a judgement error.  You are right 
though that my intuition was leading me to something like this.  However, I 
didn't realize that it was not necessary for what I was doing.  But this is 
very educational too.  It made me look up string decode, encode, and codecs 
in the library reference and I found out more about them.  I'm sure it's 
going to be useful.  Thanks!

Dan

"Scott David Daniels" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Dan Perl wrote:
>> Is there a way to convert a regular string to a raw string so that one 
>> could get from '\bblah' to r'\bblah' other than parsing the string and 
>> modifying the escapes?
>
> Assuming you might mean something else, that something else might be:
>
>s = r'no_tab_\t_here'
>len(s.split()) == 1
>len(s.decode('string_escape').split()) == 2
>s = 'tab_\t_here'
>len(s.split()) == 2
>len(s.encode('string_escape').split()) == 1
>
> -Scott David Daniels
> [EMAIL PROTECTED] 


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


Re: Python IDE

2004-12-16 Thread Dan Perl
Interesting.  I've never met anyone like that.  If they are in high tech, 
they must be hardware people, otherwise how would they make a living?  And 
I'm not sure "socialists" is the correct term in this case, it sounds to me 
more like "communists".  Or "liberals" for our friends in the US.

;-)  (for the sarcasm challenged)

Dan

"fuzzylollipop" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> no it was a sideways remark at all the software socialists that thing
> EVERYTHING should be free, never said anything about Eclipse, just the
> people that insist ALL software should be free.
> 


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


Re: Python IDE

2004-12-16 Thread Dan Perl

"Mike Meyer" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> "Dan Perl" <[EMAIL PROTECTED]> writes:
>
>> "Mike Meyer" <[EMAIL PROTECTED]> wrote in message
>> news:[EMAIL PROTECTED]
>>> A: What's the most obnoxious thing on Usenet?
>>> Q: topposting.
>>
>> What is "Jeopardy", Alex?  You got your Q&A mixed up.
>
> No, the Q&A in that order demonstrate what's wrong with top posting.
>
> -- 
> Mike Meyer <[EMAIL PROTECTED]> http://www.mired.org/home/mwm/
> Independent WWW/Perforce/FreeBSD/Unix consultant, email for more 
> information.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

Yeah, you're right. 


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


Re: [OT] Python IDE

2004-12-17 Thread Dan Perl

"Fuzzyman" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>
> Dan Perl wrote:
>> "Mike Meyer" <[EMAIL PROTECTED]> wrote in message
>> news:[EMAIL PROTECTED]
>> > "Dan Perl" <[EMAIL PROTECTED]> writes:
>> >
>> >> "Mike Meyer" <[EMAIL PROTECTED]> wrote in message
>> >> news:[EMAIL PROTECTED]
>> >>> A: What's the most obnoxious thing on Usenet?
>> >>> Q: topposting.
>> >>
>> >> What is "Jeopardy", Alex?  You got your Q&A mixed up.
>> >
>> > No, the Q&A in that order demonstrate what's wrong with top
> posting.
>> >
>> >> > --
>> > Mike Meyer <[EMAIL PROTECTED]> http://www.mired.org/home/mwm/
>> > Independent WWW/Perforce/FreeBSD/Unix consultant, email for more
>> > information.
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>>
>> Yeah, you're right.
> .
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> If it's a short reply to a long post - in context - top posting is
> often *significantly * less annoying..
>
> Isn't there a quote that includes 'hobgobline of little minds'..
>
> Regards,
> Fuzzy
> http://www.voidspace.org.uk/atlantibots/pythonutils.html
>

That was my point.  I was being sarcastic again but I didn't put a warning 
anymore.  As I'm doing now, still posting at the bottom of a long posting.

I'm not sure whether you were attacking me too (am I the "little mind"?) or 
supporting me, Fuzzy.  Anyway, I think that Mike Meyer was more annoyed by 
my jab at US politics than by my top posting.  But it was safer for him to 
pick on the latter.

Dan 


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


Re: from string to raw string

2004-12-14 Thread Dan Perl
Yeah, you're right.  I got it all twisted in my mind.  It's late and I must 
be getting tired.

Thanks.

Dan

"Brian Beck" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Dan Perl wrote:
>> Is there a way to convert a regular string to a raw string so that one 
>> could get from '\bblah' to r'\bblah' other than parsing the string and 
>> modifying the escapes?
>
> There's no such thing as a raw string, only raw string literals. In other 
> words, it's a syntax to tell the Python interpreter which characters in 
> your string are 'special' and has no effect on strings not input as 
> literals directly within your code.  Strings from files or any input 
> besides the interactive Python shell will already be what you're looking 
> for.
>
> -- 
> Brian Beck
> Adventurer of the First Order 


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


from string to raw string

2004-12-14 Thread Dan Perl
Is there a way to convert a regular string to a raw string so that one could 
get from '\bblah' to r'\bblah' other than parsing the string and modifying 
the escapes?

I am interested in this for the use of regular expressions.  I would like to 
be able to accept re patterns as inputs either from a file or from a GUI 
tool and I would like to allow those inputs as regular strings and not force 
users to write patterns with double escapes.

I naively thought there could be utilities for such a conversion either in 
the string module or the StringIO module but I don't see any utilities like 
that.

So, any suggestions?

Dan 


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


Re: Python IDE

2004-12-16 Thread Dan Perl

"Mike Meyer" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> A: What's the most obnoxious thing on Usenet?
> Q: topposting.

What is "Jeopardy", Alex?  You got your Q&A mixed up.

So what, just because I wrote at the top of the posting you couldn't see my 
warning sign for the sarcasm impaired?

Dan 


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