473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Reading Until EOF

Hi, everyone,

I'm a Python novice and would love some tips on how I should perform the
following task: I'd like to have my Python script read objects (using their
constructors) and terminate gracefully when an EOF is encountered. My first
attempt looked like this:

# This is enclosed in a 'try' block
file = open(...)
while 1:
# The Object constructor raises an exception for end-of-file
object = Object(file)
self.list = self.list + [object]

However, I'm not entirely comfortable having the Object constructor raise an
exception when the end-of-file is hit. Per my experience with C++, this
does not qualify as "exceptional behavior". Frankly, it is accepted. I'd
like to save the exceptions for true exceptions. My next guess:

# This is enclosed in a 'try' block
file = open(...)
while object = Object(file):
self.list = self.list + [object]

With this implementation, the file reading stops both with an exception
(raised by a parsing error, as an example) and when the Object constructor
returns something that makes the while conditional fail. However, therein
lies the rub: I don't know how to do this in Python.

Is there a way for me to make the second implementation work? Is there an
"empty" operator in Python that I could overload? Can I have a constructor
return 'null'?

Thanks!
Scott

--
Remove ".nospam" from the user ID in my e-mail to reply via e-mail.
Jul 18 '05 #1
7 10825
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 2003-10-21T18:27:57Z, "Scott Brady Drummonds" <sc**********************@intel.com> writes:
Per my experience with C++, this does not qualify as "exceptional
behavior". Frankly, it is accepted. I'd like to save the exceptions for
true exceptions. My next guess:


But it *is* exceptional behavior for the file read to fail. The normal
behavior is for the call to succeed, right?

Exceptions are handy for all sorts of things. One of my projects uses
something like:

class Success(Exception):
def __init__(self, added = 0, deleted = 0, modified = 0):
self.added = added
self.deleted = deleted
self.modified = modified

to hold return values from various calls. I use it like:

try:
SomeDatabaseQuery()
except Success, result:
print 'Just added %d new entries.' % result.added
except:
print 'The query failed.'

Don't limit yourself to a particular style simply because of the name.
- --
Kirk Strauser
The Strauser Group
Open. Solutions. Simple.
http://www.strausergroup.com/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/lYgS5sRg+Y0CpvERAm9uAKCQsxlz2U7OgR/WI3FLSJKmACO0mwCePe9H
d9mWtYpgEYOoDe7XvYjBm4E=
=2ZUb
-----END PGP SIGNATURE-----
Jul 18 '05 #2
> > Per my experience with C++, [end of file] does not qualify
as "exceptional behavior". Frankly, it is [expected]. I'd
like to save the exceptions for true exceptions.
But it *is* exceptional behavior for the file read to fail. The
normal behavior is for the call to succeed, right?


In C++, it would be considered bad style to use an exception to handle EOF.
The idea is that reaching EOF is something that you *expect* to happen, even
if only once. Similarly, the StopIteration exception would seem very odd to
a C++ programmer, since you expect to reach the end of an iteration
eventually.

You might use a C++ exception to handle something like a disk read
error--something that you do not expect to happen in the normal course of
events.

I realize the philosophy in Python is different--just trying to help clarify
why the OP is troubled by Python's more liberal use of exceptions. Anyone
coming from a C++ background is likely to have the same reaction at first.

-Mike
Jul 18 '05 #3
Scott Brady Drummonds wrote:
...
# This is enclosed in a 'try' block
file = open(...)
while object = Object(file):
self.list = self.list + [object]

With this implementation, the file reading stops both with an exception
(raised by a parsing error, as an example) and when the Object constructor
returns something that makes the while conditional fail. However, therein
lies the rub: I don't know how to do this in Python.

Is there a way for me to make the second implementation work? Is there an
It's a bad idea, as others have pointed out, but, sure -- as long as you
change this into something like:
while True:
object = Object(file)
if object is None: break
self.list.append(object)
or -- perhaps a bit more Pythonically, but otherwise equivalently:
def Objfromfile(): return Object(file)
self.list.extend([ object for object in iter(Objfromfile, None) ])
"empty" operator in Python that I could overload? Can I have a
constructor return 'null'?


I'm not sure what an "empty operator" would be. You can easily
make a function return None; use a factory function instead of the
bare ctor and you're all set (you'd do it that way in C++ too, and
there you'd have no alternative to that OR an exception).

You _CAN_ have Object.__new__ return what you want -- iff it
returns an instance of Object, that is then passed to Object.__init__.

Basically calling Object(xxx) is like calling a function that does:
def equivalent(xxx):
result = Object.__new__(Object, xxx)
if isinstance(result, Object): Object.__init__(result, xxx)
return result
but basically, for that to be used the way you want you need to do
almost all the work in Object.__new__ rather than in __init__ --
you can afford to let the latter be called only when you're fully
satisfied you'll be able to construct an Object instance just fine.

It IS a bad idea, but, if you insist, assuming that Object just
needs a field of exactly 4 bytes it reads from the file, period;
where you now have
def __init__(self, file):
self.field = file.read(4)
if len(self.field) != 4: raise EOFError
have no __init__ at all and instead a __new__:
def __new__(cls, file):
result = object.__new__(cls)
result.field = file.read(4)
if len(self.field) != 4: return None
return result

Using such exoteric techniques to avoid normal idiomatic usage
and surprise every Python-aware reader of your code IS a bad
idea. Be prepared to defend it craftily at the first code review!-)
Alex

