Re: Safe file I/O to shared file (or SQLite) from multi-threaded web server

2010-01-02 Thread John Nagle

pyt...@bdurham.com wrote:

I'm looking for the best practice way for a multi-threaded python web
server application to read/write to a shared file or a SQLite database.

What do I need to do (if anything) to make sure my writes to a regular
file on disk or to a SQLite database are atomic in nature when multiple
clients post data to my application simultaneously?


   SQLite can do that correctly, but SQLite isn't intended for use
as a database engine for a busy database being used by many
concurrent operations.  Especially if those operations involve
updates.  Any update in SQLite locks all tables involved for the duration
of the operation.  When SQLite hits a lock, it returns an error code, and
the caller should retry after a delay.  If this occurs frequently in
your application, you've hit the limits of SQLite.  Then it's
time to move up to MySQL.

  If you have enough traffic that you need a multi-threaded web server,
it's probably time to move up.

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


Re: IOError - cannot create file (linux daemon-invoked script)

2010-01-02 Thread Cameron Simpson
On 02Jan2010 15:21, cassiope  wrote:
| [...]  I want
| to save a copy of the email in a particular directory which is
| accessible to the Windows clients via samba.
| 
| The strange thing is that even with the right user-id, I cannot seem
| to write to the directory, getting an IOError exception.  Changing the
| directory to world-writable fixes this.  I can confirm the uid and gid
| for the script by having the script print these values just before
| trying to create/write the file.  Becoming the same lesser user, I
| have no problem writing a file to the same directory.

Can you show us:
  - the directory user and group ownership and permissions
  - the daemon's user and group values?

You can also strace your daemon:

  strace -f -e trace=file your-daemon your-daemon-args... 2>strace.out

and then examine the log for the precise UNIX-level failure.

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

Money won't buy happiness, but it will pay the salary of a large research
staff to study the problem. - Bill Vaughan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Any Swisses here?

2010-01-02 Thread J Sisson
On Sat, Jan 2, 2010 at 9:38 PM, n00m  wrote:

> What you achieved in the life?


English mastery, for starters...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Any Swisses here?

2010-01-02 Thread n00m
On Jan 3, 5:30 am, Steve Holden  wrote:
> Zhu Sha Zang wrote:
> >> [stuff and nonsense from a third party]
> > WTF?
>
> We do get the occasional bigot dropping in from time to time. Best to
> ignore them 'til they go away.
>
> regards
>  Steve
> --
> Steve Holden           +1 571 484 6266   +1 800 494 3119
> PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
> Holden Web LLC                http://www.holdenweb.com/
> UPCOMING EVENTS:        http://holdenweb.eventbrite.com/

And who are YOU?
What you achieved in the life?
For me you is nothing I recall only Kipling, Dickens, Newton
its your background. My backgroung is the best rockets
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Any Swisses here?

2010-01-02 Thread Steve Holden
Zhu Sha Zang wrote:
>> [stuff and nonsense from a third party] 
> WTF?

We do get the occasional bigot dropping in from time to time. Best to
ignore them 'til they go away.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: Any Swisses here?

2010-01-02 Thread Zhu Sha Zang
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Em 03-01-2010 01:17, n00m escreveu:
> Congrats!
> Your choice -- to ban building of muslim mosques -- is the only choice
> to save our civililazation.
> This yellow plague (incl. Chinese etc) must be eliminated from our
> Planet,
> They are very cunning critters, they can play on strings of
> compassion,
> but its riffraffs. What do you know about Russia? Who are yuo against
> Russia?
> Nothing. Dust. We are simply tired people.

WTF?
-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAktADoIACgkQ35zeJy7JhCgkOACfZG9y57J6WpmPRcxBLT9lHi4D
AjYAn059bwVoN+D2/WZHwsGPVVP/I3wF
=msDn
-END PGP SIGNATURE-

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


Any Swisses here?

2010-01-02 Thread n00m
Congrats!
Your choice -- to ban building of muslim mosques -- is the only choice
to save our civililazation.
This yellow plague (incl. Chinese etc) must be eliminated from our
Planet,
They are very cunning critters, they can play on strings of
compassion,
but its riffraffs. What do you know about Russia? Who are yuo against
Russia?
Nothing. Dust. We are simply tired people.
-- 
http://mail.python.org/mailman/listinfo/python-list


CMNDBOT 0.1 released

2010-01-02 Thread Bart Thate
new in this release:

* updated the repository to GZRBOT code
* a outputcache and poller gadget is now available to support writing
to waves (right now the poller polls every minute)
* RSS plugin looks stable

todo:

* make gozernet work .. this lets GZRBOT bots communicate with each
other by using json over xmpp
* use this to implement wave <-> IRC relaying
* port monitoring of bot output
* port karma plugin
* port quote plugin

demo:

http://cmndbot.appspot.com

wave/xmpp:

cmnd...@appspot.com

about CMNDBOT:

CMNDBOT is a port of GOZERBOT to the Google Application Engine. It
supports wave, web and xmpp. It supports a plugin structure that lets
you add commands or register callbacks for events. License is BSD
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IOError - cannot create file (linux daemon-invoked script)

2010-01-02 Thread Christian Heimes
cassiope wrote:
> The strange thing is that even with the right user-id, I cannot seem
> to write to the directory, getting an IOError exception.  Changing the
> directory to world-writable fixes this.  I can confirm the uid and gid
> for the script by having the script print these values just before
> trying to create/write the file.  Becoming the same lesser user, I
> have no problem writing a file to the same directory.

Are you able to write to the directory with the user id when you tried
to create a file manually?
How are you changing the uid and gid of your script? IIRC you have to
set the effective user id with os.seteuid() and os.setegid().

Christian

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


Re: Bare Excepts

2010-01-02 Thread Dave Angel

Steven D'Aprano wrote:

On Sat, 02 Jan 2010 09:40:44 -0800, Aahz wrote:

  

OTOH, if you want to do something different depending on whether the
file exists, you need to use both approaches:

if os.path.exists(fname):
try:
f = open(fname, 'rb')
data = f.read()
f.close()
return data
except IOError:
logger.error("Can't read: %s", fname) return ''
else:
try:
f = open(fname, 'wb')
f.write(data)
f.close()
except IOError:
logger.error("Can't write: %s", fname)
return None



Unfortunately, this is still vulnerable to the same sort of race 
condition I spoke about.


Even more unfortunately, I don't know that there is any fool-proof way of 
avoiding such race conditions in general. Particularly the problem of 
"open this file for writing only if it doesn't already exist".
  

In Windows, there is  a way to do it.  It's just not exposed to the 
Python built-in function open().  You use the CreateFile() function, 
with /dwCreationDisposition/  of CREATE_NEW.


It's atomic, and fails politely if the file already exists.

No idea if Unix has a similar functionality.

DaveA


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


Re: Python Concurrency Workshop, Jan 14-15, 2010

2010-01-02 Thread Robert Kern

On 2010-01-02 17:17 , Brian Blais wrote:

On Jan 2, 2010, at 13:21 , Mike Howard wrote:


Hi Dave,

Chicago in January?

How about moving it to Denver - it's a nice town and I live close by.

Mike

David Beazley wrote:

Python Concurrency Workshop, v2.0


I think, in the spirit of the topic, they should hold it at both places
at the same time.


 Well played. +1 QOTW

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Dynamic text color

2010-01-02 Thread John Posner

On Sat, Jan 2, 2010 at 1:47 PM, Dave McCormick wrote:


WooHoo!!!
I got it!!! Yup, I am sure it can be optimized but it works!!!


Hmmm ... it doesn't work for me ...



 RED

   for word in redList:
   new_Rword(complete, word)  def new_Rword(complete, word):
   Tbox.tag_remove(word, "1.0", END)
   for matchobj in re.finditer(word, complete):
   start,end =  matchobj.span()   Tbox.tag_add("red", 
"1.0 + %d chars" % start,"1.0 + %d chars" % end)

   Tbox.tag_config("red", foreground="red")


How *could* this work, Dave, since you call function new_Rword() before 
you define it? I rearranged the statements in your program, and it now 
works for me:


#-
from Tkinter import *
import re

redList = "red dog".split()
blueList = "blue ball".split()
greenList = "green grass".split()

def get_position(event):
complete = Tbox.get("1.0", END)
for word in redList:
new_Rword(complete, word)
for word in blueList:
new_Bword(complete, word)
for word in greenList:
new_Gword(complete, word)

def new_Rword(complete, word):
Tbox.tag_remove(word, "1.0", END)
for matchobj in re.finditer(word, complete):
start,end =  matchobj.span()
Tbox.tag_add("red", "1.0 + %d chars" % start,"1.0 + %d chars" % 
end)


def new_Bword(complete, word):
Tbox.tag_remove(word, "1.0", END)
for matchobj in re.finditer(word, complete):
start,end =  matchobj.span()
Tbox.tag_add("blue", "1.0 + %d chars" % start,"1.0 + %d chars" 
% end)


