Defining getter and setter methods

In our previous section, we needed to know the value of a button in a given row and column of the button matrix for a given pattern. If the value was True, we colored the button green. If the value was False, we colored it in an alternative color.

We can get the value of the button by calling this line of code:

self.all_patterns[self.current_pattern.get()]['is_button_clicked_list'][row][col]

Notice how this line has four sets of square brackets, []. Since this nested super-scripting business can soon get ugly, we encapsulated this logic in a method named get_button_value(row, col). Now, whenever we need to get a button's value, we can simply call this method with the right parameters.

Now our code will not be littered with those ugly nested superscripts. Whenever we want to get the value of a button, we can call the get_button_value(row, col) method, which has a nice indicative name for the work it does. Isn't this much more readable and comprehensible than its rather ugly counterpart?

One thing is for sure: all logic that we build from now onward will heavily rely on data we get from, or set to, our data structure. Given that we will need all this data all the time in our program, let's write its getter and setter methods in advance. This will certainly make our lives a lot easier.

The goal for this part of the iteration is simpleā€”to define getter and setter methods for all the data that we have decided to store in our data structure.

The code is as follows (see code 3.04.py):

def get_current_pattern_dict(self):
return self.all_patterns[self.current_pattern_index]

def get_bpu(self):
return self.get_current_pattern_dict()['bpu']

def set_bpu(self):
self.get_current_pattern_dict()['bpu'] = int(self.bpu_widget.get())

def get_number_of_units(self):
return self.get_current_pattern_dict()['number_of_units']

def set_number_of_units(self):
self.get_current_pattern_dict()['number_of_units']
= int(self.number_of_units_widget.get())

def get_list_of_drum_files(self):
return self.get_current_pattern_dict()['list_of_drum_files']

def get_drum_file_path(self, drum_index):
return self.get_list_of_drum_files()[drum_index]

def set_drum_file_path(self, drum_index, file_path):
self.get_list_of_drum_files()[drum_index] = file_path

def get_is_button_clicked_list(self):
return self.get_current_pattern_dict()['is_button_clicked_list']

def set_is_button_clicked_list(self, num_of_rows, num_of_columns):
self.get_current_pattern_dict()['is_button_clicked_list']
= [[False] * num_of_columns for x in range(num_of_rows)]

That is all there is to coding the getter and setter methods. The code should be self-explanatory if you have understood the underlying data structure, as all that we do here is either get a value or set a value for various items in the data structure.

With these methods now handy, let's complete coding the functionality of widgets we had earlier left uncoded.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.76.138