How does one detect the EOF gracefully? Assuming I have a pickle file
containing an unknown number of objects, how can I read (i.e.,
pickle.load()) until the EOF is encountered without generating an EOF
exception?
Thanks for any assistance.
MickeyBob 20 14935
Write a file-like object that can "look ahead" and provide a flag to
check in your unpickling loop, and which implements enough of the file
protocol ("read" and "readline", apparently) to please pickle. The
following worked for me.
class PeekyFile:
def __init__(self, f):
self.f = f
self.peek = ""
def eofnext(self):
if self.peek: return False
try:
self.peek = self.f.read(1)
except EOFError:
return True
return not self.peek
def read(self, n=None):
if n is not None:
n = n - len(self.peek)
result = self.peek + self.f.read(n)
else:
result = self.peek + self.f.read()
self.peek = ""
return result
def readline(self):
result = self.peek + self.f.readline ()
self.peek = ""
return result
import StringIO, pickle
o = StringIO.String IO()
for x in range(5):
pickle.dump(x, o)
i = PeekyFile(Strin gIO.StringIO(o. getvalue()))
while 1:
i.eofnext()
if i.eofnext():
break
print pickle.load(i)
print "at the end"
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
iD8DBQFBZZsVJd0 1MZaTXX0RAl0FAJ 9GCBIWmLaS+UbhC gZGR6PlJ94c4QCe Pq/k
x9c7Hokjaj+RpSY ryvEwCJ8=
=sIw8
-----END PGP SIGNATURE-----
MickeyBob wrote: How does one detect the EOF gracefully? Assuming I have a pickle file containing an unknown number of objects, how can I read (i.e., pickle.load()) until the EOF is encountered without generating an EOF exception?
Why isn't catching the exception graceful?
# UNTESTED CODE
def load_pickle_ite r(infile):
while 1:
try:
yield pickle.load(inf ile)
except EOFError:
break
for obj in load_pickle_ite r(open("mydata. pickle", "rb")):
print obj
This is well in line with the normal Python idiom,
as compared to "look before you leap".
Andrew da***@dalkescie ntific.com
Andrew Dalke wrote: MickeyBob wrote:
How does one detect the EOF gracefully? Assuming I have a pickle file containing an unknown number of objects, how can I read (i.e., pickle.load()) until the EOF is encountered without generating an EOF exception?
Why isn't catching the exception graceful?
# UNTESTED CODE
def load_pickle_ite r(infile): while 1: try: yield pickle.load(inf ile) except EOFError: break
for obj in load_pickle_ite r(open("mydata. pickle", "rb")): print obj
This is well in line with the normal Python idiom, as compared to "look before you leap".
Andrew da***@dalkescie ntific.com
So, what you're saying is that the Python way, in contradistincti on to
"look before you leap", is "land in it, then wipe it off?" Can we get
that in the Zen of Python? :-)
Seriously, this is beautiful. I understand generators, but haven't
become accustomed to using them yet. That is just beautiful, which _is_
Zen.
Jeremy Jones
A file is too large to fit into memory.
The first line must receive a special treatment, because
it contains information about how to handle the rest of the file.
Of course it is not difficult to test if you are reading the first line
or another one, but it hurts my feelings to do a test which by definition
succeeds at the first record, and never afterwards.
Any suggestions ?
egbert
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
=============== =============== =============== =============== ============
Egbert Bouwman wrote: A file is too large to fit into memory. The first line must receive a special treatment, because it containsÂ*Â*inf ormationÂ*about Â*howÂ*toÂ*hand leÂ*theÂ*restÂ* ofÂ*theÂ*file.
Of course it is not difficult to test if you are reading the first line or another one, but it hurts my feelings to do a test which by definition succeeds at the first record, and never afterwards. lines = iter("abc") for first in lines:
.... print first
.... break
....
a for line in lines:
.... print line
....
b
c
Unless it hurts your feelings to unconditionally break out of a for-loop,
that is.
Peter
Peter Otten wrote: >>> lines = iter("abc") for first in lines: ... print first ... break ... a for line in lines: ... print line ... b c
Unless it hurts your feelings to unconditionally break out of a for-loop, that is.
How about: lines = iter("abc") first = lines.next() print first
a for line in lines:
.... print line
....
b
c
Would hurt less feeling I presume.
Gerrit.
--
Weather in Twenthe, Netherlands 08/10 11:25:
11.0°C Few clouds mostly cloudy wind 0.9 m/s None (57 m above NAP)
--
In the councils of government, we must guard against the acquisition of
unwarranted influence, whether sought or unsought, by the
military-industrial complex. The potential for the disastrous rise of
misplaced power exists and will persist.
-Dwight David Eisenhower, January 17, 1961
Jeremy Jones <za******@bells outh.net> wrote:
... This is well in line with the normal Python idiom, as compared to "look before you leap".
Andrew da***@dalkescie ntific.com
So, what you're saying is that the Python way, in contradistincti on to "look before you leap", is "land in it, then wipe it off?" Can we get that in the Zen of Python? :-)
The "normal Python idiom" is often called, in honor and memory of
Admiral Grace Murray-Hopper (arguably the most significant woman in the
history of programming languages to this time), "it's Easier to Ask
Forgiveness than Permission" (EAFP, vs the LBYL alternative). This
motto has been attributed to many, but Ms Hopper was undoubtedly the
first one to use it reportedly and in our field.
In the general case, trying to ascertain that an operation will succeed
before attempting the operation has many problems. Often you end up
repeating the same steps between the ascertaining and the actual usage,
which offends the "Once and Only Once" principle as well as slowing
things down. Sometimes you cannot ensure that the ascertaining and the
operating pertain to exactly the same thing -- the world can have
changed in-between, or the code might present subtle differences between
the two cases.
In contrast, if a failed attempt can be guaranteed to not alter
persistent state and only result in an easily catchable exception, EAFP
can better deliver on its name. In terms of your analogy, there's
nothing to "wipe off" -- if the leap "misfires", no damage is done.
Alex
Egbert Bouwman <eg*********@hc cnet.nl> wrote: A file is too large to fit into memory. The first line must receive a special treatment, because it contains information about how to handle the rest of the file.
Of course it is not difficult to test if you are reading the first line or another one, but it hurts my feelings to do a test which by definition succeeds at the first record, and never afterwards.
option 1, the one I would use:
thefile = open('somehugef ile.txt')
first_line = thefile.next()
deal_with_first (first_line)
for line in thefile:
deal_with_other (line)
this requires Python 2.3 or better, so that thefile IS-AN iterator; in
2.2, get an iterator with foo=iter(thefil e) and use .next and for on
that (better still, upgrade!).
option 2, not unreasonable (not repeating the open & calls...):
first_line = thefile.readlin e()
for line in thefile: ...
option 3, a bit cutesy:
for first_line in thefile: break
for line in thefile: ...
(again, in 2.2 you'll need some foo=iter(thefil e)).
I'm sure there are others, but 3 is at least 2 too many already,
so...;-)
Alex
Gerrit wrote: first = lines.next()
[as opposed to 'for first in lines: break'] Would hurt less feeling I presume.
iter("").next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration
I feel a little uneasy with that ...unless I'm sure I want to deal with the
StopIteration elsewhere.
Looking at it from another angle, the initial for-loop ist just a peculiar
way to deal with an empty iterable. So the best (i. e. clear, robust and
general) approach is probably
items = iter(...)
try:
first = items.next()
except StopIteration:
# deal with empty iterator, e. g.:
raise ValueError("nee d at least one item")
else:
# process remaining data
part of which is indeed your suggestion.
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jacob H |
last post by:
Hello all,
I'm nearing the completion of my first graphical console game and my
thoughts have turned to the subject of gracefully handling runtime
errors. During development I like to not handle exceptions, so that
program execution will halt and I can immediately read the traceback
to see what's up. Once the bugs are more or less worked out, I have a
system ready wherein any exceptions are caught and an attempt is made
to write the...
|
by: Fotios |
last post by:
Hi guys,
I have put together a flexible client-side user agent detector (written in
js). I thought that some of you may find it useful. Code is here:
http://fotios.cc/software/ua_detect.htm
The detector requires javascript 1.0 to work. This translates to netscape
2.0 and IE 3.0 (although maybe IE 2.0 also works with it)
|
by: Gustav Medler |
last post by:
Hello,
there is a known problem with Opera and the execution of content shown
in <NOSCRIPT> tag.
Everythings works fine, if there is only one simple script like:
http://www.dr-wald.de/test/Opera-NOSCRIPT.html
Switching off Javascript will show the alternative content. Javascript
enabled will only show the JS-content, _not_ the <NOSCRIPT> content.
|
by: R. Smits |
last post by:
I've have got this script, the only thing I want to be changed is the first
part. It has to detect IE version 6 instead of just "Microsoft Internet
Explorer". Can somebody help me out?
I tried "Microsoft Internet Explorer 6" but that doesn't work.
<SCRIPT LANGUAGE="Javascript">
<!--
bName = navigator.appName;
if (bName =="Microsoft Internet Explorer") {
document.write('<link media="screen" rel="STYLESHEET" type="text/css"
|
by: mosaic |
last post by:
Hi, all
I really interested in how to check the memory leak of a program.
Your smart guys, do you have excellent ideas that could share with me?
Thank you.
The following is my idea:
In C programming language, there's a "malloc", there must a "free",
my solution of the detection of leak is, find the corresponding "free"
of "malloc". This the first condition.
| |
by: csgonan |
last post by:
I have a new 64 bit apache 2.2.4 server on Solaris 10 with openssl 0.9.8e. When I DO NOT have the ssl.conf file included and I "apachectl graceful" to apache, all my processes that are gracefully shutdown are shown by a dash (-). When I have the Include line for the ssl.conf file uncommented, the number of Gs never changes, like it is locked in the graceful shutdown.
I have prefork compiled in but not worker or event if that makes a...
|
by: darrenhello |
last post by:
hi there,
I am doing my last year's project and I have a 'little' problem. I have to do an edge detection filter. for now, some normal edge detection filters that I used worked fine but there a problem. I need an edge detection filter in which I can offset the edge according to a variable that the user gives. The problem is that I got no idea of how to make an edge detection filter with an offset.
anyone can help please?
thank you
|
by: origami.takarana |
last post by:
Intrusion Detection Strategies
-----------------------------------
Until now, we’ve primarily discussed monitoring in how it relates to
intrusion detection, but there’s more to an overall intrusion
detection installation than monitoring alone. Monitoring can help you
spot problems in your network, as well as identify performance
problems, but watching every second of traffic that passes through
your network, manually searching for...
|
by: Conrad Lender |
last post by:
In a recent thread in this group, I said that in some cases object
detection and feature tests weren't sufficient in the development of
cross-browser applications, and that there were situations where you
could improve the application by detecting the browser vendor/version.
Some of the posters here disagreed. Since then, I've had to deal with a
few of these cases; some of them could be rewritten to use object
detection, and some couldn't....
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |