def show_bar(self, line): # Draw Bars try: if len(self._std.get_gender()) == 0 or len( self._std.get_gender()) == 0: raise ValueError("No data to display.") # Draw gender if line.upper() == Data.GENDER.name: View.plot_bar(self._std.get_gender(), "Gender Distribution") # Draw BMI if line.upper() == Data.BMI.name: View.plot_bar(self._std.get_bmi(), "Body Mass Index (BMI)") except ValueError as e: View.info(e) except Exception as e: View.error(e)
def do_save(self, arg): """ Save data to specified data source :param arg: arg :return: None """ # If no data source selected, prompt user to do so. try: self._std.save_data() except ValueError as e: View.info(e) except (OSError, AttributeError) as e: View.error(e) except Exception as e: View.error(e) else: View.success("Data is saved")
def do_import(self, line): """ Import command :param line: (String) [-csv|-pk] [file path] :return: None :Author: Zhiming Liu """ cmd = ImportCommand(line) if cmd.file_type is None or cmd.file_name is None: View.info("Invalid command.") View.help_import() elif cmd.file_type == "csv": self.import_csv(cmd.file_name) elif cmd.file_type == "pk": self.import_pickle(cmd.file_name)
def do_show(self, line): """ Show command :param line: (String) [-t|-p|-b] [object] :return: None :Author: Zhiming Liu """ # Get all instructions args = list(arg.lower() for arg in str(line).split()) # Those commands are required single arguments # single_commands = ["-a"] # Those commands are required two arguments plot_commands = ["-p", "-b"] # Show data table if args[0] == "-t": if len(self._std.data) == 0 and len(self._std.new_data) == 0: View.info("No data to display.") if not len(self._std.data) == 0: View.display("ORIGINAL DATA:") View.display_data(self._std.data, ind=True) if not len(self._std.new_data) == 0: View.display("\nNEW DATA:") View.display_data(self._std.new_data, ind=True) View.display("\n(Input command \"save\" to save the new data)") elif args[0] in plot_commands: try: if len(args) == 1: raise IndexError("Incomplete command line.") if args[0] == "-p": self.show_pie(args[1]) if args[0] == "-b": self.show_bar(args[1]) except IndexError as e: View.error(str(e) + "\n") View.help_show() else: View.info("Invalid command line.\n") View.help_show()
def do_export(self, line): """ Export command :param line: (String) [-pk] [file path] :return: None :Author: Zhiming Liu """ args = list(arg.lower() for arg in str(line).split()) if args[0] == "-pk" and len(args) > 1: try: pk = PickleOperations() pk.pickle_dump(args[1], self._std.get_all_data()) except Exception as e: View.error(e) View.help_export() else: View.success("Data has been saved to %s" % args[1]) else: View.info("Invalid command") View.help_export()
def show_bar(self, line): """ Draw bar chart :param line: String :return: None :Author: Zhiming Liu """ # Draw Bars try: if self._std.get_gender().total_count == 0 \ or len(self._std.get_bmi()) == 0: raise ValueError("No data to display.") # Draw gender if line.upper() == Data.GENDER.name: View.plot_bar(self._std.get_gender().formatted_data, "Gender Distribution", "numer of people") # Draw BMI if line.upper() == Data.BMI.name: View.plot_bar(self._std.get_bmi(), "Body Mass Index (BMI)", "number of people") except ValueError as e: View.info(e) except Exception as e: View.error(e)