eof

D

Dennis Lee Bieber

Actually, I think it was get(f) to read the next record
into the buffer. Read(f, x) was a higher-level procedure
equivalent to something like

x = f^;
get(f)
I should have flagged my stuff as "pseudocode" -- I only meant to
illustrate the sequence differences rather than actual Pascal. <G>

Ignoring (or trying to) the fact that my last Pascal was some
maintenance work six years ago, and I think all the I/O used VMS
calls... Prior to that was Alcor Pascal on a TRS-80 Model III/4
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

samwyse

There's a fair amount of overhead associated with providing
the ability to set arbitrary attributes on an object, which
is almost never wanted for built-in types, so it's not
provided by default.

You can easily get it if you want it by defining a Python
subclass of the type concerned.

Speaking of which, I've got a big file:

I'd like to get the md5 hash of the file:

I've also got this nifty standard module which will allow me, among
other things, to copy an arbitrary file:

I'd like to say copy the file to my object, but it doesn't quite work:

Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
shutil.copyfileobj(source, m)
File "C:\Python25\lib\shutil.py", line 24, in copyfileobj
fdst.write(buf)
AttributeError: '_hashlib.HASH' object has no attribute 'write'

No problem, I'll just add an attribute:
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
setattr(m, 'write', m.update)
AttributeError: '_hashlib.HASH' object has no attribute 'write'

Anyone have an example of how to efficiently do this? Thanks!
 
G

Grant Edwards

Help on built-in function xor in module operator:

xor(...)
xor(a, b) -- Same as a ^ b.

Which isn't the same thing:

------------------------------testit.py------------------------------
import operator

def xor(a,b):
return a and not b or b and not a

print xor(1,3), operator.xor(1,3)
------------------------------testit.py------------------------------

$ python testit.py
False 2

The user-defined xor is operates on "logical" boolean values.
The one in the operator module is a bitwise operator.
 
Z

ZeD

Grant said:
The user-defined xor is operates on "logical" boolean values.
The one in the operator module is a bitwise operator.

def xor(a, b):
return bool(a) ^ bool(b)

seems more explicit to me.
maybe, to make "more" explicit (too much, onestly...)

from operator import xor as bitwise_xor

def logical_xor(a, b):
return bitwise_xor(bool(a), bool(b))
 
B

Boris Borcic

ZeD said:
def xor(a, b):
return bool(a) ^ bool(b)

seems more explicit to me.
maybe, to make "more" explicit (too much, onestly...)

from operator import xor as bitwise_xor

def logical_xor(a, b):
return bitwise_xor(bool(a), bool(b))

I'd prefer bool(a)!=bool(b)

or even bool(a) is not bool(b)
 
J

jjnoakes

def read(self, size=None):
if size is None:
val = file.read(self)
self.eof = True
else:
val = file.read(self, size)
if len(val) < size:
self.eof = True
return val

Anyone using this routine must be careful not to fall in to this trap.

!f.eof implies f.read() will return data

For example, with a 5-byte file:

f = MyFile(5byte.bin)
data = f.read(5)
f.eof # ==> False
data = f.read(5) # ==> Empty String
f.eof # ==> True

In other words, don't rely on "f.eof" telling you that there is or is
not more data... on Unix systems, no matter what, you ALWAYS have to
read past EOF to detect it. Languages (like C with FILE*, or Ruby, or
anything else) that detects EOF either does it incorrectly, or (what
usually happens) reads 1 byte to see if they get an empty string back
from the OS.

Think about this: If you have to read 1 byte every time you want to
check for EOF, what happens if you are reading from a network stream?
That 1-byte read may take a while, waiting for data to be sent over
the wire. What a performance hit.

-JJ
 
J

jjnoakes

class open(file):
def __init__(self, name):
self.size = os.stat(name).st_size
file.__init__(self, name)
def eof(self):
return self.tell() == self.size

f = open('tmp.py')
print f.eof() # False
f.read()
print f.eof() # True

This is a bad idea because self.size is not guaranteed to be accurate.
For files in /proc, /sys, and for fifo's, unix domain sockets, etc,
you will get incorrect results.

Always write your algorithm to simply read until there is no more
data. Once that happens, you are at EOF.

EOF is a property of a file only after reading didn't return any data.
If you use EOF in the middle of reading a file, it will never be
accurate (or it will be slow).

-JJ
 

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

Forum statistics

Threads
473,794
Messages
2,569,641
Members
45,353
Latest member
RogerDoger

Latest Threads

Top