def new_Gword(complete, word):
Tbox.tag_remove(word, "1.0", END)
for matchobj in re.finditer(word, complete):
start,end =  matchobj.span()
Tbox.tag_add("green", "1.0 + %d chars" % start,"1.0 + %d chars" 
% end)


root = Tk()
Tbox = Text(root, width=40, height=15, wrap=CHAR,
   font="Times 14 bold", bg="#dd")
Tbox.tag_config("red", foreground="red")
Tbox.tag_config("blue", foreground="blue")
Tbox.tag_config("green", foreground="green")

Tbox.pack()
Tbox.bind("", get_position)
Tbox.focus()
root.mainloop()
#-

This version also has the advantage of defining each function just once, 
instead of multiple times on each keystroke! Still to-do:


* rename get_position() to get_complete_text()

* replace new_Rword(), new_Bword(), and new_Gword() with a single 
function that has an extra parameter, "color".


Keep at it!

-John

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


Re: Significant whitespace

2010-01-02 Thread Mensanator
On Jan 2, 4:19 pm, Emile van Sebille  wrote:
> On 1/1/2010 5:05 PM Steven D'Aprano said...
>
> > In Python terms, imagine if we could write
>
> >      foriinrange(10):
>
> > instead of the usual
>
> >      for i in range(10):
>
> > Since the colon makes it unambiguous that it is some sort of block
> > construct, and it starts with "for", it must be a for loop. Pretty
> > horrible, yes?
>
> Yes -- early on I got stung by something similar.  My first programming
> job (1977?) was working in a basic dialect called Buisness Basic Level
> II (BBII).  To delete a record in the file you used a remove statement.
>   White space on the line was optional, allowing you to write lines like
> '100 remove(1,key=k$)' and '100 fori=1to10'.  REM was how a remark
> statement began, which allowed statements like "100 REM comment".  I was
> sent on-site to a customer running the prior version (BBI).  I didn't
> realize the REMOVE command changed between versions from REKEY, and it
> was only after a more experienced programmer co-wroker pointed out that
> my REMOVE statement was being interpreted as a REM that I finally found
> the bug I'd created.

Better than the other way 'round, eh?

>
> Manuals and documentation, while often lacking, are as often underrated.
>
> Emile

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


Re: Dynamic text color

2010-01-02 Thread John Posner

On Sat, Jan 2, 2010 at 1:47 PM, Dave McCormick wrote:


WooHoo!!!
I got it!!! Yup, I am sure it can be optimized but it works!!!


Dave, please ignore a couple of my bogus complaints in the previous 
message:


   ... you call function new_Rword() before you define it

   ... this version also has the advantage of defining each function 
just once, instead of multiple times on each keystroke



But I stand by my overall statement that the program didn't work for me, 
and that rearranging the lines produces a working program.


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


Re: Bare Excepts

2010-01-02 Thread Steven D'Aprano
On Sun, 03 Jan 2010 01:10:51 +1100, Lie Ryan wrote:

> On 1/2/2010 9:42 PM, Steven D'Aprano wrote:
>> On Fri, 01 Jan 2010 15:27:57 -0800, myle wrote:
>>
>>> Why we should prefer ``if: ...'' over  a ``try: ... except something:
>>> pass'' block?
>>
>> We shouldn't, not in general.
> 
> One exception (pun intended) is if the try-block have a side effect that
> is difficult to cleanup cleanly.

What if the "if ..." test has a side-effect? This is an argument against 
side-effects, not against using exceptions.


But of course you are right, there are lots of good reasons for choosing 
to use an if test rather than a try block, or vice versa. I would argue 
that the try block is generally more Pythonic, and should be preferred, 
but that doesn't mean that there aren't reasons for sometimes choosing 
other strategies instead.


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


Re: Bare Excepts

2010-01-02 Thread Steven D'Aprano
On Sat, 02 Jan 2010 09:40:44 -0800, Aahz wrote:

> OTOH, if you want to do something different depending on whether the
> file exists, you need to use both approaches:
> 
> if os.path.exists(fname):
> try:
> f = open(fname, 'rb')
> data = f.read()
> f.close()
> return data
> except IOError:
> logger.error("Can't read: %s", fname) return ''
> else:
> try:
> f = open(fname, 'wb')
> f.write(data)
> f.close()
> except IOError:
> logger.error("Can't write: %s", fname)
> return None

Unfortunately, this is still vulnerable to the same sort of race 
condition I spoke about.

Even more unfortunately, I don't know that there is any fool-proof way of 
avoiding such race conditions in general. Particularly the problem of 
"open this file for writing only if it doesn't already exist".


