class fails if imported

W

witichis

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
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top