优雅地检测EOF [英] Graceful detection of EOF

查看:97
本文介绍了优雅地检测EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何优雅地检测EOF?假设我有一个包含未知数量对象的pickle文件

,我怎么能读到(即

pickle.load())直到遇到EOF而没有生成EOF

例外?


感谢您的帮助。

MickeyBob

解决方案

写一个类似文件的对象,可以向前看。并提供一个标志

检查你的unpickling循环,并实现足够的文件

协议(阅读和阅读线,显然)取悦泡菜。

以下为我工作。


类PeekyFile:

def __init __(self,f):

self.f = f

self.peek =""


def eofnext(self):

如果self.peek:返回False

尝试:

self.peek = self.f.read(1)
除了EOFError之外的


返回True

返回非self.peek


如果n不是None:

n = n - len(self.peek)

result = self.peek + self.f.read(n)

else:

result = self.peek + self.f.read()

self.peek =""

返回结果


def readline(self):

result = self.peek + self.f.readline()

self.peek =""

返回结果


import StringIO,pickle

o = StringIO.StringIO()

for x in range(5):

pickle.dump(x,o)

i = PeekyFile(StringIO.StringIO(o.getvalue() ))<无线电通信/>
而1:

i.eofnext()

if i.eofnext():

break

print pickle.load(i)

print" at the end"


-----开始PGP SIGNATURE ---- -

版本:GnuPG v1.2.1(GNU / Linux)

iD8DBQFBZZsVJd01MZaTXX0RAl0FAJ9GCBIWmLaS + UbhCgZGR6 PlJ94c4QCePq / k

x9c7Hokjaj + RpSYryvEwCJ8 =

= sIw8

----- END PGP SIGNATURE -----


MickeyBob写道:

如何正常检测EOF?假设我有一个包含未知数量对象的pickle文件,我怎么能读到(即
pickle.load())直到遇到EOF而没有生成EOF
异常? / blockquote>


为什么没有优雅的异常?


#未解决的代码


def load_pickle_iter(infile):

而1:

试试:

收益率pickle.load(infile)

除了EOFError:

打破


for load in load_pickle_iter(open(" mydata.pickle"," rb")):

print obj

这与普通的Python习惯用语相吻合,

与在你跳跃之前看相比。


Andrew
da***@dalkescientific.com

< br>

Andrew Dalke写道:

MickeyBob写道:

如何检测EOF gracefull ÿ?假设我有一个包含未知数量对象的pickle文件,我怎么能读到(即
pickle.load())直到遇到EOF而没有生成EOF
异常? / blockquote>

为什么没有优雅的异常捕捉?

#未完成的代码

def load_pickle_iter(infile):
1:
尝试:
产品pickle.load(infile)
除了EOFError:


对于load_pickle_iter中的obj(打开(&mydata) .pickle"," rb")):
print obj

这与普通的Python习惯用语相吻合,
与在你跳跃之前看相比。

Andrew
da***@dalkescientific.com



所以,你所说的是,Python的方式与

在你跳跃之前相比,是落在其中,然后擦掉它?我们可以在Python的Zen中获得

吗? :-)


说真的,这很漂亮。我理解发电机,但是还没有b $ b习惯使用它们。那真是太美了,_s_

Zen。

Jeremy Jones


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

解决方案

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.StringIO()
for x in range(5):
pickle.dump(x, o)
i = PeekyFile(StringIO.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)

iD8DBQFBZZsVJd01MZaTXX0RAl0FAJ9GCBIWmLaS+UbhCgZGR6 PlJ94c4QCePq/k
x9c7Hokjaj+RpSYryvEwCJ8=
=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_iter(infile):
while 1:
try:
yield pickle.load(infile)
except EOFError:
break

for obj in load_pickle_iter(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***@dalkescientific.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_iter(infile):
while 1:
try:
yield pickle.load(infile)
except EOFError:
break

for obj in load_pickle_iter(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***@dalkescientific.com



So, what you''re saying is that the Python way, in contradistinction 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


这篇关于优雅地检测EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