> (This is a somewhat stupid example strictly for illustration.  A better
> and more-elaborate example would be something like trying to copy a file
> to fname and rename an existing file to '.bak' if it exists.  The tricky
> part would be trying to rename the '.bak' to fname if the copy fails.
> And yes, that's exactly what some code I wrote a few days ago does.)

Sounds interesting and useful. Would you care to share it with us, or to 
publish it as a recipe?



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


Re: Python Concurrency Workshop, Jan 14-15, 2010

2010-01-02 Thread Brian Blais

On Jan 2, 2010, at 13:21 , Mike Howard wrote:


Hi Dave,

Chicago in January?

How about moving it to Denver - it's a nice town and I live close by.

Mike

David Beazley wrote:

Python Concurrency Workshop, v2.0



I think, in the spirit of the topic, they should hold it at both  
places at the same time.



bb

--
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais
http://bblais.blogspot.com/



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


Re: Xah's Edu Corner: Teach Ourself Programing In Ten Years?

2010-01-02 Thread Wanna-Be Sys Admin
Alain Picard wrote:

> [Aplogies about the wild cross-post follow-up --- I guess the topic
> [really
> is relevant to most programming communities.]

No, it's not relevant.  Xah Lee is a self spammer, in that he spams
about himself, tries to get people hyped up about him and thinks he's
impressing people (he's not).  He continually cross posts to the
language groups he thinks he knows about (often to Perl, where there's
no mention of Perl, other than he's used it once or so).  He knows very
little about any of these languages, but likes to talk a whole lot, as
in talking up about himself and how he thinks himself a genius.  If you
could please not cross post his self-spamming replies to the Python and
Perl groups, at least, I'd appreciate it.  Most people know about him
and have filtered his posts by now.  Thanks.
-- 
Not really a wanna-be, but I don't know everything.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IOError - cannot create file (linux daemon-invoked script)

2010-01-02 Thread Steve Holden
cassiope wrote:
> I have a daemon on a Linux system that supports a number of Windows
> clients.  Among the functions is to send e-mails, which is
> sufficiently complicated that I fork() a separate process which gets
> setuid to a lesser user, and calls a python script which does the
> actual formatting and emailing (the daemon is written in C).  I want
> to save a copy of the email in a particular directory which is
> accessible to the Windows clients via samba.
> 
> The strange thing is that even with the right user-id, I cannot seem
> to write to the directory, getting an IOError exception.  Changing the
> directory to world-writable fixes this.  I can confirm the uid and gid
> for the script by having the script print these values just before
> trying to create/write the file.  Becoming the same lesser user, I
> have no problem writing a file to the same directory.
> 
Have you looked at the IOError's errno attribute to find out exactly why
the Python subprocess is unable to write to the directory?

> Is there anything that I can do to diagnose why this script is
> failing?  For various reasons I don't want to make the directory world-
> writable.
> 
I'd concur on that decision.

> This is on a Debian "squeeze" system, with python 2.5.
> 
> Thanks for any insights!

Take a closer look at the exception, that might stimulate a thought or two.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: Windows 7 : any problems installing or running Python ?

2010-01-02 Thread Lewis Cawthorne


I'm not Skippy, but I do have my wife's Toshiba Satellite running 
Windows 7 Home Premium laying around.  I figured that since I am fairly 
well set to duplicate all the information you gave in your problem, I 
would take a look.  The bad news is that Python works great on it, so 
your problem needs more troubleshooting..  I have IDLE open right now 
with no issues.  Python version: 3.1.1, Idle version: 3.1.1, Tk version: 
8.5.


Have you tried launching Python from a command line in the directory 
that you installed it to perhaps?  Maybe it would spit out an error 
message (or the interpreter might just work and maybe you have a Tk 
problem...  who knows).


Lewis Cawthorne

On 01/02/2010 09:15 AM, David M Covey Sr. wrote:


Hello Skippy,

In response to your message "Windows 7 : any problems installing or 
running Python ?" I found posted at 
(http://mail.python.org/pipermail/python-list/2009-August/1215524.html), 
I've got to say that I can't seem to get any version of Python to work 
on my computer.  I have a Toshiba Satellite laptop running Windows 7 
Home Premium with an AMD Turion II Dual-Core processor. I've tried all 
of the versions listed at http://python.org/download with no success.  
They install fine but when I try to run the IDLE (Python GUI), it does 
nothing at all.


Do you have any suggestions that might help me out here? I would 
really appreciate your input.


Thank you,

David M Covey Sr.

ad...@daffitt.com 



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


IOError - cannot create file (linux daemon-invoked script)

2010-01-02 Thread cassiope
I have a daemon on a Linux system that supports a number of Windows
clients.  Among the functions is to send e-mails, which is
sufficiently complicated that I fork() a separate process which gets
setuid to a lesser user, and calls a python script which does the
actual formatting and emailing (the daemon is written in C).  I want
to save a copy of the email in a particular directory which is
accessible to the Windows clients via samba.

The strange thing is that even with the right user-id, I cannot seem
to write to the directory, getting an IOError exception.  Changing the
directory to world-writable fixes this.  I can confirm the uid and gid
for the script by having the script print these values just before
trying to create/write the file.  Becoming the same lesser user, I
have no problem writing a file to the same directory.

Is there anything that I can do to diagnose why this script is
failing?  For various reasons I don't want to make the directory world-
writable.

This is on a Debian "squeeze" system, with python 2.5.

Thanks for any insights!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where is "urllib2" module in windows python3.1.1?

2010-01-02 Thread Hidekazu IWAKI
Hi.

Thank you for the answers.
Oh, sorry. It was one of the changes from .2.x to 3.x. I didn't know.
There are really important and a lot of changes.

Thank you!

At 01 Jan 2010 13:57:03 + Duncan Booth wrote:
> Hidekazu IWAKI  wrote:
> 
> > Hi;
> > I'd like to import "urllib2" in windows python 3.1.1, but I'm not
able to
> > do it.
> 
> Python 3 doesn't have a urllib2 module; use the urllib package instead.
> 
> See http://www.python.org/dev/peps/pep-3108/#urllib-package
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


Re: Xah's Edu Corner: Teach Ourself Programing In Ten Years?

2010-01-02 Thread Emile van Sebille

On 1/2/2010 1:14 AM Xah Lee said...

These books are the bedrock of the industry. It is not because people
are impatient, or that they wish to hurry, but rather, it is the
condition of the IT industry, in the same way modern society drives
people to live certain life styles.


Turing complete.  Once you've learned to program, learning dialects is a 
different study.  I hated Word Perfect early on, but if I were faced 
with it today I expect that everything I know about word processing 
would kelp to deploy it effectively with minimal in-depth knowledge of 
it's peculiarities.  Likewise, I don't work in perl, but I don't 
hesitate to deploy and modify perl projects to my needs, and mostly 
having only brushed up on the semantics that need to change.



No amount of patience or
proselytization can right this, except that we change the industry's
practice of quickly churning out bug-ridden software products to beat
competitors. Companies do that due to market forces, and the market
forces is a result of how people and organizations actually choose to
purchase software. In my opinion, a solution to this is by installing
the concept of responsible licenses. Please see this essay Responsible
Software Licensing and spread the word.


Licensing programmers isn't likely to turn Microsoft around. 
Enforcement doesn't carry much weight with them.  Let's work to 
strengthen that, then there's a functioning mechanism in place to work 
with.


Not-holding-my-breath-ly y'rs,

Emile

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


Re: Xah's Edu Corner: Teach Ourself Programing In Ten Years?

2010-01-02 Thread John Bokma
Alain Picard  writes:

> If you want to change the world, you start by changing yourself.

Like for starters setting a follow-up to header, especially if you spam
4 groups. But Xah is Xah.

-- 
John Bokma

Read my blog: http://johnbokma.com/
Hire me (Perl/Python): http://castleamber.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Significant whitespace

2010-01-02 Thread Emile van Sebille

On 1/1/2010 5:05 PM Steven D'Aprano said...

In Python terms, imagine if we could write

 foriinrange(10):

instead of the usual

 for i in range(10):

Since the colon makes it unambiguous that it is some sort of block
construct, and it starts with "for", it must be a for loop. Pretty
horrible, yes?



Yes -- early on I got stung by something similar.  My first programming 
job (1977?) was working in a basic dialect called Buisness Basic Level 
II (BBII).  To delete a record in the file you used a remove statement. 
 White space on the line was optional, allowing you to write lines like 
'100 remove(1,key=k$)' and '100 fori=1to10'.  REM was how a remark 
statement began, which allowed statements like "100 REM comment".  I was 
sent on-site to a customer running the prior version (BBI).  I didn't 
realize the REMOVE command changed between versions from REKEY, and it 
was only after a more experienced programmer co-wroker pointed out that 
my REMOVE statement was being interpreted as a REM that I finally found 
the bug I'd created.


Manuals and documentation, while often lacking, are as often underrated.

Emile


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


Re: Xah's Edu Corner: Teach Ourself Programing In Ten Years?

2010-01-02 Thread Alain Picard

[Aplogies about the wild cross-post follow-up --- I guess the topic really
 is relevant to most programming communities.]

Xah Lee  writes:

> To see this in a different context, suppose you need to pass a
> important Math XYZ exam or review in your career or get a certificate,
> but you don't remember your Math XYZ. 

There's a difference between cramming to remember XYZ, and never
having understood (or even been exposed to!) XYZ previously.  The
former is reasonable; the latter bound to fail.

> [SNIP] In my opinion, a solution to this is by installing
> the concept of responsible licenses. Please see this essay Responsible
> Software Licensing 

IOW, you think IT will have to become like engineering and "grow up".
I.e. when your manager says "do XYZ in 7 days", you just laugh at
them, and your risk of being fired are minimal, because all other
certified engineers who could replace you also will laugh at him
because your duty of care to your profession and certification is
greater than the one you owe your bozo manager.

I'm, personally, not holding my breath.

If you want to change the world, you start by changing yourself.

Cheers,
--ap
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception as the primary error handling mechanism?

2010-01-02 Thread Terry Reedy

On 1/2/2010 10:04 AM, Peng Yu wrote:


For my own interest, I want understand the run time behavior of python


That depends on the implementation.


and what details causes it much slower.


A language feature that slows all implementation is the dynamic 
name/slot binding and resolution. Any implementation can be made faster 
by restricting the dynamism (which makes the imlementaion one of a 
subset of Python).


> Although people choose python

for its programming efficiency, but sometimes the runtime still
matters.


There is no 'the' runtime. Whether or not there even *is* a runtime, as 
usually understoold, is a matter of the implementation.




This is an important aspect of the language.


It is an aspect of each implementation, of which there are now more than 
one.


Terry Jan Reedy

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


Re: whoops: create a splash window in python

2010-01-02 Thread Peter Decker
On Thu, Dec 31, 2009 at 12:31 AM, Ron Croonenberg  wrote:

> is there a way, in python, to create a splash window and when the program
> has completed disappears by sending a msg to it? (I tried creating two gtk
> windows but gtk_main doesn't seem to return unless it gets closed.)

It's really simple to do this in Dabo (which uses wxPython under the
hood, but is _so_ much more elegant!). When you create your app, add
the following two parameters:

app = dabo.dApp(showSplashScreen=True, splashImage="/path/to/splash.png")

That's it!

-- 

# p.d.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dangerous behavior of list(generator)

2010-01-02 Thread Martin v. Loewis
> I'm asking about why the behavior of a StopIteration exception being
> handled from the `expression` of a generator expression to mean "stop
> the loop" is accepted by "the devs" as acceptable. 

I may be late to this discussion, but the answer is "most definitely
yes". *Any* exception leads to termination of the iterator, and
StopIteration is no different:

py> def stop(e):
...   def f():
... raise e
...   return f
...
py> g = (f() for f in (lambda:1,stop(StopIteration),lambda:2))
py> g.next

py> g.next()
1
py> g.next()
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in 
  File "", line 3, in f
StopIteration
py> g.next()
Traceback (most recent call last):
  File "", line 1, in 
StopIteration
py> g = (f() for f in (lambda:1,stop(ValueError),lambda:2))
py> g.next()
1
py> g.next()
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in 
  File "", line 3, in f
ValueError
py> g.next()
Traceback (most recent call last):
  File "", line 1, in 
StopIteration

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dangerous behavior of list(generator)

2010-01-02 Thread Martin v. Loewis
>> Bottom line, I'm going to have to remove this pattern from my code:
>>
>>   foo = (foo for foo in foos if foo.bar).next()

I recommend to rewrite this like so:

def first(gen):
  try:
return gen.next()
  except StopIteration:
raise ValueError, "No first value"

foo = first(foo for foo in foos if foo.bar)

As others have said: don't let StopIteration appear unexpectedly;
IOW, consume generators right away in a loop construct (where
this first function is a loop construct as well). A different
way of writing it would be

def first(gen):
  for value in gen:
return value
  raise ValueError, "empty collection"

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Thx (Was: assert type([]) == type(()) )

2010-01-02 Thread VanceE
A big Thank You to all for the answer (and the hints).
That sure explains a lot.

Again, Thank you very much.

Cheers,
 Vance
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: assert type([]) == type(())

2010-01-02 Thread Mel
VanceE wrote:

> Just curious.
> 
> type([]) == type(())
> is False as expected
> 
> and
> 
> assert type([]) == type(())
> throws an AssertionError as expected.
> 
> However the following is not an error
> 
> for x in []:
> assert type(x) == type(())
> 
> I expected an AssertionError but get no errors at all.
> Any explaination?

Others have said what happens.  Bit of a mind bender -- here's the smallest 
change to do what you expected:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for x in [],:
...   assert type(x) == type (())
... 
Traceback (most recent call last):
  File "", line 2, in 
AssertionError
>>> 


Because of the comma, this creates a tuple containing the empty list, and 
iterates over the tuple.

Mel.

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


Re: Dynamic text color

2010-01-02 Thread Dave McCormick

WooHoo!!!
I got it!!! Yup, I am sure it can be optimized but it works!!!
John,
I have to admit that I spent several hours working on this before I 
looked at your example, then I spent another several hours getting this far.

Would never have gotten it with out you help.  Thanks!!!

Also reading the thread "Significant whitespace" helped.  I had an 
indent problem in a FOR loop.
This is a whole lot different than the MCU programming I do in ASM or 
BASIC, starting to make sense though.


Thanks again for all the help and putting up with folks like me,
Dave

Here is what I can up with.
###
from Tkinter import *
import re

redList = "red dog".split()
blueList = "blue ball".split()
greenList = "green grass".split()
def get_position(event):
   complete = Tbox.get("1.0", END)
RED
   for word in redList:
   new_Rword(complete, word)  
def new_Rword(complete, word):

   Tbox.tag_remove(word, "1.0", END)
   for matchobj in re.finditer(word, complete):
   start,end =  matchobj.span()
   Tbox.tag_add("red", "1.0 + %d chars" % start,"1.0 + %d chars" % end)

   Tbox.tag_config("red", foreground="red")
BLUE###
   for word in blueList:
   new_Bword(complete, word)  
def new_Bword(complete, word):

   Tbox.tag_remove(word, "1.0", END)
   for matchobj in re.finditer(word, complete):
   start,end =  matchobj.span()
   Tbox.tag_add("blue", "1.0 + %d chars" % start,"1.0 + %d chars" % 
end)

   Tbox.tag_config("blue", foreground="blue")
GREEN###
   for word in greenList:
   new_Gword(complete, word)  
def new_Gword(complete, word):

   Tbox.tag_remove(word, "1.0", END)
   for matchobj in re.finditer(word, complete):
   start,end =  matchobj.span()
   Tbox.tag_add("green", "1.0 + %d chars" % start,"1.0 + %d chars" 
% end)
   Tbox.tag_config("green", foreground="green")  


root = Tk()
Tbox = Text(root, width=40, height=15, wrap=CHAR,
  font="Times 14 bold", bg="#dd")
Tbox.pack()
Tbox.bind("", get_position)
Tbox.focus()
root.mainloop()


John Posner wrote:

On Thu, 31 Dec 2009 10:24:44 -0500, Dave McCormick 
wrote:


John,

Thank you for the tips.
I was changing the line-column index to a FLOAT because the search 
would return the starting position (pos) of the string, then by 
making it a FLOAT and adding the string length I was able to get the 
end position.

If "red" was on line 1 column 0..
  Tbox.tag_add("red", pos, float(pos)+.03)
   =
  Tbox.tag_add("red", 1.0, 1.3)
It was all I could come up with.


Yup, Dave, I've dug this kind of hole for myself many times!



You have convinced me about the re.finditer for this,  I think... 
Still in the prototyping mode:


   def get_position(event):
pos = Tbox.get(1.0, END)
match = [ matchobj.span() for matchobj in 
re.finditer("red", pos) ]

print "match ",match  #debug to shell


Notes:

* Variable "pos" should be "text" or "complete_text" or something 
similar.


* The first argument to the get() function must be a string:

  wrong ... complete_text = Tbox.get(1.0, END)
  right ... complete_text = Tbox.get("1.0", END)

But there's a more important problem. Is this function supposed to 
handle *one* word to be colored red, or *all the words* to be colored 
red? Here's what you want to do on each user keystroke:


1. Use get() to place the entire contents of the Text widget in a
variable, say "complete_text".

2. Use re.finditer() to generate START,END pairs for the 
substrings to
be colored red. You might find it easier to create a list from the 
iterator, though it isn't really necessary:


 start_end_pairs = list(re.finditer("red", complete_text))

3. Loop over the START,END pairs in this list. In each loop, use
tag_add() to tag one of the substrings.

[OOPS: I apologize if my suggestion in the previous post misled you. I 
described the output of finditer() as "a list of (start,end) pairs for 
an invocation of Text.tag_add()". I should have said "a list of 
(start,end) pairs, *WHICH CAN BE LOOPED OVER, FOR A SERIES OF 
INVOCATIONS* of Text.tag_add()".]


Note that the 3 steps above only handle the color red. So you want to 
place these steps in a function, then call the function for each color:


   insert_color_markup(text, "red")
   insert_color_markup(text, "green")
   insert_color_markup(text, "blue")

For each call, pass in the contents of complete_text as the first 
argument.


So the function-call hierarchy would be:

 get_position()<--- invoked on each keystroke
insert_color_markup()  <--- called once for each color
   get() then finditer() then loop-driven calls to tag_add()

I hope that makes sense to you!


Gives all of START,END pairs just fine. It is the last hint about 
line-column indexes that I am have problems with.  All of the 
documentation I can find about "text.tag_add()" uses line-column for 
coordinates.


Lie 

Re: Python Concurrency Workshop, Jan 14-15, 2010

2010-01-02 Thread Mike Howard

Hi Dave,

Chicago in January?

How about moving it to Denver - it's a nice town and I live close by.

Mike

David Beazley wrote:

  Python Concurrency Workshop, v2.0
 January 14-15, 2010
  Chicago, Illinois
http://www.dabeaz.com/chicago/concurrent.html

  *** Last Two Weeks to Register ***

Join David Beazley, author of the Python Essential Reference, for an
in-depth workshop on concurrent programming techniques and
idioms. This workshop, designed for more experienced Python
programmers, covers threads, synchronization, message passing,
multiprocessing, distributed computing, coroutines, asynchronous I/O
and other related topics with an eye towards writing programs that can
run on multiple CPU cores, clusters, or distributed systems.  A major
theme of the workshop is to explore and understand different
programming techniques, their associated performance properties, and
other tradeoffs.  You'll definitely walk away with new insight and a
better understanding of how different parts of Python work under the
covers.

Workshop attendance is strictly limited to six people. More information,
including a detailed topic index, is available at:

http://www.dabeaz.com/chicago/concurrent.html

Hopefully I'll see you in a few weeks! 



  


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


Re: Bare Excepts

2010-01-02 Thread Aahz
In article <034f1009$0$1277$c3e8...@news.astraweb.com>,
Steven D'Aprano   wrote:
>
>The try version is also better whenever there could be a race condition. 
>For example, when opening a file, you might be tempted to do this:
>
>if os.path.exists(filename):
>f = open(filename, 'r')
>text = f.read()
>else:
>print "file missing"
>
>but that is dangerous in a multi-processing computer system (which nearly 
>all computers are these days). The problem is that in the fraction of a 
>second after you test that the file exists, some other process might come 
>along and delete it and the open call will then fail. So to be safe, you 
>have to do this:
>
>if os.path.exists(filename):
>try:
>f = open(filename, 'r')
>except IOError:
>print "file missing"
>else:
>text = f.read()
>else:
>print "file missing"
>
>But of course now the test is redundant, and all you really need is the 
>try...except block:
>
>try:
>f = open(filename, 'r')
>except IOError:
>print "file missing"
>else:
>text = f.read()

OTOH, if you want to do something different depending on whether the file
exists, you need to use both approaches:

if os.path.exists(fname):
try:
f = open(fname, 'rb')
data = f.read()
f.close()
return data
except IOError:
logger.error("Can't read: %s", fname)
return ''
else:
try:
f = open(fname, 'wb')
f.write(data)
f.close()
except IOError:
logger.error("Can't write: %s", fname)
return None

(This is a somewhat stupid example strictly for illustration.  A better
and more-elaborate example would be something like trying to copy a file
to fname and rename an existing file to '.bak' if it exists.  The tricky
part would be trying to rename the '.bak' to fname if the copy fails.
And yes, that's exactly what some code I wrote a few days ago does.)
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: assert type([]) == type(())

2010-01-02 Thread Jan Kaliszewski

However the following is not an error

for x in []:
assert type(x) == type(())


Trying to iterate over an empty sequence or iterator causes
0 (zero) steps of iteration -- so above assert statement is
never run.

Cheers,
*j

--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


Re: assert type([]) == type(())

2010-01-02 Thread Tim Chase

However the following is not an error

for x in []:
assert type(x) == type(())

I expected an AssertionError but get no errors at all.
Any explaination?



  number_of_times_through_the_loop = 0
  for x in []:
assert type(x) == type(())
number_of_times_through_the_loop += 1

  print number_of_times_through_the_loop

make sense? :)

-tkc



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


Re: assert type([]) == type(())

2010-01-02 Thread Wojciech Muła
VanceE  wrote:

> for x in []:
> assert type(x) == type(())
> 
> I expected an AssertionError but get no errors at all.
> Any explaination?

[] is an empty sequence, so your loop executes exactly 0 times. :)

for x in [None]:
assert ...

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


Re: assert type([]) == type(())

2010-01-02 Thread Josh Holland
On 2010-01-02, VanceE  wrote:
> for x in []:
> assert type(x) == type(())
>
> I expected an AssertionError but get no errors at all.
> Any explaination?

That loop never runs. It immediately raises a StopIteration and the body
is never executed.
cf.

for x in []:
print "In loop body"

You won't see anything printed.

OTOH, look at this one:

for x in [[]]: # a list containing an empty list
assert type(x) == type(())

That will raise the AssertionError as expected.


-- 
Josh "dutchie" Holland 
http://joshh.co.uk
http://twitter.com/jshholland
http://identi.ca/jshholland
-- 
http://mail.python.org/mailman/listinfo/python-list


assert type([]) == type(())

2010-01-02 Thread VanceE
Just curious.

type([]) == type(())
is False as expected

and 

assert type([]) == type(())
throws an AssertionError as expected.

However the following is not an error

for x in []:
assert type(x) == type(())

I expected an AssertionError but get no errors at all.
Any explaination?

BTW I'm using Python2.5 and tried
type(None) == type(())
which is False as expected.

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


Re: Potential Conflicts by Installing Two Versions of Python (Windows)?

2010-01-02 Thread Nobody
On Fri, 01 Jan 2010 17:37:40 -0800, W. eWatson wrote:

> I suspect that if one installs v2.4 and 2.5, or any two versions, that 
> one will dominate, or there will be a conflict.  I suppose it would not 
> be possible to choose which one should be used. Comments?

The only inherent conflict is that you can only associate the .py
extension with one version for any particular user.

This only matters if you need to be able to run Python files as if they
were executables, e.g. by double-clicking them in Explorer. If you invoke
Python files by explicitly specifying the Python interpreter, there isn't
a problem.



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


Re: Append Problem

2010-01-02 Thread Victor Subervi
On Sat, Jan 2, 2010 at 10:46 AM, Steve Holden  wrote:

>
> See the .sig. Of course I'm a businessman.
>

Nice site! In that case, respectfully I disagree!
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Append Problem

2010-01-02 Thread Steve Holden
Victor Subervi wrote:
[...]
> While the learning you have performed in getting this system to work
> (for some rather questionable value of "work", I can't help feeling, but
> we'll overlook the horrors induced by your lack of programming
> experience) is a testament to your persistence (and this list's
> tolerance) I can't help feeling that it leaves you no better qualified
> to supervise programmers.
> 
> 
> Ah, but you aren't a businessman, are you? You wouldn't know...
> beno

See the .sig. Of course I'm a businessman.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Append Problem

2010-01-02 Thread Victor Subervi
There isn't a tuple is sight there - you are dealing with lists, and you

> are modifying the very list you are iterating over.
>
> Try rewriting the code to create a new list from the old one (i.e.
> iterate over catChains and have your code append to an initially empty
> list called, for example, newCatChains, then finally throw the old list
> away with
>
>  catChains = newCatChains
>
> or some such).
>

Thanks!


> > PS (Mainly, I believe, for Carsten): While programming is difficult for
> > me, I am writing (and have pretty much finished) this (almost) fully
> > automated shopping cart. I don't intend to write any other serious
> > programming project. However, a shopping cart is vital to my Web design
> > business. I will hereafter outsource my programming. But, as any good
> > supervisor knows, one has to know how to supervise! If I can't read
> > code, then I'm at the mercy of my programmer(s). If I can't afford to
> > hire some top gun who can supervise, then it's my responsibility. Plus,
> > if my programmer(s) quit, I need to step in and take over. I'm an ok
> > businessman, and absolutely gifted in sales. And in poetry ;)
> > beno
> >
> Given that there are umpteen thousand hosting services who will let you
> create a web site with shopping cart included I have to question whether
> you are making the best use of your time here. Of course it's your time,
> and your decision, so please don't think I am dictating to you.
>

It doesn't give one the control upon which I insist. No. That's not an
option.

>
> While the learning you have performed in getting this system to work
> (for some rather questionable value of "work", I can't help feeling, but
> we'll overlook the horrors induced by your lack of programming
> experience) is a testament to your persistence (and this list's
> tolerance) I can't help feeling that it leaves you no better qualified
> to supervise programmers.
>

Ah, but you aren't a businessman, are you? You wouldn't know...
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception as the primary error handling mechanism?

2010-01-02 Thread Martin v. Loewis
> For my own interest, I want understand the run time behavior of python
> and what details causes it much slower. Although people choose python
> for its programming efficiency, but sometimes the runtime still
> matters. This is an important aspect of the language. I'm wondering
> this is not even documented.  Why everybody has to go to the source
> code to understand it?

There are two answers to this question:

a) Because the source is the most precise and most complete way of
documenting it. Any higher-level documentation would necessarily be
incomplete.
b) Because nobody has contributed documentation.

The two causes correlate: because writing documentation of VM
internals takes a lot of effort and is of questionable use, nobody
has written any.

> Are you sure that there is no document that describes how python is
> working internally (including exceptions)?

Such documents certainly exist, but not as part of the Python
distribution. See

http://wiki.python.org/moin/CPythonVmInternals

for one such document.

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception as the primary error handling mechanism?

2010-01-02 Thread Peng Yu
On Sat, Jan 2, 2010 at 6:05 AM, Diez B. Roggisch  wrote:
> Peng Yu schrieb:
>>
>> On Thu, Dec 31, 2009 at 11:24 PM, Chris Rebert  wrote:
>>>
>>> On Thu, Dec 31, 2009 at 8:47 PM, Peng Yu  wrote:

 I observe that python library primarily use exception for error
 handling rather than use error code.

 In the article API Design Matters by Michi Henning

 Communications of the ACM
 Vol. 52 No. 5, Pages 46-56
 10.1145/1506409.1506424
 http://cacm.acm.org/magazines/2009/5/24646-api-design-matters/fulltext

 It says "Another popular design flaw—namely, throwing exceptions for
 expected outcomes—also causes inefficiencies because catching and
 handling exceptions is almost always slower than testing a return
 value."

 My observation is contradicted to the above statement by Henning. If
 my observation is wrong, please just ignore my question below.

 Otherwise, could some python expert explain to me why exception is
 widely used for error handling in python? Is it because the efficiency
 is not the primary goal of python?
>>>
>>> Correct; programmer efficiency is a more important goal for Python
>>> instead.
>>> Python is ~60-100x slower than C;[1] if someone is worried by the
>>> inefficiency caused by exceptions, then they're using completely the
>>> wrong language.
>>
>> Could somebody let me know how the python calls and exceptions are
>> dispatched? Is there a reference for it?
>
> The source?
>
> http://python.org/ftp/python/2.6.4/Python-2.6.4.tgz
>
> These are really deep internals that - if they really concern you - need
> intensive studies, not casual reading of introductionary documents. IMHO you
> shouldn't worry, but then, there's a lot things you seem to care I
> wouldn't... :)

For my own interest, I want understand the run time behavior of python
and what details causes it much slower. Although people choose python
for its programming efficiency, but sometimes the runtime still
matters. This is an important aspect of the language. I'm wondering
this is not even documented.  Why everybody has to go to the source
code to understand it?

Are you sure that there is no document that describes how python is
working internally (including exceptions)?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Windows 7 : any problems installing or running Python ?

2010-01-02 Thread David M Covey Sr.
Hello Skippy,

 

In response to your message "Windows 7 : any problems installing or running
Python ?" I found posted at
(http://mail.python.org/pipermail/python-list/2009-August/1215524.html),
I've got to say that I can't seem to get any version of Python to work on my
computer.  I have a Toshiba Satellite laptop running Windows 7 Home Premium
with an AMD Turion II Dual-Core processor. I've tried all of the versions
listed at http://python.org/download with no success.  They install fine but
when I try to run the IDLE (Python GUI), it does nothing at all.

 

Do you have any suggestions that might help me out here? I would really
appreciate your input.

 

Thank you,

David M Covey Sr.

ad...@daffitt.com

 

 

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


Re: Bare Excepts

2010-01-02 Thread Lie Ryan

On 1/2/2010 9:42 PM, Steven D'Aprano wrote:

On Fri, 01 Jan 2010 15:27:57 -0800, myle wrote:


Why we should prefer ``if: ...'' over  a ``try: ... except something:
pass'' block?


We shouldn't, not in general.


One exception (pun intended) is if the try-block have a side effect that 
is difficult to cleanup cleanly.

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


Re: Endless loop

2010-01-02 Thread vsoler
On 2 ene, 14:21, Ulrich Eckhardt  wrote:
> vsoler wrote:
> > class stepper:
> >     def __getitem__(self, i):
> >         return self.data[i]
>
> > X=stepper()
> > X.data="Spam"
> > for item in X:
> >     print item,
>
> > ... what I get is     S p a m     which seems logical to me since the
> > loop stops after the 4th character.
>
> I think you're mistaking the cause and effect here, see below.
>
> > class stepper:
> >     def __getitem__(self, i):
> >         return i
>
> > X=stepper()
> > X.data="Spam"
> > for item in X:
> >     print item,
>
> > ... what I get is an endless loop, starting at zero:    0 1 2 3 4 5 6
> > 7 8 9 10 11  and so on.
>
> > My question is: why does this second script not stop after printing
> > number 3?  what made the first one stop while the second one will not?
>
> First thing to observe in the second case is that X's data field is not used
> anywhere. The field is set and then not used any further. In particular, it
> is not used to somehow get the length of the sequence to return.
>
> Now, "spam"[4] will raise an IndexError, thus terminating the iteration. The
> second implementation which only returns the index never will do that, so
> the iteration never stops.
>
> Uli

It's clear, I now understand.

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


Re: Append Problem

2010-01-02 Thread Steve Holden
Victor Subervi wrote:
> Hi;
> I have the following code snippet:
> 
>   print 'Original: ', catChains, ''
>   while i < MAXLEVEL:
> flag = 0
> j = 0
> while j < len(parents):
>   for chain in catChains:
> if parents[j] == chain[len(chain)-1]:
>   chain.append(children[j])
>   print '1: ', catChains, ''
>   catChains.append(chain)
>   print '2: ', catChains, ''
>   flag = 1
>   j += 1
> i += 1
> if flag == 0:
>   break
>   print 'Final: ', catChains
>  
> which prints this:
> 
> Original: [['prodCat1'], ['prodCat2']]
> 1: [['prodCat1', 'prodCat3'], ['prodCat2']]
> 2: [['prodCat1', 'prodCat3'], ['prodCat2'], ['prodCat1', 'prodCat3']]
> 1: [['prodCat1', 'prodCat3', 'prodCat5'], ['prodCat2'], ['prodCat1',
> 'prodCat3', 'prodCat5']]
> 2: [['prodCat1', 'prodCat3', 'prodCat5'], ['prodCat2'], ['prodCat1',
> 'prodCat3', 'prodCat5'], ['prodCat1', 'prodCat3', 'prodCat5']]
> Final: [['prodCat1', 'prodCat3', 'prodCat5'], ['prodCat2'], ['prodCat1',
> 'prodCat3', 'prodCat5'], ['prodCat1', 'prodCat3', 'prodCat5']]
> 
> Why is it that my append statement *deletes* elements of my tuple? The
> entire code follows:
> 
There isn't a tuple is sight there - you are dealing with lists, and you
are modifying the very list you are iterating over.

Try rewriting the code to create a new list from the old one (i.e.
iterate over catChains and have your code append to an initially empty
list called, for example, newCatChains, then finally throw the old list
away with

  catChains = newCatChains

or some such).

[code snipped]
> 
> PS (Mainly, I believe, for Carsten): While programming is difficult for
> me, I am writing (and have pretty much finished) this (almost) fully
> automated shopping cart. I don't intend to write any other serious
> programming project. However, a shopping cart is vital to my Web design
> business. I will hereafter outsource my programming. But, as any good
> supervisor knows, one has to know how to supervise! If I can't read
> code, then I'm at the mercy of my programmer(s). If I can't afford to
> hire some top gun who can supervise, then it's my responsibility. Plus,
> if my programmer(s) quit, I need to step in and take over. I'm an ok
> businessman, and absolutely gifted in sales. And in poetry ;)
> beno
> 
Given that there are umpteen thousand hosting services who will let you
create a web site with shopping cart included I have to question whether
you are making the best use of your time here. Of course it's your time,
and your decision, so please don't think I am dictating to you.

While the learning you have performed in getting this system to work
(for some rather questionable value of "work", I can't help feeling, but
we'll overlook the horrors induced by your lack of programming
experience) is a testament to your persistence (and this list's
tolerance) I can't help feeling that it leaves you no better qualified
to supervise programmers.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: Endless loop

2010-01-02 Thread Ulrich Eckhardt
vsoler wrote:
> class stepper:
> def __getitem__(self, i):
> return self.data[i]
> 
> X=stepper()
> X.data="Spam"
> for item in X:
> print item,
> 
> ... what I get is S p a m which seems logical to me since the
> loop stops after the 4th character.

I think you're mistaking the cause and effect here, see below.

> class stepper:
> def __getitem__(self, i):
> return i
> 
> X=stepper()
> X.data="Spam"
> for item in X:
> print item,
> 
> ... what I get is an endless loop, starting at zero:0 1 2 3 4 5 6
> 7 8 9 10 11  and so on.
> 
> My question is: why does this second script not stop after printing
> number 3?  what made the first one stop while the second one will not?

First thing to observe in the second case is that X's data field is not used 
anywhere. The field is set and then not used any further. In particular, it 
is not used to somehow get the length of the sequence to return.

Now, "spam"[4] will raise an IndexError, thus terminating the iteration. The 
second implementation which only returns the index never will do that, so 
the iteration never stops.

Uli

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


Re: Endless loop

2010-01-02 Thread alexru
On Jan 2, 3:50 pm, vsoler  wrote:

> My question is: why does this second script not stop after printing
> number 3?  what made the first one stop while the second one will not?

First one will raise IndexError when string is over, second one won't.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trying to run a sudo command from script

2010-01-02 Thread Steve Holden
Paul Kölle wrote:
> Am 01.01.2010 23:55, schrieb Kent Tenney:
>> Howdy,
> Hi Kent,
> 
>> A script running as a regular user sometimes wants
>> to run sudo commands.
>>
>> It gets the password with getpass.
>> pw = getpass.getpass()
>>
>> I've fiddled a bunch with stuff like
>> proc = subprocess.Popen('sudo touch /etc/foo'.split(),
>> stdin=subprocess.PIPE)
>> proc.communicate(input=pw)
> If you don't use shell=True you have to provide the full path to
> commands (and split command and parameters as you do). So eather of this
> works for me:
> p = Popen('/usr/bin/sudo /usr/bin/touch /tmp/foo.txt'.split(),
> stdin=PIPE, stdout=PIPE)
> 
> p = Popen('/usr/bin/sudo /usr/bin/touch /tmp/foo2.txt', stdin=PIPE,
> stdout=PIPE, shell=True)
> 
> The bad news is: It this gives me a password promt inside the
> interactive interpreter. Seems you can't catch stdout this way.
> 
[please put your answers *after* the questions!]

I don't think it so much that you can't catch stdout. Rather, sudo
ensures it is talking to the user by explicitly reading the password
from /dev/tty. Consequently there is no way to send the password value
over the process's stdin, hence the need to use pexpect.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: Exception as the primary error handling mechanism?

2010-01-02 Thread Martin v. Loewis

> I mentioned an "exception stack" above, though I'm not 100% sure if that is 
> the proper term. I think that exceptions can be stacked upon each other 
> (e.g. an HTTPD throwing a high-level RequestError when it encounters a low-
> level IOError) and that that is also how the backtrace is implemented, but 
> I'm not sure about that.

Not exactly. In this scenario, the IOError exception gets caught, its
entire traceback discarded, and an entirely new exception RequestError
gets raised (that has no connection to the original IOError anymore,
unless the httpd code explicitly links the two).

Instead, the traceback objects are created for a single exception.
They are essentially the same as the call stack, just in reverse
order (so that you get the "most recent call last" traceback output).
Each traceback links to a frame object, and a next traceback object.

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Endless loop

2010-01-02 Thread vsoler
hello,

I'm learning Python and OOP, and I am confronted with a rather
theoretical problem.

If I run the following script:

class stepper:
def __getitem__(self, i):
return self.data[i]

X=stepper()
X.data="Spam"
for item in X:
print item,

... what I get is S p a m which seems logical to me since the
loop stops after the 4th character.


However if I run

class stepper:
def __getitem__(self, i):
return i

X=stepper()
X.data="Spam"
for item in X:
print item,

... what I get is an endless loop, starting at zero:0 1 2 3 4 5 6
7 8 9 10 11  and so on.

My question is: why does this second script not stop after printing
number 3?  what made the first one stop while the second one will not?

Thanks for your advise

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


Re: Exception as the primary error handling mechanism?

2010-01-02 Thread Diez B. Roggisch

Peng Yu schrieb:

On Thu, Dec 31, 2009 at 11:24 PM, Chris Rebert  wrote:

On Thu, Dec 31, 2009 at 8:47 PM, Peng Yu  wrote:

I observe that python library primarily use exception for error
handling rather than use error code.

In the article API Design Matters by Michi Henning

Communications of the ACM
Vol. 52 No. 5, Pages 46-56
10.1145/1506409.1506424
http://cacm.acm.org/magazines/2009/5/24646-api-design-matters/fulltext

It says "Another popular design flaw—namely, throwing exceptions for
expected outcomes—also causes inefficiencies because catching and
handling exceptions is almost always slower than testing a return
value."

My observation is contradicted to the above statement by Henning. If
my observation is wrong, please just ignore my question below.

Otherwise, could some python expert explain to me why exception is
widely used for error handling in python? Is it because the efficiency
is not the primary goal of python?

Correct; programmer efficiency is a more important goal for Python instead.
Python is ~60-100x slower than C;[1] if someone is worried by the
inefficiency caused by exceptions, then they're using completely the
wrong language.


Could somebody let me know how the python calls and exceptions are
dispatched? Is there a reference for it?


The source?

http://python.org/ftp/python/2.6.4/Python-2.6.4.tgz

These are really deep internals that - if they really concern you - need 
intensive studies, not casual reading of introductionary documents. IMHO 
you shouldn't worry, but then, there's a lot things you seem to care I 
wouldn't... :)


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


Re: Significant whitespace

2010-01-02 Thread Steven D'Aprano
On Sat, 02 Jan 2010 10:42:39 +, Duncan Booth wrote:

> Donn  wrote:
> 
>> On Saturday 02 January 2010 00:02:36 Dan Stromberg wrote:
>>> I put together a page about significant whitespace (and the lack
>>> thereof).
>> The only thing about Python's style that worries me is that it can't be
>> compressed like javascript can*, and perhaps that will prevent it
>> becoming a browser-side language one day. How I'd love to code PyQuery
>> instead of JQuery!
> 
> You can do simple white-space removal as easily in Python as in
> Javascript, you just need to maintain a minimal indentation and as you
> don't need Javascript's curly braces there probably isn't must
> difference especially once the code has been gzipped for transfer to the
> browser.

I don't understand why you would bother removing whitespace, particularly 
if you're going to gzip the file anyway.

I've just picked one of my modules at random: about 47K in size, of which 
about 15K is whitespace. Compressed to .bz, it is 12K, and if I strip the 
whitespace from it first and then compress it, it is 11K.

So, even though whitespace (spaces and newlines) make up about 30% of the 
original source, once compressed they represent only about 8%. Given the 
extra complexity of stripping whitespace, I don't know that it's 
worthwhile. It's not like you're going to reduce the download time from 
three hours to one; it's more like 2 seconds to 1.5.



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


Re: Exception as the primary error handling mechanism?

2010-01-02 Thread Ulrich Eckhardt
Peng Yu wrote:
> Could somebody let me know how the python calls and exceptions are
> dispatched? Is there a reference for it?

I'm not a Python expert, but I have read some parts of the implementation. 
Hopefully someone steps up if I misrepresent things here...

In order to understand Python exception handling, take a look at various C 
function implementations. You will see that they commonly return a pointer 
to a Python object (PyObject*), even if it is a pointer to the 'None' 
singleton. So, a function in Python _always_ returns something, even if it 
is 'None'.

If, at the C level, a function doesn't return anything (i.e. a C NULL 
pointer) that means that the function raised an exception. Checking this 
pointer is pretty easy, typically you check that, clean up and return NULL 
yourself. Further functions for manipulating the exception stack and 
declarations of exception types and singletons are found in pyerrors.h (in 
Python 2.5, at least).

I mentioned an "exception stack" above, though I'm not 100% sure if that is 
the proper term. I think that exceptions can be stacked upon each other 
(e.g. an HTTPD throwing a high-level RequestError when it encounters a low-
level IOError) and that that is also how the backtrace is implemented, but 
I'm not sure about that.


Hopefully someone can confirm or correct me here and that it helped you.

Cheers!

Uli


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


Append Problem

2010-01-02 Thread Victor Subervi
Hi;
I have the following code snippet:

  print 'Original: ', catChains, ''
  while i < MAXLEVEL:
flag = 0
j = 0
while j < len(parents):
  for chain in catChains:
if parents[j] == chain[len(chain)-1]:
  chain.append(children[j])
  print '1: ', catChains, ''
  catChains.append(chain)
  print '2: ', catChains, ''
  flag = 1
  j += 1
i += 1
if flag == 0:
  break
  print 'Final: ', catChains

which prints this:

Original: [['prodCat1'], ['prodCat2']]
1: [['prodCat1', 'prodCat3'], ['prodCat2']]
2: [['prodCat1', 'prodCat3'], ['prodCat2'], ['prodCat1', 'prodCat3']]
1: [['prodCat1', 'prodCat3', 'prodCat5'], ['prodCat2'], ['prodCat1',
'prodCat3', 'prodCat5']]
2: [['prodCat1', 'prodCat3', 'prodCat5'], ['prodCat2'], ['prodCat1',
'prodCat3', 'prodCat5'], ['prodCat1', 'prodCat3', 'prodCat5']]
Final: [['prodCat1', 'prodCat3', 'prodCat5'], ['prodCat2'], ['prodCat1',
'prodCat3', 'prodCat5'], ['prodCat1', 'prodCat3', 'prodCat5']]

Why is it that my append statement *deletes* elements of my tuple? The
entire code follows:

#! /usr/bin/python

import string
import cgitb; cgitb.enable()
import MySQLdb
import cgi
import sys,os
sys.path.append(os.getcwd())
from login import login
import datetime, Cookie, random
from particulars import title
from templateFrame import top, bottom
from particulars import myCookie
import time
import fpformat
from sets import Set

def makeNav():
  print 'Content-type: text/html\r\n'
  print '\n'
  user, passwd, db, host = login()
  db = MySQLdb.connect(host, user, passwd, db)
  cursor= db.cursor()
  form = cgi.FieldStorage()
  store = form.getfirst('store')
  sql = 'select Category, Parent from categories%s;' % (store[0].upper() +
store[1:])
  cursor.execute(sql)
  children = [itm[0] for itm in cursor]
  parents = [itm[1] for itm in cursor]
  catChains = []
  i = 0
  while i < len(parents):
if parents[i] is None:
  child = []
  child.append(children[i])
  catChains.append(child)
i += 1
  i = 0
  MAXLEVEL = 10
  print 'Original: ', catChains, ''
  while i < MAXLEVEL:
flag = 0
j = 0
while j < len(parents):
  for chain in catChains:
if parents[j] == chain[len(chain)-1]:
  chain.append(children[j])
  print '1: ', catChains, ''
  catChains.append(chain)
  print '2: ', catChains, ''
  flag = 1
  j += 1
i += 1
if flag == 0:
  break
  print 'Final: ', catChains
  lastChain = []
  lastChain.append('root')
  print ""
  for chain in sorted(catChains):
link = ''
i = 0
while i < len(chain):
  link += '%s/' % string.replace(chain[i], ' ', '_')
  i += 1
link = '%s.py' % link[:-1]
if len(chain) == len(lastChain)+1: # This is a new category level within
the current chain.
  print "%s" % (' ' * (len(chain)-1))
elif len(chain) == len(lastChain): # This is a new category within in
the same parent level.
  pass # This is just here to remind me how this works!
j = 0
while len(chain) < len(lastChain)-j:
  j += 1
  print "%s" % (' ' * (len(chain)-j))
print "%s%s" % ((' ' *
len(chain)), chain[len(chain)-1].lower(), link, chain[len(chain)-1])
lastChain = chain
  print ''

makeNav()

PS (Mainly, I believe, for Carsten): While programming is difficult for me,
I am writing (and have pretty much finished) this (almost) fully automated
shopping cart. I don't intend to write any other serious programming
project. However, a shopping cart is vital to my Web design business. I will
hereafter outsource my programming. But, as any good supervisor knows, one
has to know how to supervise! If I can't read code, then I'm at the mercy of
my programmer(s). If I can't afford to hire some top gun who can supervise,
then it's my responsibility. Plus, if my programmer(s) quit, I need to step
in and take over. I'm an ok businessman, and absolutely gifted in sales. And
in poetry ;)
beno

