This is a mess...

Terry Reedy tjreedy at udel.edu
Thu Jul 16 16:55:08 EDT 2009


Nick wrote:
> this is the new oop version, its pretty messy currently, and i do
> understand it is a simple routine, but i'm using it as an exercise to
> learn oop python...

You neglected to specify Python version.
> 
> first the (current) traceback:
>  [:~/python]$  python oop_covariance.py b2ar_all_test b2ar_all_test
> <Eigen.Eigen_vect instance at 0x7fa26c404ab8>
> <Eigen.Eigen_vect instance at 0x7fa26c404ab8>
> Traceback (most recent call last):
>   File "oop_covariance.py", line 24, in <module>
>     cov = set1.covariance(set2, Eigen_vect.dot)
>   File "/home/nleioatts/python/Eigen.py", line 66, in covariance
>     print self.vectors[i][i]
> AttributeError: Eigen_vect instance has no attribute '__getitem__'

self.vectors[i] is apparently an Eigen_vect instance and you try to 
subscript it, but, as the message said, there is no __getitem__ method.

So add one.


> 
> and a quick explaination:  the file structures aare a 2d list of lists
> of numbers corresponding to eginvectors and a list of numbers
> corresponding to eigenvalues
> 
> #########################33##Here is the main body
> #!/usr/bin/env python
> import sys
> import Eigen
> from Eigen import *
> 
> if len(sys.argv) != 3:
>     print " "
>     print "The corrent usage is 'python covariance.py file1 file2'"
>     print "where the _U.asc and _s.asc will be appended when needed"
>     print " "
>     exit(1)
> file1 =  sys.argv[1]
> file2 =  sys.argv[2]
> set1 = Eigen_set(file1+"_U.asc", file1+"_s.asc")
> set2 = Eigen_set(file2+"_U.asc", file2+"_s.asc")
> cov = set1.covariance(set2, Eigen_vect.dot)
> print cov
> 
> 
> ###############and here are the classes:
> 
> #!/usr/bin/env python
> import sys
> import math
> from math import sqrt
> 
> class Eigen_vect:
>     def __init__(self, e_val, e_vect):
>         self.e_val  = e_val
>         self.e_vect = e_vect
>     def length(self):
>         return len(self.e_vect)

You should really call this __len__ so len(Eigen_vect()) will work.

I presume you need something like
def __getitem__(self, i): return self.e_vect[i]

Before continuing, read the Language Reference / Data Model / Special 
method names to understand how to write classes to integrate into syntax.

Terry Jan Reedy




More information about the Python-list mailing list