which one of these is better?

J

John Salerno

def create_sql_script(self):
try:
with open('labtables.sql') as sql_script:
return sql_script.read()
except IOError:
wx.MessageBox('Could not locate the file "labtables.sql"',
'File Not Found')


OR


def create_sql_script(self):
try:
f = open('labtables.sql')
sql_script = f.read()
except IOError:
wx.MessageBox('Could not locate the file "labtables.sql"',
'File Not Found')
finally:
f.close()



Do they both do the same thing?
 
J

John Salerno

John said:
def create_sql_script(self):
try:
f = open('labtables.sql')
sql_script = f.read()
except IOError:
wx.MessageBox('Could not locate the file "labtables.sql"',
'File Not Found')
finally:
f.close()

Hmm, looks like this doesn't work anyway if open() doesn't work, because
then f.close() raises an UnboundLocalError for obvious reasons.
 
S

Sybren Stuvel

John Salerno enlightened us with:
Hmm, looks like this doesn't work anyway if open() doesn't work,
because then f.close() raises an UnboundLocalError for obvious
reasons.

Neither work 100% correct, actually. If the file can be located and
opened, but not read, the message

Could not locate the file "labtables.sql" File Not Found

is plain wrong.

Sybren
 
M

Michael B. Trausch

John said:
def create_sql_script(self):
try:
with open('labtables.sql') as sql_script:
return sql_script.read()
except IOError:
wx.MessageBox('Could not locate the file "labtables.sql"',
'File Not Found')

I can't comment on this, because it won't work in my implemention of
Python, I don't think.
OR


def create_sql_script(self):
try:
f = open('labtables.sql')
sql_script = f.read()
except IOError:
wx.MessageBox('Could not locate the file "labtables.sql"',
'File Not Found')
finally:
f.close()

Not really über-qualified to say anything, but I think that the
following would be better:

try:
f = open('file.sql')
script = f.read()
f.close()
except IOError:
wx.MessageBox('Message', 'Message Title')
Do they both do the same thing?

Not sure about the with-- I just went to read the PEP on it, and it
confused me greatly. :) So, I don't know.

-- Mike
 
J

John Salerno

Michael said:
Not really über-qualified to say anything, but I think that the
following would be better:

try:
f = open('file.sql')
script = f.read()
f.close()
except IOError:
wx.MessageBox('Message', 'Message Title')

I was thinking of something like this actually.
Not sure about the with-- I just went to read the PEP on it, and it
confused me greatly. :) So, I don't know.

Ditto. :)
 
F

Fredrik Lundh

John said:

No need to be confused; the "with" statement is actually very simple.
Consider this piece of code:

set things up
try:
do something
finally:
tear things down

If you do this a lot, it would quite convenient if you could put the
"set things up" and "tear things down" code in a library function, so
you could reuse it. You can of course do something like

def controlled_execution(callback):
set things up
try:
callback(thing)
finally:
tear things down

def my_function(thing):
do something

controlled_execution(my_function)

but that's a bit verbose, especially if you need to modify local
variables. Another approach is to use a one-shot generator, and
abuse the for-in statement:

def controlled_execution():
set things up
try:
yield thing
finally:
tear things down

for thing in controlled_execution():
do something with thing

but this looks a bit weird, and doesn't even work in 2.4 and earlier.
So after contemplating a number of alternatives, GvR finally came up
with a generalization of the latter, using an object instead of a
generator to control the behaviour of an external piece of code:

class controlled_execution:
def __enter__(self):
set things up
return thing
def __exit__(self, type, value, traceback):
tear things down

with controlled_execution() as variable:
some code

When the "with" statement is executed, Python evaluates the expression,
calls the __enter__ method on the resulting value (which is called a
"context guard"), and assigns whatever __enter__ returns to the given
variable. Python will then execute the code body, and *no matter what
happens in that code*, call the guard object's __exit__ method.

For extra bonus, the __exit__ method can look at the exception, if any,
and suppress or modify it, if necessary. For example, the following
__exit__ method swallows any TypeError, but lets all other exceptions
through:

def __exit__(self, type, value, traceback):
return isinstance(value, TypeError)

In Python 2.5, the file object has been equipped with __enter__ and
__exit__ methods; the former simply returns the file object itself, and
the latter closes the file:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file

This wasn't very difficult, was it?

</F>
 
J

John Salerno

Fredrik said:
This wasn't very difficult, was it?

Well when you put it that way, no! :)

But one thing that caught me up was what happens if the initial
expression raises an exception? For example:

with open('file.txt'):
#do stuff

If the file can't be opened for whatever reason, does __exit__ get
called, or can it not get that far if the expression doesn't evaluate?

I guess my thinking was this: before the with statement came along, the
idiom for opening files was something like this:

try:
open('file.txt')
#do stuff
except IOError:
#more stuff
except OtherError:
#yet more

and so forth (and all this was wrapped in a try/finally blocked to close
the file, I think). But now that we can use the 'with' statement, it
seems like the initial opening of the file (previously "checked" in the
try block) is now just floating around and still requires a try block
wrapped around it in case something goes wrong.
 
R

robert

Michael said:
Not really über-qualified to say anything, but I think that the
following would be better:

try:
f = open('file.sql')
script = f.read()
f.close()
except IOError:
wx.MessageBox('Message', 'Message Title')


Not sure about the with-- I just went to read the PEP on it, and it
confused me greatly. :) So, I don't know.

when .read() fails the file will not be close here.

to replace the 'with' solution you#d need:


try:
f=open(path)
try:
script=f.read()
finally:
f.close()
except EnvironmentError,v:
wx.MessageBox(v, 'Message Title')




-robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top