-- 
The Logos has come to bear
http://logos.13gems.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Significant whitespace

2010-01-02 Thread Duncan Booth
Donn  wrote:

> On Saturday 02 January 2010 00:02:36 Dan Stromberg wrote:
>> I put together a page about significant whitespace (and the lack
>> thereof). 
> The only thing about Python's style that worries me is that it can't
> be compressed like javascript can*, and perhaps that will prevent it
> becoming a browser-side language one day. How I'd love to code PyQuery
> instead of JQuery! 

You can do simple white-space removal as easily in Python as in Javascript, 
you just need to maintain a minimal indentation and as you don't need 
Javascript's curly braces there probably isn't must difference especially 
once the code has been gzipped for transfer to the browser.

You can already run Python client side in certain situations (e.g. 
Silverlight), but if you want to write Python for the browser portably 
today have a look at Pyjamas http://code.google.com/p/pyjamas/ .

Google's Chrome also supports native code (currently just for x86 machines 
though they have said they intend to support ARM), so there should be some 
potential there to compile a Python interpreter that runs client-side in 
Google's sandbox: see http://plash.beasts.org/wiki/NativeClient for an 
early port.

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


Re: Bare Excepts

2010-01-02 Thread Steven D'Aprano
On Fri, 01 Jan 2010 15:27:57 -0800, myle wrote:

