473,241 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 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 926

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jerry | last post by:
My "main" class is getting a bit long...Is it possble to split a class definition into several files and then import the pieces to get the whole definition? Jerry
4
by: harold fellermann | last post by:
Hi all, I have a problem pickling an extension class. As written in the Extending/Embedding Manual, I provided a function __reduce__ that returns the appropreate tuple. This seams to work fine,...
10
by: lkrubner | last post by:
I killed last night and a good chunk of today trying to figure out this one particular attempt to get a class and initialize it. My code is using a class method called getObject to include() a file...
4
by: Lonnie Princehouse | last post by:
So, it turns out that reload() fails if the module being reloaded isn't in sys.path. Maybe it could fall back to module.__file__ if the module isn't found in sys.path?? .... or reload could...
3
by: mirek | last post by:
Hi, I've got problem building managed class library to wrap unmanaged code. I created managed class library using standard patten: #include "../Unmanaged/Class1.h" //Class1 unmanaged ...
23
by: patang | last post by:
When I create my own class, to use the functions of that class I have to create an object of that class and then I can access the functions of that class, for example: dim obj1 as myclass...
4
by: Ritesh Raj Sarraf | last post by:
Hi, I have a class defined in a file called foo.py In bar.py I've imported foo.py In bar.py's main function, I instantiate the class as follows: log = foo.log(x, y, z) Now in main I'm...
13
by: John Dann | last post by:
A Python newbie, but some basic understanding of how classes, objects etc work in eg VB.Net. However, I'm struggling a little to translate this knowledge into the Python context. I'm trying to...
7
by: c4tech | last post by:
I am using VB.net with ASP.net 2.0 and am not using Visual Studio. I use my text editor. I have written a class and wrapped it in a namespace called "MyBulb_NS". I want to import and use that...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.