Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > class fails if imported

Reply
Thread Tools

class fails if imported

 
 
witichis@gmail.com
Guest
Posts: n/a
 
      10-15-2007
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()
(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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why do directly imported variables behave differently than thoseattached to imported module? Dun Peal Python 10 05-03-2011 10:11 PM
del an imported Class at EOF... why? Ryan Python 12 10-08-2009 08:20 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
"Variable ... is not imported..." using an imported variable from a module Volker Nicolai Perl Misc 9 07-04-2005 08:34 AM
WSDL file produces useless class when imported with WSDL.exe RH ASP .Net Web Services 1 05-27-2004 09:40 PM



Advertisments