[SciPy-User] [scikits-timeseries] Some problems with inheritance (begginer's question, long)

J. David Lee johnl at cs.wisc.edu
Sat Oct 2 13:27:38 EDT 2010


Paweł,

I think the code is probably calling one of your class's functions with
keywords that it doesn't expect. You can pass arbitrary arguments up to
the superclass using *args and **kwargs. For example, with init:

def __init__(self, data, dates, *args, **kwargs):
    super(ChromaData, self).__init__(*args, **kwargs)

Now if a constructor to your object is passed an unknown keyword
argument, it will get passed up to the superclass. The same for any
other function you want to override. 

I think you should be careful not to change the signature of an
overridden function. In this case I think you will have to add any
additional arguments as keywords:

def someFn(self, *args, myNewArg = None, **kwargs):
    super(ChromaData, self).someFn(*args, **kwargs)
    # Additional code here.

I'm not sure there is another way to preserve the function's signature
while adding arguments. I'd be interested to know of other ways.

Now that I look at it, if you want your constructor to be callable by
code that works with the superclass as well, you'd have to be sure not
to change the constructor's signature as I did above by adding the two
positional arguments.

JD

On Sat, 2010-10-02 at 15:39 +0200, Paweł Rumian wrote:
> hello,
> 
> I'm using Python to do scientfic research - most of the time I'm
> analysing meteorogical data (mainly from gas chromatographs).
> I'm not an experienced programmer. I learned C and Pascal quite a long
> time ago, and then discovered Python, which quickly became my main
> tool to do day-to-day tasks, but I never went 'deeper'. I've also
> never touched object-oriented programming.
> 
> Last year I've written a bunch of programs using scikits-timeseries -
> it's certainly one of the most important modules I use. Everything
> works flawlessly, but the demands are increasing and currently I have
> to add some kind of GUI and web interface to my tools. So I recently
> decided to rewrite my code using object-oriented approach - which
> means that I needed to learn OOP, which I have never tried before.
> 
> After some reading I decided to extend TimeSeries class with a few
> methods that are specific to chomatographic data.
> The problem is that some of my methods work, but some not. I suspect
> that it is somehow connected with inheritance, but so far I cannot
> resolve it on my own...
> 
> I define the class like this:
> 
> class ChromaData(ts.TimeSeries):
>     u"""Represents chromatographic timeseries data, inherits from TimeSeries."""
> 
>     def __init__(self,data,dates):
>         super(ChromaData, self).__init__()
> 
> and initialize the instance with something like:
> 
> data_to_be_analyzed=
> ChromaData(retrieved_timeseries.data,retrieved_timeseries.dates)
> 
> where retrived_timeseries is a TimeSeries instance, initialised using tsfromtxt.
> 
> Then I define methods inside the ChromaData. Some of them (the simpler
> ones) work, but some don't. An example of such not working method is
> the one that I used to retrieve the lower envelope of data:
> 
>     def daily_bottom(self):
>         u"""Returns bottom envelope of input data, optimised for data
> with daily frequency."""
> 
>         daily_interpolated= ipl.interp_masked1d(self, kind='linear')
>         minima_of_daily_interpolated= mvf.mov_min(daily_interpolated,span=8)
>         minima_of_daily_minima= mvf.mov_min(minima_of_daily_interpolated,span=8)
> 
>         return minima_of_daily_minima
> 
> what I get is:
> 
> Traceback (most recent call last):
>   File "process.py", line 118, in <module>
>     dbottom= test_data.daily_bottom()
>   File "process.py", line 75, in daily_bottom
>     daily_interpolated= ipl.interp_masked1d(self, kind='linear')
>   File "/usr/lib/python2.6/site-packages/scikits/timeseries/lib/interpolate.py",
> line 125, in interp_masked1d
>     marr = marray(marr, copy=True)
>   File "/usr/lib/python2.6/site-packages/numpy/ma/core.py", line 5491, in array
>     fill_value=fill_value, ndmin=ndmin, shrink=shrink)
> TypeError: __init__() got an unexpected keyword argument 'fill_value'
> 
> Does it mean that I need to pass every possible argument of TimeSeries
> in __init__?
> Like def __init__(self,data,dates, fill_value=None, dtype=None,...........) ?
> Or am I doing something completely wrong?
> 
> Like I've written - I'm completely new to object-oriented programming,
> so excuse me if the problem is really trivial...
> 
> greetings,
> Paweł
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user





More information about the SciPy-User mailing list