def list_by_type(self): """ Displays only the apartments having a specific expense type """ self._validate_parsed_command(["expense_type"]) expense_type = self._parsed_command["expense_type"] filtered_bloc_dict = {} for apartment_id in self._bloc_dict.keys(): if self._bloc_dict[apartment_id].expenses[expense_type] != 0: filtered_bloc_dict[apartment_id] = self._bloc_dict[apartment_id] if filtered_bloc_dict: UI.set_message(UI.get_bloc_table(filtered_bloc_dict)) else: UI.set_message("There are no apartments with " + expense_type)
def list_greater_than(self): """ Displays only the apartments with an overall expense greater than the given amount """ self._validate_parsed_command(["greater"]) greater_than = float(self._parsed_command["greater"]) filtered_bloc_dict = {} for apartment_id in self._bloc_dict.keys(): if self._bloc_dict[apartment_id].get_total_expenses() > greater_than: filtered_bloc_dict[apartment_id] = self._bloc_dict[apartment_id] # check if empty if filtered_bloc_dict: UI.set_message(UI.get_bloc_table(filtered_bloc_dict)) else: UI.set_message("There are no apartments with overall expenses greater than " + str(greater_than))
def list_less_than(self): """ Displays only the apartments with an overall expense less than the given amount """ self._validate_parsed_command(["less"]) less_than = float(self._parsed_command["less"]) upper_id_limit = self._parsed_command["upper_id_limit"] filtered_bloc_dict = {} for apartment_id in range(1, int(upper_id_limit) + 1): apartment_id = str(apartment_id) if self.is_apartment(apartment_id): if self._bloc_dict[apartment_id].get_total_expenses() < less_than: filtered_bloc_dict[apartment_id] = self._bloc_dict[apartment_id] # check if empty if filtered_bloc_dict: UI.set_message(UI.get_bloc_table(filtered_bloc_dict)) else: UI.set_message("There are no apartments with overall expenses less than " + str(less_than))
def sort(self): """ Displays the list of apartments sorted """ self._validate_parsed_command(["type", "reverse"]) reverse_sort = self._parsed_command["reverse"] if reverse_sort is True: header_message = " sorted descending" else: header_message = " sorted ascending" # simple sort by total expense if self._parsed_command["type"] is None: # we create a list of tuples consisting of (id, apartment_obj, total_expense) list_of_tuples = [ (apartment_id, self._bloc_dict[apartment_id], self._bloc_dict[apartment_id].get_total_expenses()) for apartment_id in self._bloc_dict] # set message header_message += " by total expenses" else: # sort by type expense_type = self._parsed_command["type"] # we create a list of tuples consisting of (id, apartment_obj, expense_type_amount) list_of_tuples = [ (apartment_id, self._bloc_dict[apartment_id], self._bloc_dict[apartment_id].expenses[expense_type]) for apartment_id in self._bloc_dict] # set message header_message += " by expense '" + expense_type + "'" # print("raw_list: ", list_of_tuples) # use the last key of the tuple for the value sorted_list_of_tuples = sorted(list_of_tuples, key=lambda item: item[2], reverse=reverse_sort) # print("sorted_list: ", sorted_sort_it) UI.set_message(UI.get_bloc_table(sorted_list_of_tuples, header_message))
def list_all(self): """ Displays all the apartments in the bloc """ UI.set_message(UI.get_bloc_table(self._bloc_dict))