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 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


[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] shebang question

2006-11-25 Thread john maclean
>From what I can tell/remember, the first works in the *nix environment
if python is in your $PATH, the latter will find python "somehere" on
your system by looking at wher the executables should be.

On 26/11/06, shawn bright <[EMAIL PROTECTED]> wrote:
> Hey there all,
> what is the difference between
> #!/usr/bin/python
> and
> #!/usr/bin/env python
> ?
>
> thanks
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


-- 
John Maclean  - 07739 171 531
MSc (DIC)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor