def choose(): inputs = ui.get_inputs(["Please enter a number"], "") option = inputs[0] table = data_manager.get_table_from_file(FILE_NAME) common.add_short_id(table) if option == "1": show_table(table) elif option == "2": table = add(table) elif option == "3": update(table, ui.get_id()) # id_ z funkcji z common elif option == "4": table = remove(table, ui.get_id()) elif option == "5": longest_name_id = get_longest_name_id(table) ui.print_result(longest_name_id, 'Longest name id\n') elif option == "6": newsletter_customers = get_subscribed_emails(table) ui.print_result(newsletter_customers, 'Customers with newsletter subscription\n') elif option == "0": return False else: raise KeyError("There is no such option") common.remove_short_id(table) data_manager.write_table_to_file(FILE_NAME, table) return True
def get_lowest_price_item_id(table): """ Question: What is the id of the item that was sold for the lowest price? if there are more than one item at the lowest price, return the last item by alphabetical order of the title Args: table (list): data table to work on Returns: string: id """ #set starting lowest price as the one of first element lowest_price = int(table[0][price_index]) lowest_price_id = 0 for i in range(len(table)): if lowest_price > int(table[i][price_index]): lowest_price = int(table[i][price_index]) lowest_price_id = i elif lowest_price == int( table[i][price_index] ) and table[i][title_index] < table[lowest_price_id][title_index]: lowest_price_id = i ui.print_result([table[lowest_price_id]], "Lowest price game") return table[lowest_price_id][unique_id]
def get_item_id_sold_last(): """ Reads the table with the help of the data_manager module. Returns the _id_ of the item that was sold most recently. Returns: str: the _id_ of the item that was sold most recently. """ month_column = 3 day_column = 4 year_column = 5 all_date_list = [] filecontent = data_manager.get_table_from_file('sales/sales.csv') for line in filecontent: date = line[month_column] + ";" + \ line[day_column] + ";" + line[year_column] all_date_list.append(date) dates_sorted_ = common.my_sort_(all_date_list) latest_date = dates_sorted_[0] latest_dates_list = latest_date.split(';') item_id_index = 0 month = 0 day = 1 year = 2 for data in filecontent: if latest_dates_list[month] in data and latest_dates_list[ day] in data and latest_dates_list[year] in data: output = data[item_id_index] ui.print_result(output, "Last item's ID is: ") return output
def choose_option(table): """ Asks user for input and basing on this calls proper function Args: table: list of lists with data Returns: Table with a new record String with user's input """ inputs = ui.get_inputs(["Please enter a number: "], "") option = inputs[0] if option == "1": show_table(table) elif option == "2": table = add(table) elif option == "3": id_ = ui.get_inputs(["Id"], "Please provide record you want to remove")[0] table = remove(table, id_) elif option == "4": id_ = ui.get_inputs(["Id"], "Please provide record you want to update")[0] table = update(table, id_) elif option == "5": result = get_oldest_person(table) ui.print_result(result, "The oldest people are: ") elif option == "6": result = get_persons_closest_to_average(table) ui.print_result(result, "The closest people to average age are: ") return option, table
def get_persons_closest_to_average(table, *args): """ Question: Who is the closest to the average age? Args: table (list): data table to work on Returns: list: list of strings (name or names if there are two more with the same value) """ table = sort_by_year(table) average_age = 0 for row in table: average_age += int(row[YEAR_NUMBER]) average_age = average_age / len(table) for row in table: row[YEAR_NUMBER] = abs(float(row[YEAR_NUMBER]) - average_age) table = sort_by_year(table) average_people = [] for row in table: if row[YEAR_NUMBER] == table[0][YEAR_NUMBER]: average_people.append(row[NAME_NUMBER]) ui.print_result(average_people, 'The average aged person is')
def get_counts_by_manufacturers(table): manufacturer = [] count = 0 # count from 0 for i, value in enumerate(table): manufacturer.append(value[2]) # if the manufacturer in the list , its gonna take the it to the new list ui.print_result(result=dict((i, manufacturer.count(i)) for i in manufacturer), label="") # format to dict and return
def choose(table): inputs = ui.get_inputs(["Please enter a number: "], "") option = inputs[0] if option == "1": show_table(table[:]) elif option == "2": add(table) elif option == "3": id_to_remove = ui.get_inputs(['Enter id to remove: '], '') if common.is_this_record_exist(table, id_to_remove): remove(table, id_to_remove) elif option == "4": id_to_update = ui.get_inputs(['Enter id to update'], '') if common.is_this_record_exist(table, id_to_update): table = update(table, id_to_update) elif option == "5": year = which_year_max(table) ui.print_result(str(year), 'Best profit year') elif option == "6": year = ui.get_inputs(['which year?'], '') if year[0].isdigit(): answear = avg_amount(table, year[0]) ui.print_result(str(answear), 'Averge profit') else: ui.print_error_message('Wrong year') elif option == "0": return False else: raise KeyError("There is no such option.")
def get_items_sold_between( table, month_from, day_from, year_from, month_to, day_to, year_to): """ Calcualte items, which are sold between two given dates. :param table: text file where are included some information. :param ..._from: choose "MM/YY/DD" from start to search :param ..._to: choose "MM/YY/DD" to start to search :return: list of lists with items in range. """ date_from = "".join(str(year_from) + str(month_from) + str(day_from)) date_to = "".join(str(year_to) + str(month_to) + str(day_to)) dates_to_check = ["".join(str(record[5]) + str(record[3]) + str(record[4])) for record in table] # convert to YEAR/MONTH/DAY items_sold_between = [table[i] for i in range( len(table)) if date_from < dates_to_check[i] < date_to] convert_items = [[int(number) if number.isdigit() else number for number in record] for record in items_sold_between] ui.print_result(convert_items, "Items from range:") show_table(items_sold_between) return convert_items
def update(table, id_): """ Updates specified record in the table. Ask users for new data. Args: table (list): list in which record should be updated id_ (str): id of a record to update Returns: list: table with updated record """ id_ = ("".join(map(str, id_))) index_table = 0 for row in table: if row[0] == id_: ui.print_result(row, f"This is employee which you choose ") datauser = ui.get_inputs( ['input your name: ', 'Choose your hire year: '], "Please insert new information") table[index_table][1:] = datauser ui.print_result(table[index_table], f"This is your record after changes") index_table += 1 data_manager.write_table_to_file("hr/persons.csv", table) return table
def choose(): while True: ui.print_menu("Store", options, "Back to main menu") inputs = ui.get_inputs(["Please enter a number: "], "") option = inputs[0] if option == "1": show_table(table) elif option == "2": add(table) elif option == "3": id_ = ui.get_inputs(['Please enter ID:'], "") remove(table, id_) elif option == "4": id_ = ui.get_inputs(['Please enter ID:'], "") update(table, id_) elif option == "5": counts_by_manufacturers = get_counts_by_manufacturers(table) ui.print_result(counts_by_manufacturers, 'number of games are available of each manufacturer list ') elif option == "6": manufacturer_input = ui.get_inputs(['Please enter manufacturer:'], "") average_stock = get_average_by_manufacturer(table, manufacturer_input) ui.print_result(average_stock, 'average stock by manufacturers ') elif option == "0": answer_list = ui.get_inputs(["Do you want to save the changes? (Y/N)"], "") answer = answer_list[0].upper() if answer == "Y": data_manager.write_table_to_file('store/games.csv', table) elif answer == "N": break else: ui.print_error_message("Invalid answer.") else: ui.print_error_message("There is no such option.")
def choose(table): ''' Asks user to choose a number of special feature in this module Gets inputs needed to run chosen function User may exit this module by typing '0' Returns: table = list of lists option = string ''' inputs = ui.get_inputs(["Please enter a number: "], "") option = inputs[0] if option == "1": show_table(table) elif option == "2": table = add(table) elif option == "3": id_ = ui.get_inputs(["Enter person's id: "], "") table = remove(table, id_) elif option == "4": id_ = ui.get_inputs(["Enter person's id: "], "") table = update(table, id_) elif option == "5": result = get_oldest_person(table) ui.print_result(result, "Oldest people") elif option == "6": result = get_persons_closest_to_average(table) ui.print_result(result, "People closest to the average age") return table, option
def get_counts_by_manufacturers(table): """ Question: How many different kinds of game are available of each manufacturer? Args: table (list): data table to work on Returns: dict: A dictionary with this structure: { [manufacturer] : [count] } """ readed_file = data_manager.get_table_from_file(table) POSITION_OF_MANUFACTURER = 2 i = 0 manufacturer_dict = {} starting_game_number = 1 while i < len(readed_file): manufacturer_name = readed_file[i][POSITION_OF_MANUFACTURER] if manufacturer_name in manufacturer_dict.keys(): manufacturer_dict[manufacturer_name] += 1 else: manufacturer_dict[manufacturer_name] = starting_game_number i += 1 for k, v in manufacturer_dict.items(): ui.print_result(f'{v} diffrent games', f"{k} has ") time.sleep(2) return table
def get_the_buyer_name_spent_most_and_the_money_spent(): """ Returns the customer's _name_ who spent the most in sum and the money (s)he spent. Returns: tuple: Tuple of customer name and the sum the customer spent eg.: ('Daniele Coach', 42) """ # your code sales_id_dict = sales.get_all_sales_ids_for_customer_ids() list_of_tuples = [] for key in sales_id_dict.keys(): sum_per_id = sales.get_the_sum_of_prices(sales_id_dict[key]) list_of_tuples.append((key, sum_per_id)) maxi = list_of_tuples[0][1] returnable_list = [] for elem in list_of_tuples: if elem[1] > maxi: maxi = elem[1] for elem in list_of_tuples: if elem[1] == maxi: returnable_list.append((crm.get_name_by_id(elem[0]), elem[1])) ui.print_result(returnable_list,'Most frequent buyer(s) Name(s).')
def get_the_buyer_id_spent_most_and_the_money_spent(): """ Returns the customer's _id_ who spent more in sum and the money (s)he spent. Returns: tuple: Tuple of customer id and the sum the customer spent eg.: (aH34Jq#&, 42) """ # your code sales_id_dict = sales.get_all_sales_ids_for_customer_ids() list_of_tuples = [] for key in sales_id_dict.keys(): sum_per_id = sales.get_the_sum_of_prices(sales_id_dict[key]) list_of_tuples.append((key, sum_per_id)) maxi = list_of_tuples[0][1] returnable_list = [] for elem in list_of_tuples: if elem[1] > maxi: maxi = elem[1] for elem in list_of_tuples: if elem[1] == maxi: returnable_list.append((elem)) ui.print_result(returnable_list,'Most frequent buyer(s) ID.')
def get_the_last_buyer_name(table): """ Returns the customer _name_ of the customer made sale last. Returns: str: Customer name of the last buyer """ crm_table = crm.table sales_table = sales.table item_date = 0 last_item_date = 0 for row in sales_table: for column in row: item_date = row[5]+row[3]+row[4] if int(item_date) > int(last_item_date): last_item_date = item_date temp_sales_id = row[0] print(temp_sales_id) for row in crm_table: for column_1 in row: if temp_sales_id in row: temp_name = row[1] ui.print_result(temp_name, "Name of last buyer: ")
def add(table): """ Asks user for input and adds it into the table. Args: table (list): table to add new record to Returns: list: Table with a new record """ message = ("Please check your input") CURRENT_YEAR = 2020 id_ = common.generate_random(table) datauser = ui.get_inputs(['input your name: ', 'Choose your hire year: '], "Please provide your personal information") datauser[1] = int(datauser[1]) # while isinstance(datauser[0], str) and isinstance(datauser[1], int) and datauser[1] <= CURRENT_YEAR: if isinstance(datauser[0], str) and isinstance( datauser[1], int) and datauser[1] <= CURRENT_YEAR: table.append([id_, datauser[0], str(datauser[1])]) label = "You added the new emploee" ui.print_result(datauser, label) data_manager.write_table_to_file("hr/persons.csv", table) else: ui.print_error_message(message) return table
def choose(): inputs = ui.get_inputs(['Enter a number: '], '') option = inputs[0] if option == "1": show_table(TABLE) elif option == "2": add(TABLE) elif option == "3": id_get = ui.get_inputs([LIST_OF_TITLES[0]],"Enter the ID of the item you want to delete: ") id_=''.join(id_get) remove(TABLE,id_) data_manager.write_table_to_file(file_name, TABLE) elif option == "4": id_get = ui.get_inputs([LIST_OF_TITLES[0]],"Enter the ID of the item you want to modify: ") id_=''.join(id_get) table = update(TABLE,id_) data_manager.write_table_to_file(file_name, table) elif option == "5": ui.print_result (get_longest_name_id(TABLE), "The longest name is:") elif option == "6": ui.print_result(get_subscribed_emails(TABLE), "The names and emails of the subscribed customers:") elif option == '0': raise ValueError while option not in OPTION: raise KeyError
def update(table, id_): """ Updates specified record in the table. Ask users for new data. Args: table (list): list in which record should be updated id_ (str): id of a record to update Returns: list: table with updated record """ index_table = 0 for row in table: if row[0] == id_: ui.print_result(row, "This is client you want to update: ") datauser = ui.get_inputs([ 'Please input new name: ', 'Please input new email: ', 'Is client is subscribed to newsletter or not 1/0 = yes/no ' ], "Please insert new information") table[index_table][1:] = datauser ui.print_result(table[index_table], "This is your record after changes") index_table += 1 data_manager.write_table_to_file("crm/customers.csv", table) return table
def choose(): while True: ui.print_menu("CRM", options, "Back to main menu") inputs = ui.get_inputs(["Please enter a number: "], "") option = inputs[0] if option == "1": show_table(table) elif option == "2": add(table) elif option == "3": id_ = ui.get_inputs(['Please enter ID'], "") remove(table, id_) elif option == "4": id_ = ui.get_inputs(['Please enter ID'], "") update(table, id_) elif option == "5": result_get_longest_name_id = get_longest_name_id(table) ui.print_result(result_get_longest_name_id, "longest name ID ") elif option == "6": result_get_subscribed_emails = get_subscribed_emails(table) ui.print_result(result_get_subscribed_emails, "Subscribed customer(s):") elif option == "0": answer_list = ui.get_inputs( ["Do you want to save the changes? (Y/N)"], "") answer = answer_list[0].upper() if answer == "Y": data_manager.write_table_to_file('crm/customers.csv', table) elif answer == "N": break else: ui.print_error_message("Invalid answer.") else: ui.print_error_message("There is no such option.")
def add(table): """ Asks user for input and adds it into the table. Args: table (list): table to add new record to Returns: list: Table with a new record """ message = ("Please check your input") while True: id_ = common.generate_random(table) datauser = ui.get_inputs([ 'Input client name: ', 'Input client email: ', 'Is she/he subscribed to the newsletter? 1/0 = yes/no ' ], "Please provide your personal information") if isinstance(datauser[0], str) and datauser[2] == '0' or datauser[2] == '1': table.append([id_, datauser[0], datauser[1], datauser[2]]) label = "You have just added new client: " ui.print_result(datauser, label) data_manager.write_table_to_file("crm/customers.csv", table) break else: ui.print_error_message(message) return table
def choose(): inputs = ui.get_inputs(['Enter a number: '], '') option = inputs[0] if option == "1": show_table(TABLE) elif option == "2": add(TABLE) elif option == "3": id_get = ui.get_inputs([LIST_OF_TITLES[0]], "Enter the ID of the item you want to delete: ") id_ = ''.join(id_get) remove(TABLE, id_) data_manager.write_table_to_file(file_name, TABLE) elif option == "4": id_get = ui.get_inputs([LIST_OF_TITLES[0]], "Enter the ID of the item you want to modify: ") id_ = ''.join(id_get) table = update(TABLE, id_) data_manager.write_table_to_file(file_name, table) elif option == "5": ui.print_result(get_oldest_person(TABLE), "The oldest person is:") elif option == "6": ui.print_result(get_persons_closest_to_average(TABLE), "Closest to average: ") elif option == '0': raise ValueError while option not in OPTION: raise KeyError
def choose_option(table): inputs = ui.get_inputs(["Please enter a number: "], "") option = inputs[0] if option == "1": show_table(table) elif option == "2": table = add(table) elif option == "3": id_ = ui.get_inputs(["Id"], "Please provide record you want to remove")[0] table = remove(table, id_) elif option == "4": id_ = ui.get_inputs(["Id"], "Please provide record you want to update")[0] table = update(table, id_) elif option == "5": result = get_available_items(table) ui.print_result(result, "Item that not exceed their durability: ") elif option == "6": result = get_average_durability_by_manufacturers(table) ui.print_result(result, "Average durability itmes for each manufacturer: ") return option, table
def which_year_max(table): """ Question: Which year has the highest profit? (profit = in - out) Args: table (list): data table to work on Returns: number """ # 5 profit = {} for line in table: if line[3] in profit.keys(): if line[4] == 'in': profit[line[3]] += int(line[5]) elif line[4] == 'out': profit[line[3]] -= int(line[5]) else: if line[4] == 'in': profit[line[3]] = int(line[5]) elif line[4] == 'out': profit[line[3]] = -(int(line[5])) for key, value in profit.items(): if value == max(profit.values()): ui.print_result(key, 'The biggest profit was in') return int(key)
def get_oldest_person(table): """ Question: Who is the oldest person? Args: table (list): data table to work on Returns: list: A list of strings (name or names if there are two more with the same value) """ # oldest = [] # for i,value in enumerate(table): # if value[2] >= value[2]: # oldest.append(value[1]) # print(min(oldest)) date_dict = {} d = date_dict for line in table: date = int(line[2]) date_dict[line[1]] = date sort_dict = sorted(d.items(), key=lambda x: (x[0], -x[1])) # sorting by year the names enough = [] for i, v in sort_dict: enough.append(i) # print(enough) ui.print_result(enough[:3], label="")
def get_lowest_price_item_id(table): """ Question: What is the id of the item that was sold for the lowest price? if there are more than one item at the lowest price, return the last item by alphabetical order of the title Args: table (list): data table to work on Returns: string: id """ # 5 lowest_price = 100 id_ = [] title = [] for line in table: if int(line[2]) < lowest_price: id_.clear() title.clear() lowest_price = int(line[2]) id_.append(line[0]) title.append(line[1]) else: if int(line[2]) == lowest_price: id_.append(line[0]) title.append(line[1]) ui.print_result(sort_list(id_)[-1], 'The ID of lowest sold is') return sort_list(id_)[-1]
def get_counts_by_manufacturers(table, *args): """ Question: How many different kinds of game are available for each manufacturer? Args: table (list): data table to work on Returns: dict: A dictionary with this structure: { [manufacturer] : [count] } """ manufacturers = set(row[MANUFACTURER_NUMBER] for row in table) counts_by_manufacturers = {m:0 for m in manufacturers} for m in manufacturers: for row in table: if row[MANUFACTURER_NUMBER] == m: counts_by_manufacturers[m] += 1 # Delete this after you fix the print_result() to also print dictionaries: counts_by_manufacturers = [(k,v) for k, v in counts_by_manufacturers.items()] ui.print_result(counts_by_manufacturers, 'Number of games for each manufacturer') return counts_by_manufacturers
def choose_option(table): inputs = ui.get_inputs(["Please enter a number: "], "") option = inputs[0] if option == "1": show_table(table) elif option == "2": table = add(table) elif option == "3": id_ = ui.get_inputs(["Id"], "Please provide record you want to remove")[0] table = remove(table, id_) elif option == "4": id_ = ui.get_inputs(["Id"], "Please provide record you want to update")[0] table = update(table, id_) elif option == "5": result = which_year_max(table) result = str(result) ui.print_result(result, "The year with the hightest profit is: ") elif option == "6": year = ui.get_inputs( ["Year"], "Please provide year for which you want to see profit")[0] try: result = avg_amount(table, year) result = str(result) ui.print_result( result, "The Avarage profit per item in {} is: ".format(year)) except UnboundLocalError as err: ui.print_error_message("") return option, table
def get_average_by_manufacturer(table): """ Question: What is the average amount of games in stock of a given manufacturer? Args: table (list): data table to work on manufacturer (str): Name of manufacturer Returns: number """ manufacturers = set(row[MANUFACTURER_NUMBER] for row in table) the_manufacturer = ui.get_inputs(['Enter the manufacturer: '], '')[0] print(the_manufacturer) average_by_manufacturer = 0 game_count = 0 for row in table: if row[MANUFACTURER_NUMBER] == the_manufacturer: average_by_manufacturer += int(row[IN_STOCK_NUMBER]) game_count += 1 try: average_by_manufacturer /= game_count ui.print_result(average_by_manufacturer, f'Average size of {the_manufacturer}\'s game stock is') except ZeroDivisionError: common.ID_error()
def get_item_id_sold_last_from_table(table): """ Returns the _id_ of the item that was sold most recently. Args: table (list of lists): the sales table Returns: str: the _id_ of the item that was sold most recently. """ filecontent = table month = 3 day = 4 year = 5 all_date_list = [] for line in filecontent: date = line[month] + ";" + line[day] + ";" + line[year] all_date_list.append(date) dates_sorted_ = common.my_sort_(all_date_list) latest_date = dates_sorted_[0] latest_dates_list = latest_date.split(';') item_id_sold_last = 0 day = 1 month = 0 year = 2 for data in filecontent: if latest_dates_list[month] in data and latest_dates_list[ day] in data and latest_dates_list[year] in data: output = data[item_id_sold_last] ui.print_result(output, 'Last sold item ID is: ') return output
def get_persons_closest_to_average(table): """ Question: Who is the closest to the average age? Args: table (list): data table to work on Returns: list: list of strings (name or names if there are two more with the same value) """ # your code ages = [] avg = 0 list_of_names = [] for i in range(len(table)): ages.append(2019 - int(table[i][2])) closest_to_avg = abs(ages[0] - avg) avg = common.avg_of_list(ages) for j in range(len(ages)): if abs(ages[j] - avg) < closest_to_avg: closest_to_avg = abs(ages[j] - avg) for k in range(len(table)): if abs((2019 - int(table[k][2])) - avg) == closest_to_avg: list_of_names.append(table[k][1]) ui.print_result(list_of_names, "") ui.get_inputs("Press Enter to to advance!", "") return list_of_names