Python Forum
The derivate class dosn't behave like the base when i pass parameter to them
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The derivate class dosn't behave like the base when i pass parameter to them
#1
I have done a class hierarchy for product several plot stile , in which change almost any parameter, just inherit and define a set of parameter for change the default plot ..

but I have some problem with the derived class , basically from the abstract base class
derive a complete and usable class which is callable ! (in this way i can call directly subplot, or plot referring to the derived class)

The problem is that the usable base class that define the method __call__ works fine and accept and use in the right way the parameter passed by a generic script that instantiate it for create a plot, the derivatives classes don't.

here I'm report minimal working code

the base abstract class is:


class BasePlot(metaclass=ABCMeta):
        
       def __init__(self, title : str = ' ' , filename : str = ' '):
          self.title = title
          self.filename = filename   
          
       def schemes(self, style:str = 'nb'):
           if style == 'nb':
                return ['#8DA0CB', '#E58AC3', '#A6D853', '#FFD930', '#B2B2B2', '#5FC3A4', '#FC8D62', '#66C2A5']
           elif style == 'vega':
                return  ['#1F77B4', '#FF7F0E', '#2CA02C', '#D62728', '#9467BD', '#8C564B', '#E377C2', '#7F7F7F', '#BCBD22', '#17BECF']
    
       
from this base class derive the "Default plot" before called "the usable base class"


class DefaultPlot(BasePlot) :
       
        def __init__(self, figsize,*args,**kwargs):
            self.var = [*args]
            self.params = kwargs
                   
        def cycle(self,n : str):
                if n == '0':
                      return plt.cycler("color", self.schemes(self.parameters['scheme']) )      #colors)         
                elif n=='1':
                      return plt.cycler("color", self.schemes(self.parameters['scheme']) ) + plt.cycler("linestyle", self.linestyles(self.parameters['linestyle']))
    
        def setparams(self, kwargs):
                 self.parameters = kwargs
                 
                 if 'scheme' not in self.parameters.keys():
                    self.parameters['scheme'] = self.schemes('nb')
                
    
                 myparams = {
                   'axes.prop_cycle': 0,     
                                    
                 }
             
                 plt.rcParams.update(myparams)
        
        def __call__ (self,nrows,ncols,*args,**kwargs):
           
           self.setparams(kwargs)
    
           
                self.fig, self.axs = plt.subplots(nrows,ncols,figsize=(9.5,4.5))



then the derivate class for which the parameter passed as dictionary dosen't affect
the parameters (as expected ... here is the problem)

#-*- coding : utf-8 -*- 
    
      
    class Standard(DefaultPlot):
    
        def __init__(self , figsize , *args , **kwargs):
            self.args = [*args]
            self.params = kwargs
            super().__init__(figsize,*self.args, **self.params )
        
        def setparams(self, kwargs):
                 
                 self.parameters = kwargs
                 if 'scheme' in self.parameters.keys():
                    self.parameters['scheme'] = 'nb' 
                 if 'cycle' not in self.parameters.keys():
                    self.parameters['cycle']  = self.cycle('0')
                 
                 super().setparams(self.parameters)
and finally the script in which the class is called :



import defaultplot 
    import qualityplot
    
    def main():
        
        x = np.linspace(0,2*np.pi,100)
        y1 = np.sin(x)
        y2 = np.sin(2*x)
        y3 = np.sin(3*x)
    
        fig,axs = qualityplot.Standard(figsize=(9.5,4.5))(1,1,**{'scheme':'vega'})
        #fig,axs = defaultplot.DefaultPlot(figsize=(9.5,4.5))(1,1,**{'scheme':'vega'})
    
        axs.plot(x,y1,x,y2,x,y3)
        plt.show()
    
    if __name__ == '__main__':
        main()
In this script above the Standard class is instantiate and a dictionary containing parameters is passed , in particular the parameter for the scheme colors !
even thought the code works the result is not was I expected indeed the scheme 'vega' is not used in the plot ! while if you call in the same way the base class (the commented call) the code use the scheme 'vega' !! whyy ??? what I'm wronging ?
Reply
#2
REMOVED
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
I'm sorry but ..the parameters **{'scheme':'vega'} are given to the __call__ methods of the base class ! right? and why it works if you try to uncomment
fig,axs = defaultplot.DefaultPlot(figsize=(9.5,4.5))(1,1,**{'scheme':'vega'})
thanks for your support
Reply
#4
Well, easy to miss in a messy code. You pass
           self.setparams(kwargs)
where is double asterick?
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
Indeed :

class DefaultPlot(BasePlot) :
   
    def __init__(self, figsize,**kwargs):
        self.params = kwargs
        print(kwargs)
give me {}

while
def __call__ (self,nrows,ncols,**kwargs):
       print(kwargs)
       self.setparams(kwargs)
give me {'scheme': 'vega'}

(Aug-05-2018, 06:42 PM)volcano63 Wrote: Well, easy to miss in a messy code. You pass
           self.setparams(kwargs)
where is double asterick?

in this poiunt is not necessary the **

I fixed the things and I solved .. thanks for the hint !!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 849 Jul-27-2023, 12:40 AM
Last Post: tester_V
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,957 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  Calling a base class variable from an inherited class CompleteNewb 3 1,673 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  How to pass variables from one class to another hobbyist 18 10,643 Oct-01-2021, 05:54 PM
Last Post: deanhystad
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 4,857 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  Importing issues with base class for inheritance riccardoob 5 4,668 May-19-2021, 05:18 PM
Last Post: snippsat
  Behavior of Abstract Base Class dgrunwal 4 2,168 Oct-15-2020, 07:19 PM
Last Post: Gribouillis
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,548 Sep-07-2020, 08:02 AM
Last Post: perfringo
  read a parameter after updating it in another class MKS2020 3 2,252 Jul-25-2020, 08:27 AM
Last Post: MKS2020
  Pass by reference vs Pass by value leodavinci1990 1 2,192 Nov-20-2019, 02:05 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020