> Why we should prefer ``if: ...'' over  a ``try: ... except something:
> pass'' block?

We shouldn't, not in general.

Often, the if test is just as expensive as actually doing it. E.g.:

if x in mylist:
position = mylist.index(x)
else:
do_something()

has to search the list twice, first to see if x is there, then search it 
again to return the index. That does double the work needed. A better way 
is:

try:
position = mylist.index(x)
except ValueError:
do_something()

unless you expect that x is often missing: setting up the try block is 
much faster than the test, but catching the exception is much, much 
slower. So long as exceptions are rare, it is better to use the try 
version (also known as "Better to ask forgiveness than permission"). But 
if exceptions are common, it may be better to use the if version ("Look 
before you leap").

The try version is also better whenever there could be a race condition. 
For example, when opening a file, you might be tempted to do this:

if os.path.exists(filename):
f = open(filename, 'r')
text = f.read()
else:
print "file missing"

but that is dangerous in a multi-processing computer system (which nearly 
all computers are these days). The problem is that in the fraction of a 
second after you test that the file exists, some other process might come 
along and delete it and the open call will then fail. So to be safe, you 
have to do this:

if os.path.exists(filename):
try:
f = open(filename, 'r')
except IOError:
print "file missing"
else:
text = f.read()
else:
print "file missing"


But of course now the test is redundant, and all you really need is the 
try...except block:

try:
f = open(filename, 'r')
except IOError:
print "file missing"
else:
text = f.read()


(Note: the above is still not entirely robust. For example, if you try to 
read a file that you don't have permission to read, it wrongly reports it 
is missing instead of a permissions error. To make this more robust, you 
need to inspect the exception and check the error code.)



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


Re: Trying to run a sudo command from script

2010-01-02 Thread Paul Kölle

Am 01.01.2010 23:55, schrieb Kent Tenney:

Howdy,

Hi Kent,


A script running as a regular user sometimes wants
to run sudo commands.

It gets the password with getpass.
pw = getpass.getpass()

I've fiddled a bunch with stuff like
proc = subprocess.Popen('sudo touch /etc/foo'.split(), stdin=subprocess.PIPE)
proc.communicate(input=pw)
If you don't use shell=True you have to provide the full path to 
commands (and split command and parameters as you do). So eather of this 
works for me:
p = Popen('/usr/bin/sudo /usr/bin/touch /tmp/foo.txt'.split(), 
stdin=PIPE, stdout=PIPE)


p = Popen('/usr/bin/sudo /usr/bin/touch /tmp/foo2.txt', stdin=PIPE, 
stdout=PIPE, shell=True)


The bad news is: It this gives me a password promt inside the 
interactive interpreter. Seems you can't catch stdout this way.


hth
 Paul



getting assorted errors with all variations I try.

Googling says use pexpect, but I'd prefer a stdlib solution.

Any help appreciated.

Thanks,
Kent



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


Xah's Edu Corner: Teach Ourself Programing In Ten Years?

2010-01-02 Thread Xah Lee
On Dec 25 2009, 12:44 am, r...@rpw3.org (Rob Warnock) wrote:
> jos...@corporate-world.lisp.de  wrote:
>
> +---
> | p...@informatimago.com (Pascal J. Bourguignon) wrote:
> | > LOL Yeah right! Give gavino ten years of rest to let
> | > his unconscious mind work on it!
> |
> | Norvig's 'Teach Yourself Programming in Ten Years' (
> |http://norvig.com/21-days.html
> | ) gets a new meaning...
> +---


See:

• The Condition of Industrial Programers
  http://xahlee.org/UnixResource_dir/writ/it_programers.html

plain text version follows.

--
The Condition of Industrial Programers

Xah Lee, 2006-05

Before i stepped into the computing industry, my first industrial
programing experience was at Wolfram Research Inc as a intern in 1995.
(Wolfram Research is famously known for their highly successful
flagship product Mathematica) I thought, that the programers at
Wolfram are the world's top mathematicians, gathered together to
research and decide and write a extremely advanced technology. But i
realized it is not so. Not at all. In fact, we might say it's just a
bunch of Ph Ds (or equivalent experience). The people there, are not
unlike average white-collar Joes. Each working individually. And,
fights and bouts of arguments between co-workers are not uncommon.
Sometimes downright ugly in emails. Almost nothing is as i naively
imagined, as if some world's top mathematicians are gathered together
there, daily to confer and solve the world's top problems as in some
top secret government agency depicted in movies.

Well, that was my introduction to the industry. The bulk of my
surprise is due to my naiveness and inexperience of the industry, of
any industry, as i was just a intern and this is my first experience
seeing how the real world works.

After Wolfram, after a couple of years i went into the web programing
industry in 1998, using unix, Perl, Apache, Java, database
technologies, in the center of world's software technology the Silicon
Valley. My evaluation of industrial programers and how software are
written is a precipitous fall from my observations at Wolfram. In the
so-called Info Tech industry, the vast majority of programers are
poorly qualified. I learned this from my colleagues, and in dealing
with programers from other companies, service providers, data centers,
sys admins, API gateways, and duties of field tutoring. I didn't think
i have very qualified expertise in what i do, but the reality i
realized is that most are far lesser than me, and that is the common
situation. That they have no understanding of basic mathematics such
as trigonometry or calculus. Most have no interest in math whatsoever,
and would be hard pressed for them to explain what is a “algorithm”.

I have always thought, that programing X software of field Y usually
means that the programers are thoroughly fluent in languages,
protocols, tools of X, and also being a top expert in field of Y. But
to my great surprise, the fact is that that is almost never the case.
In fact, most of the time the programers simply just had to learn a
language, protocol, software tool, right at the moment as he is trying
to implement a software for a field he never had experience in. I
myself had to do jobs half of the time i've never done before.
Constantly I'm learning new languages, protocols, systems, tools,
APIs, other rising practices and technologies, reading semi-written or
delve into non-existent docs. It is the norm in the IT industry, that
most products are really produces of learning experiences. Extremely
hurried grasping of new technologies in competition with deadlines.
There is in fact little actual learning going on, as there are immense
pressure to simply “get it to (demonstrably) work” and ship it.

Thinking back, in fact the Wolfram people are the most knowledgeable
and inquisitive people i've met as colleagues, by far.

What prompted me to write this essay is after reading the essay Teach
Yourself Programming in Ten Years by Peter Norvig, 2001, at
http://www.norvig.com/21-days.html. In which, the LISP dignitary Peter
Norvig derides the widely popular computing books in the name of
Teaching Yourself X In (Fast) Days. Although i agree with his
sentiment that a language or technology takes time to master and use
well, that these books form somewhat of a damaging fad and subtly
multiply ignorance, but he fails to address the main point, that is:
the cause of the popularity of such books, and how to remedy the
situation.

When you work in the industry, and are given a responsibility of
coding in some new language the company decided to use, or emerging
protocol (such as voice-chat protocols or cellphone internet), or your
engineering group adopted a new team coding/reviewing process, you are
not going to tell you boss “nah, i want to do a good job so i'll study
the issue a few months before i contribute”. Chances are, you are
going to run out and buy a copy of “XYZ in 7 days”, and complete t

Re: Exception as the primary error handling mechanism?

2010-01-02 Thread Stephen Hansen
On Fri, Jan 1, 2010 at 5:36 PM, Peng Yu  wrote:

> >> Otherwise, could some python expert explain to me why exception is
> >> widely used for error handling in python? Is it because the efficiency
> >> is not the primary goal of python?
> >
> > Correct; programmer efficiency is a more important goal for Python
> instead.
> > Python is ~60-100x slower than C;[1] if someone is worried by the
> > inefficiency caused by exceptions, then they're using completely the
> > wrong language.
>
> Could somebody let me know how the python calls and exceptions are
> dispatched? Is there a reference for it?
>

I don't quite understand what you're asking here, but it sounds almost like
you're looking at the question from an incorrect POV. "Exceptions" are a
general sort of concept in computer science and various computer programming
languages, but they are not at all equal from one language to another. The
document you referenced was speaking to a particular implementation of the
concept, and speaking to particular characteristics of that language's
implementation. Even though its not talking just about say, C, C#, Java, or
anything -- its speaking from a certain POV of a certain classes of
languages.

In Python, setting up an exception -- the 'try' clause -- costs virtually
nothing. Its about equivalent to having a 'pass' statement in there. If you
do a test for every iteration of some activity, you're incurring a
non-negligable cost each time. If you're performing an action and "usually"
(to varying definitions of 'usually'), it's going to succeed-- then that
test will result in far more cost in time then using a try/except clause in
Python.

Because in the implementation of exceptions in Python, you only pay a more
expensive cost /if/ that exception is thrown and handled. If its very likely
that in a situation an exception would be thrown, then yes-- then you should
probably test first... if that exception-catch is so expensive as to be
important to your. In most cases, its not. In the vast majority of cases,
this is premature optimization and often adds additional weight to the test
as you have to protect against race conditions. (As an aside, in many cases
using exceptions actually helps you in a wider problem of preventing race
conditions. Its not a cure-all by any means, but it helps)

If someone specifies a file, the chances are-- the file is there. Its
cheaper for you to just try to open it then to test if its there first,
because if you do the test? Then the likely circumstance (the file exists)
is running code to test that case /every time/. Whereas if you just try to
open it, in the likely circumstance -- it works. In the exceptional
circumstance, it might cost more then if you had tested first... but that's
an exceptional circumstance and therefore more rare.

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