[Tutor] understanding pydoc try

2012-08-30 Thread John Maclean
What does the first line from `pydoc try` actually mean? This does not 
look like the syntax that one is supposed to use.


try_stmt  ::= try1_stmt | try2_stmt

I can write simple statements as shown below, but I want to actually 
understand what I am doing.




try:
import io
print(importing io)
except ImportError:
print(nothing to import)
foo = None
try:
import somefunctionthatdoesnotexist
print(importing ...)
except ImportError:
print(nothing to import)
foo = None

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] understanding pydoc try

2012-08-30 Thread Michael Janßen
On 30 August 2012 15:30, John Maclean jaye...@gmail.com wrote:

 What does the first line from `pydoc try` actually mean? This does not
 look like the syntax that one is supposed to use.

 try_stmt  ::= try1_stmt | try2_stmt


looks like part of the python language reference. It goes a little further
and explains what try1_stmt and try2_stmt actually suppose to mean:
http://docs.python.org/reference/compound_stmts.html#the-try-statement

try_stmt  ::=  try1_stmt | try2_stmt
try1_stmt ::=  try : suite
   (except [expression [(as | ,) target]] : suite)+
   [else : suite]
   [finally : suite]
try2_stmt ::=  try : suite
   finally : suite

Let me try to rephrase it: a try statement is either of
try-except-else-finally or of try-finally form.

This notation is used to formally describe language syntax:
http://docs.python.org/reference/introduction.html#notation

best,
Michael
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] understanding pydoc try

2012-08-30 Thread Dave Angel
On 08/30/2012 09:30 AM, John Maclean wrote:
 What does the first line from `pydoc try` actually mean? This does not
 look like the syntax that one is supposed to use.

 try_stmt  ::= try1_stmt | try2_stmt

You're looking at the first of three BNF statements.  BNF (Backus Naur
Form, or something like that) is a way of describing a grammar.  i'll
quote the whole thing here, and try to explain it.

The following is from Python 3.2's pydoc:

   try_stmt  ::= try1_stmt | try2_stmt
   try1_stmt ::= try : suite
 (except [expression [as target]] : suite)+
 [else : suite]
 [finally : suite]
   try2_stmt ::= try : suite
 finally : suite

The first statement says that a try_stmt is one or the other of two
formats.  This simply says there are two syntaxes you can use, depending
on what try features you want.

The second lists the (most common, i expect) syntax.  It has a literal
try token, followed by a literal colon token, followed by a suite of
statements (that's defined elsewhere, but would include simple
statements, if statements, and so on.  It wouldn't include def or class,
presumably).

Then there are one or more except clauses.  Note the trailing + which
means this element may be repeated, but must be present at least once.

Then there is an optional else clause.

Then an optional finally clause.

These must be present in the specific order stated above.  And you can't
(for example) have an else without an except, because except is one or
more times.

The second syntax does not include the except nor else clauses.

Is that clearer?

-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] understanding pydoc try

2012-08-30 Thread Steven D'Aprano

On 30/08/12 23:30, John Maclean wrote:

What does the first line from `pydoc try` actually mean? This does not look 
like the syntax that one is supposed to use.

try_stmt ::= try1_stmt | try2_stmt



That's a description of the Python grammar in some variation of
Backus-Naur Form. In English, it means:

A try statement is either a try1 statement or a try2 statement.

Presumably then there will be a definition of try1_stmt and try2_stmt,
also in BNF form, probably in terms of other statements, until
eventually the syntax of try statements is fully defined.

http://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form#Example

http://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form



I can write simple statements as shown below, but I want to actually understand 
what I am doing.



try:
import io
print(importing io)
except ImportError:
print(nothing to import)
foo = None


This first block attempts to import the io module, and then print
importing io. If the import process fails, an exception is
raised. But since the io module does exist and can be imported,
nothing happens and execution happily goes on to the part *after*
the except clause:



try:
import somefunctionthatdoesnotexist
print(importing ...)
except ImportError:
print(nothing to import)
foo = None



This time, since somefunction blah blah probably doesn't exist, the
import process does fail, and an exception is raised, interrupting
normal execution of the code. Instead of the next line running,
execution of your code halts and Python signals an ImportError.

However, then the except ImportError line takes over and catches
the exception. nothing to import is printed and foo is set to None.


Does that help?



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] understanding pydoc try

2012-08-30 Thread John Maclean

On 08/30/2012 03:05 PM, Dave Angel wrote:

On 08/30/2012 09:30 AM, John Maclean wrote:

What does the first line from `pydoc try` actually mean? This does not
look like the syntax that one is supposed to use.

try_stmt  ::= try1_stmt | try2_stmt


You're looking at the first of three BNF statements.  BNF (Backus Naur
Form, or something like that) is a way of describing a grammar.  i'll
quote the whole thing here, and try to explain it.

The following is from Python 3.2's pydoc:

try_stmt  ::= try1_stmt | try2_stmt
try1_stmt ::= try : suite
  (except [expression [as target]] : suite)+
  [else : suite]
  [finally : suite]
try2_stmt ::= try : suite
  finally : suite

The first statement says that a try_stmt is one or the other of two
formats.  This simply says there are two syntaxes you can use, depending
on what try features you want.

The second lists the (most common, i expect) syntax.  It has a literal
try token, followed by a literal colon token, followed by a suite of
statements (that's defined elsewhere, but would include simple
statements, if statements, and so on.  It wouldn't include def or class,
presumably).

Then there are one or more except clauses.  Note the trailing + which
means this element may be repeated, but must be present at least once.

