class fails if imported

Discussion in 'Python' started by witichis@gmail.com, Oct 15, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  2. Ryan
    Replies:
    12
    Views:
    481
    Terry Reedy
    Oct 8, 2009
  3. Dun Peal
    Replies:
    10
    Views:
    455
    Chris Rebert
    May 3, 2011
  4. RH
    Replies:
    1
    Views:
    260
    Dino Chiesa [Microsoft]
    May 27, 2004
  5. Volker Nicolai
    Replies:
    9
    Views:
    931
    Fabian Pilkowski
    Jul 4, 2005
Loading...

Share This Page