Jul 18 '05 #4
Scott Brady Drummonds wrote:
However, I'm not entirely comfortable having the Object constructor
raise an exception when the end-of-file is hit. Per my experience
with C++, this does not qualify as "exceptional behavior". Frankly,
it is accepted. I'd like to save the exceptions for true exceptions.


That's a common, but to my mind very strange preference. If you handle an
exception, the exception is by definition expected and "normal". Exceptions
are just a way to exit from nested loops and function calls; there is
nothing "exceptional" about them.
--
Rainer Deyke - ra*****@eldwood.com - http://eldwood.com
Jul 18 '05 #5
Scott Brady Drummonds wrote:
I'm a Python novice and would love some tips on how I should perform the
following task: I'd like to have my Python script read objects (using
their
constructors) and terminate gracefully when an EOF is encountered. My
first attempt looked like this:

# This is enclosed in a 'try' block
file = open(...)
while 1:
# The Object constructor raises an exception for end-of-file
object = Object(file)
self.list = self.list + [object]

However, I'm not entirely comfortable having the Object constructor raise
an
exception when the end-of-file is hit. Per my experience with C++, this
does not qualify as "exceptional behavior". Frankly, it is accepted. I'd
like to save the exceptions for true exceptions. My next guess:

# This is enclosed in a 'try' block
file = open(...)
while object = Object(file):
self.list = self.list + [object]

With this implementation, the file reading stops both with an exception
(raised by a parsing error, as an example) and when the Object constructor
returns something that makes the while conditional fail. However, therein
lies the rub: I don't know how to do this in Python.

Is there a way for me to make the second implementation work? Is there an
"empty" operator in Python that I could overload? Can I have a
constructor return 'null'?


In Python constructors are precious, as there can only be one. I think it's
the wrong decision to let it construct an object from a file instance. I
would suggest a factory method instead:

class Object:
def __init__(self, word):
self.word = word
def __str__(self):
return self.word

def factory(instream):
""" construct an Object from the first word of each nonwhite line
"""
for line in instream:
if line.strip():
yield Object(line.split()[0].upper())

instream = file("fromfile.py")
try:
alist = [o for o in factory(instream)]
finally:
instream.close()

print "\n".join(map(str, alist))

The factory method could still explicitly raise an Exception for a more
complex file parsing algorithm (or simply return, if you stick with the
generator as shown above, but that is a StopIteration exception in
disguise), where you might want to separate the loop and the object
generation, but your client code is cleaner anyway.
Peter
Jul 18 '05 #6
On Tue, 21 Oct 2003 20:10:08 GMT, Kirk Strauser wrote:
But it *is* exceptional behavior for the file read to fail. The
normal behavior is for the call to succeed, right?


No, it is a normal result for the call to succeed, and it is a normal
result for end of file to be reached. The existence of one normal
result does not make all others exceptional.

--
\ "I think it would be a good idea." -- Mahatma Gandhi (when |
`\ asked what he thought of Western civilization) |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #7
Quoth Kirk Strauser <ki**@strauser.com>:
| At 2003-10-21T18:27:57Z, "Scott Brady Drummonds" <scott.b.drummonds.nospam@=
| intel.com> writes:
|
|> Per my experience with C++, this does not qualify as "exceptional
|> behavior". Frankly, it is accepted. I'd like to save the exceptions for
|> true exceptions. My next guess:
|
| But it *is* exceptional behavior for the file read to fail. The normal
| behavior is for the call to succeed, right?

Indeed, let me be the 7th or so person to chime in here. To be
precise, I think rather than `normal' and `succeed', I would just
say that the function has a meaning like `next line from file'.
Functions with such simple meanings can exist because of exceptions -
we decide what the function means, and we make it truly mean that by
raising an exception otherwise.

| Exceptions are handy for all sorts of things. One of my projects uses
| something like:
|
| class Success(Exception):
| def __init__(self, added = 0, deleted = 0, modified = 0):
| self.added = added
| self.deleted = deleted
| self.modified = modified
|
| to hold return values from various calls. I use it like:
|
| try:
| SomeDatabaseQuery()
| except Success, result:
| print 'Just added %d new entries.' % result.added
| except:
| print 'The query failed.'

Hm, it isn't so clear why this is a good idea.

Donn Cave, do**@drizzle.com
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: adpsimpson | last post by:
Hi, I have a file which I wish to read from C++. The file, created by another programme, contains both text and numbers, all as ascii (it's a .txt file). A sample of the file is shown below: <<...
1
by: Lisa C | last post by:
Hi all. I am trying to set up a C++ program which will continuously read stdin from another applications stdout. I will continue reading until I read a message that will indicate to stop. Does...
2
by: Chris P. | last post by:
I have a C# application that connects to Perl application on a UNIX server. I am able to connect and communicate both directions with the server, but there are several occasions when it appears...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
3
by: Adman | last post by:
Hi all. I've done some websearching, and haven't been able to find my question answered, so I thought I'd post. I apologize if this has already been answered. My question seems to be a...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
6
by: paultawk | last post by:
I have written a prog to read data from files but it only works under windows. Can anyone tell me how to read data under Linux? I would need somehting similar to the following windows code, but...
3
by: Willy Stevens | last post by:
Hello, In my application I have to read sometimes quite big chunk of binary data. I have a buffer which default size is 32000 bytes. But how could I read binary data that exceeds 32000 bytes?...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.