Then there is an optional else clause.

Then an optional finally clause.

These must be present in the specific order stated above.  And you can't
(for example) have an else without an except, because except is one or
more times.

The second syntax does not include the except nor else clauses.

Is that clearer?



Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another 
set TLA that I don't need to know ;-)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] understanding pydoc try

2012-08-30 Thread Dave Angel
On 08/30/2012 10:43 AM, John Maclean wrote:
 On 08/30/2012 03:05 PM, Dave Angel wrote:

 snip

 Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another
 set TLA that I don't need to know ;-)


I learned BNF in about 1972.  I've used about 35 languages since (not
counting hobby ones).  It can clarify a new language better than many
paragraphs of description.  But I've found that it's seldom completely
rigorous.


-- 
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] understanding pydoc try

2012-08-30 Thread Steve Willoughby

On 30-Aug-12 08:22, Dave Angel wrote:

On 08/30/2012 10:43 AM, John Maclean wrote:

On 08/30/2012 03:05 PM, Dave Angel wrote:


snip


Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another
set TLA that I don't need to know ;-)



I learned BNF in about 1972.  I've used about 35 languages since (not
counting hobby ones).  It can clarify a new language better than many
paragraphs of description.  But I've found that it's seldom completely
rigorous.


True, usually because people aren't as careful writing it as they are 
real code that needs to be executed by something.  Maybe it would help 
to start by describing your grammar to YACC, getting it to work, and 
then expressing that back out as BNF (or just leaving it in YACC code).



--
Steve Willoughby / st...@alchemy.com
A ship in harbor is safe, but that is not what ships are built for.
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] understanding pydoc try

2012-08-30 Thread Dave Angel
On 08/30/2012 11:26 AM, Steve Willoughby wrote:
 On 30-Aug-12 08:22, Dave Angel wrote:
 On 08/30/2012 10:43 AM, John Maclean wrote:
 On 08/30/2012 03:05 PM, Dave Angel wrote:

 snip

 Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another
 set TLA that I don't need to know ;-)


 I learned BNF in about 1972.  I've used about 35 languages since (not
 counting hobby ones).  It can clarify a new language better than many
 paragraphs of description.  But I've found that it's seldom completely
 rigorous.

 True, usually because people aren't as careful writing it as they are
 real code that needs to be executed by something.  Maybe it would help
 to start by describing your grammar to YACC, getting it to work, and
 then expressing that back out as BNF (or just leaving it in YACC code).



There's another reason, that I usually assumed to be the case.  It
usually happens at a place where the grammar is particularly tricky, and
where the only valid thing to do in BNF is to list lots of cases (as the
one in this thread lists two).  So I assumed the BNF was more-or-less
deliberately dumbed down to make it more legible.

I like your explanation better, though.


-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] understanding pydoc try

2012-08-30 Thread Alan Gauld

On 30/08/12 15:43, John Maclean wrote:


Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another
set TLA that I don't need to know ;-)


Actually, BNF is one of those useful skills for any programmer because 
almost every language is 'formally' described using it - at least since 
the days of Algol, for which it was invented.


A simplified version of it is also used to define most command line 
tools and their arguments so its definitely worth learning, at least the 
basics. It can save a lot of typing when you want to precisely specify 
the allowed grammar in a problem.


There are tools which can translate BNF like text into something close 
to code, which is useful if you ever have to define your own programming 
language. Admittedly not something most programmers ever need to do, but 
it does happen occasionally that its the easiest way to solve a problem. 
(The so-called mini-language design pattern)



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] understanding pydoc try

2012-08-30 Thread John Maclean

On 08/30/2012 05:15 PM, Alan Gauld wrote:

On 30/08/12 15:43, John Maclean wrote:


Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another
set TLA that I don't need to know ;-)


Actually, BNF is one of those useful skills for any programmer because 
almost every language is 'formally' described using it - at least 
since the days of Algol, for which it was invented.


A simplified version of it is also used to define most command line 
tools and their arguments so its definitely worth learning, at least 
the basics. It can save a lot of typing when you want to precisely 
specify the allowed grammar in a problem.


There are tools which can translate BNF like text into something close 
to code, which is useful if you ever have to define your own 
programming language. Admittedly not something most programmers ever 
need to do, but it does happen occasionally that its the easiest way 
to solve a problem. (The so-called mini-language design pattern)





My main issue is that I am a sysadmin and not a programmer. I am aware 
of pydoc but not of BNF. So I was a bit taken aback when I saw the BNF 
syntax. It was obvious to me that syntax of the try statements were not 
python syntax but had no clue how to parse it. BTW - where in pydoc is 
it mentioned, (or anywhere else for that matter), to refer to BNF?





___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] understanding pydoc try

2012-08-30 Thread Alan Gauld

On 30/08/12 17:21, John Maclean wrote:


My main issue is that I am a sysadmin and not a programmer. I am aware
of pydoc but not of BNF. So I was a bit taken aback when I saw the BNF
syntax. It was obvious to me that syntax of the try statements were not
python syntax but had no clue how to parse it. BTW - where in pydoc is
it mentioned, (or anywhere else for that matter), to refer to BNF?


If you are writing (or reading!) code you are a programmer! :-)

Michael already gave a link to the notation page on the web site which 
does explicitly mention BNF but, to be honest it would not be surprising 
if it didn't. It would be like specifically saying that a web page was 
written in HTML, nowadays its often just assumed that anyone creating 
web pages knows about HTML... Similarly languages are usually specified 
in some approximation of BNF.


The link was:
http://docs.python.org/reference/introduction.html#notation


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor