471,325 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

class fails if imported

Hi,

I wrote a class to read in a binary file:
see the code of readWLdata.py below

--------------8<------------------------------- test.py
from readWLdata import block

if __name__ == "__main__":
print "read WL data"

b = block('WL100/AAPL.wl')
for i in range(b.cnt):
print i
b.nextBlock()
b.printb()
--------------8<-------------------------------
wich results in this:
1
......
38
39
Traceback (most recent call last):
File "C:\Apps\arb\tradehist.py", line 20, in <module>
b.nextBlock()
File "C:\Apps\arb\readWLdata.py", line 24, in nextBlock
(self.h,) = struct.unpack('f', self.f.read(4))
File "C:\Apps\python2.5\lib\struct.py", line 87, in unpack
return o.unpack(s)
error: unpack requires a string argument of length 4

But if I hit F5 on this it works fine:

--------------8<-------------------------------readWLdata.py
import struct
from datetime import *
import time

class block():
def __init__(self, fn):
self.fn = fn
self.f = open(fn, 'rb', 4096)
(self.cnt,) = struct.unpack('I', self.f.read(4))
print self.cnt
self.d = 0.0
self.o = 0.0
self.l = 0.0
self.h = 0.0
self.c = 0.0
self.v = 0.0

def printb(self):
print "d:",self.d," o:",self.o," h:",self.h," l:",self.l,"
c:",self.c," v:",self.v

def nextBlock(self):
(self.d,) = struct.unpack('d', self.f.read(8))
(self.o,) = struct.unpack('f', self.f.read(4))
(self.h,) = struct.unpack('f', self.f.read(4))
(self.l,) = struct.unpack('f', self.f.read(4))
(self.c,) = struct.unpack('f', self.f.read(4))
(self.v,) = struct.unpack('f', self.f.read(4))
return 0

if __name__ == "__main__":
print "read WL data"

b = block('WL100/AAPL.wl')
for i in range(b.cnt):
print i
b.nextBlock()
b.printb()
--------------8<-------------------------------readWLdata.py

The test data file is containing stock prices and can be downloaded
here: http://63.99.108.115/WL100.zip

Any ideas?

Cheers
Andy

Oct 15 '07 #1
0 885

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by harold fellermann | last post: by
4 posts views Thread by Lonnie Princehouse | last post: by
3 posts views Thread by mirek | last post: by
23 posts views Thread by patang | last post: by
4 posts views Thread by Ritesh Raj Sarraf | last post: by
13 posts views Thread by John Dann | last post: by
reply views Thread by rosydwin | last post: by

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.