0

I am learning Python language for about a year now, slowly venturing into the "magical python commands". I have to get my hands dirty to grasp the concept of __self.setattr__, and this is my first time using it. Please let me know if I am using it "correctly", and in what way, I can improve on it. This is because I do not have any peers to cross-check what I write and therefore I think this channel might be a good one to seek advice.

The following reference is used: Link here

Note that confirmed, recovered, deaths are DataFrames.

class PlotGo:
    __acceptable_keys_list = ['crd','crd_colors','crd_layout_params', 'Date', 'bar_params']
    def __init__(self, **kwargs):  
        self.fig = go.Figure()
        self.markers = {}

        for key in kwargs.keys():
            if key in self.__acceptable_keys_list:
                self.__setattr__(key, kwargs.get(key, None))

    def _go_bar(self, *args,**kwargs):
        return go.Bar(*args, **kwargs)



    def update_graph_crd(self):
        for crd_name, df in self.crd.items():            
            self.fig.add_trace(self._go_bar(x=df[self.__acceptable_keys_list
                                                  [self.__acceptable_keys_list.index('Date')]],                          
                                       y=df[crd_name],
                                       name=crd_name, 
                                       marker_color=self.crd_colors[crd_name]))
        self.fig.update_layout(**self.crd_layout_params)
        self.fig.show()
            
    def reset_fig():
        # reset figure to clean state again.
        self.fig = go.Figure()
    

crd_dict = {'crd': {'Confirmed': confirmed, 'Recovered': recovered, 'Deaths': deaths},
            'crd_colors': {'Confirmed': 'blue', 'Recovered': 'green', 'Deaths': 'red'},
             'crd_layout_params': { 'title':'Worldwide Corona Virus Cases - Confirmed, Deaths, Recovered (Bar Chart)',
                                    'xaxis_tickfont_size':12,
                                    'yaxis':dict(title='Number of Cases',
                                                 titlefont_size=16,
                                                 tickfont_size=12,)}}
a = PlotGo(**crd_dict)
a.update_graph_crd()
    
ilovewt
  • 911
  • 2
  • 10
  • 18
  • You may be better off storing the various values in a dict. Thus you simply get something like `self.extra = kwargs`. Because I don't see any use for it in your class as an attribute. – 9769953 Jan 31 '21 at 08:21
  • 1
    Note that elsewhere, you call `self.fig.update_layout(**self.crd_layout_params)`, but `self.crd_layout_params` may not exist! (Since it's a kwargs, thus optional, in `__init__`.) Thus, you should check for its existence, which may be easier with a dict (`if 'crd_layout_params in self.extra`, or use `try` - `except KeyError`), and then `self.fig.update_layout(**self.dict['crd_layout_params'])`. – 9769953 Jan 31 '21 at 08:24
  • @00 Hi thanks for the reply, would you mind demonstrating it out for me? I am a bit lost on what you meant. – ilovewt Jan 31 '21 at 08:29

0 Answers0