473,396 Members | 1,927 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,396 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 931

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.