def average_age(): date_list = [] employees_list = data_manager.read_table_from_file(DATAFILE) for the_date in employees_list: date_list.append(the_date[BIRTH_DATE]) TEMP = [] TEMP_2 = [] for i in range(len(date_list)): employee_bday = date_list[i].replace("-", ",") employee_bday = employee_bday.split(",") for element in employee_bday: element = int(element) TEMP_2.append(element) employee_bday = TEMP_2 TEMP_2 = [] TEMP.append(employee_bday) employees_bdays = TEMP ages = [] for i in employees_bdays: days_in_year = 365.2425 age_count = int((date.today() - date(i[0], i[1], i[2])).days / days_in_year) ages.append(age_count) age = 0 total = 0 for age in ages: total += age average = round(total / len(ages)) return average
def read_data(file_name=DATAFILE): '''Reads all data from file. As an option, it adds a header to the data.''' table = data_manager.read_table_from_file(file_name) if table == []: # error: reading data from file faild raise IOError("Error reading data from file") return table
def remove_record(record_id): table=data_manager.read_table_from_file(DATAFILE, separator=';') for n in range(0, len(table)): if table[n][0] == record_id: del table[n] data_manager.write_table_to_file(DATAFILE, table, separator=';') return table
def update_nested_list_and_write_content(number, entry): data = data_manager.read_table_from_file(DATAFILE) index_of_customer = int(number) - 1 data[index_of_customer][1] = entry[0] data[index_of_customer][2] = entry[1] data[index_of_customer][3] = entry[2] write_to_file(data)
def count_employees_with_clearance(celearance_level): table = data_manager.read_table_from_file(DATAFILE) employees_with_clearance_level = 0 for employee in table: if int(employee[CLEARANCE_INDEX]) >= celearance_level: employees_with_clearance_level += 1 return employees_with_clearance_level
def update_employee(updated_employee_id, updated_employee_data): new_table = data_manager.read_table_from_file(DATAFILE) for employee in new_table: if employee[ID_INDEX] == updated_employee_id: updated_employee_data.insert(0, employee[ID_INDEX]) new_table[new_table.index(employee)] = updated_employee_data data_manager.write_table_to_file(DATAFILE, new_table, ";")
def run(): input_file = "resource/input/rawData3.txt" output_file = "resource/output/output3.txt" lines = data_manager.read_table_from_file(input_file) int_account_numbers = convert_line_account_numbers_to_int_account_numbers( lines) data_manager.write_table_to_file(output_file, int_account_numbers)
def get_biggest_revenue_transaction(): lines = file_handling.read_table_from_file(sales.DATAFILE, separator=';') biggest_revenue_transaction_list = [] line_of_biggest_revenue_transaction = biggest_revenue_product_and_transaction( ) biggest_revenue_transaction_list.append( lines[line_of_biggest_revenue_transaction]) view.print_table(biggest_revenue_transaction_list, sales.HEADERS)
def append_nested_list_and_write_content(entry): data = data_manager.read_table_from_file(DATAFILE) name, birth_date, department, clearance = entry data_to_append = [ util.generate_id(), name, birth_date, department, clearance ] data.append(data_to_append) write_to_file(data)
def count_employees_with_clearance(): temp_list = data_manager.read_table_from_file(hr.DATAFILE) user_input = input('\nSelect Clearance Level: ') count = 0 for item in temp_list: if item[4] == str(user_input): count += 1 print('\nNumber of employees with clearance level ',user_input,' is :',count)
def revenue_in_dates(date1, date2): table=data_manager.read_table_from_file(DATAFILE, separator=';') transactions_dates = transaction_in_period(date1, date2) revenue_in_period=0 for n in range(0, len(table)): if table[n][4] in transactions_dates: revenue_in_period=revenue_in_period+float(table[n][3]) return revenue_in_period
def modify_item(record_id, Customer, Product, Price, Date): table=data_manager.read_table_from_file(DATAFILE, separator=';') for n in range(0, len(table)): if table[n][0] == record_id: table[n]=[record_id, Customer, Product, Price, Date] print(table) data_manager.write_table_to_file(DATAFILE, table, separator=';') return table
def count_employees_per_department(): table = data_manager.read_table_from_file(DATAFILE) employees_per_department = {} for employee in table: if employee[DEPARTMENT_INDEX] not in employees_per_department: employees_per_department[employee[DEPARTMENT_INDEX]] = 1 elif employee[DEPARTMENT_INDEX] in employees_per_department: employees_per_department[employee[DEPARTMENT_INDEX]] += 1 return employees_per_department
def transaction_in_period(date1, date2): table=data_manager.read_table_from_file(DATAFILE, separator=';') date_list=column_from_table(HEADERS.index("Date")) good_dates=[] #---------choosing dates between date1 and date2 for date in date_list: if date>date1 and date<date2: good_dates.append(date) return good_dates
def read(): """Read file "model/sales/sales.csv" and remove empty records.""" data = data_manager.read_table_from_file(DATAFILE) data_without_empty_lines = [] for index in range(len(data)): if (len(data[index]) == 1 and data[index][0] == '') or data[index] == []: continue data_without_empty_lines.append(data[index]) return data_without_empty_lines
def count_transactions_between(): my_list = data_manager.read_table_from_file(sales.DATAFILE, ";") list_with_years = [] list_with_months = [] list_with_days = [] year_start_input = int(input("The year from which the transactions begin: ")) while year_start_input not in range(1990, 2021): year_start_input = int(input("The year from which the transactions begin(1990-2020): ")) mounth_start_input = int(input("The mounth from which the transactions begin: ")) while mounth_start_input not in range(1, 13): mounth_start_input = int(input("The mounth from which the transactions begin(1-12): ")) day_start_input = int(input("The day from which the transactions begin: ")) while day_start_input not in range(1, 32): day_start_input = int(input("The day from which the transactions begin(1-31): ")) year_finish_input = int(input("In what year does the transaction end?: ")) while year_finish_input not in range(1990, 2021): year_finish_input = int(input("In what year does the transaction end?(1990-2020): ")) mounth_finish_input = int(input("In what mounth does the transaction end?: ")) while mounth_finish_input not in range(1, 13): mounth_finish_input = int(input("In what mounth does the transaction end?(1-12): ")) day_finish_input = int(input("In what day does the transaction end?: ")) while day_finish_input not in range(1, 32): day_finish_input = int(input("In what day does the transaction end?(1-31): ")) for element in my_list: date = element[4][:4] if int(date) >= year_start_input and int(date) <= year_finish_input: list_with_years.append(element) for element1 in list_with_years: date11 = element1[4][:4] date22 = element1[4][5:7] if int(date11) == year_start_input: if int(date22) >= mounth_start_input and int(date22) <= mounth_finish_input: list_with_months.append(element1) else: list_with_months.append(element1) for element2 in list_with_months: date33 = element[4][5:7] date44 = element2[4][8:10] if int(date33) == mounth_start_input: if int(date44) >= day_start_input and int(date44) <= day_finish_input: list_with_days.append(element2) else: list_with_days.append(element2) print("The number of transactions: " + str(len(list_with_days)))
def count_employees_per_department(): temp_list = data_manager.read_table_from_file(hr.DATAFILE) deps = {} for item in temp_list: if item[3] in deps: deps[item[3]] += 1 else: deps[item[3]] = 1 for key,value in deps.items(): print(key,': ',value)
def subscribed_emails(): client_data = data_manager.read_table_from_file(DATAFILE) e_mail_index = 2 subscription_index = 3 list_of_subscribed_emails = [] for i in range(len(client_data)): if client_data[i][subscription_index] == "1" and "@" in client_data[i][ e_mail_index]: list_of_subscribed_emails.append(client_data[i][e_mail_index]) return list_of_subscribed_emails
def update_transaction(id, index, value): updated_transaction = [] transactions = data_manager.read_table_from_file(DATAFILE) for i in range(len(transactions)): if transactions[i][ID_TRANSACTION_INDEX] == id: transactions[i][index] = value updated_transaction.append(transactions[i]) data_manager.write_table_to_file(DATAFILE, transactions) return updated_transaction
def read_data(): ''' matrix to display data accurately in view module ''' employees_list = data_manager.read_table_from_file(DATAFILE) the_list = [] for employee_data in employees_list: the_list.append(employee_data) return the_list
def given_clearance_level(given_level): ''' Returns the number of employees with at least the given clearance level ''' employees_list = data_manager.read_table_from_file(DATAFILE) count_employees = 0 for i in range(len(employees_list)): if int(employees_list[i][CLEARANCE_LEVEL]) >= int(given_level): count_employees += 1 return count_employees
def get_oldest_and_youngest(): table = data_manager.read_table_from_file(DATAFILE) date_of_birth_list = [] for employee in table: date_split = [int(i) for i in employee[DATE_OF_BIRTH_INDEX].split("-")] date_of_birth_list.append(date_split) oldest_date_of_birth_index = date_of_birth_list.index( min(date_of_birth_list)) youngest_date_of_birth_index = date_of_birth_list.index( max(date_of_birth_list)) return (table[oldest_date_of_birth_index][NAME_INDEX], table[youngest_date_of_birth_index][NAME_INDEX])
def employees_per_department(): ''' Returns the number of employees per department in a dictionary (like {'dep1': 5, 'dep2': 11}) ''' employees_list = data_manager.read_table_from_file(DATAFILE) new_list = [] for i in employees_list: new_list.append(i[DEPARTAMENT]) result = dict((i, new_list.count(i)) for i in new_list) return result
def count_and_sum_transactions(): lines = file_handling.read_table_from_file(sales.DATAFILE, separator=';') user_input_start_date = view.get_input(sales.HEADERS[DATE_INDEX]) user_input_end_date = view.get_input(sales.HEADERS[DATE_INDEX]) user_range_date_list = [user_input_start_date, user_input_end_date] counter = 0 sum_of_transactions = 0 for line in range(len(lines)): transaction_date = lines[line][DATE_INDEX] month_of_user_input_start_date = months.get( user_range_date_list[0][5:7]) month_of_user_input_end_date = months.get(user_range_date_list[1][5:7]) month_of_transaction = months.get(transaction_date[5:7]) day_of_user_input_start_date = days.get(user_range_date_list[0][8:10]) day_of_user_input_end_date = days.get(user_range_date_list[1][8:10]) day_of_transaction = days.get(transaction_date[8:10]) if transaction_date[0:4] > user_range_date_list[0][ 0:4] and transaction_date[0:4] < user_range_date_list[1][0:4]: counter += 1 sum_of_transactions = float(sum_of_transactions) + float( lines[line][PRICE_INDEX]) elif transaction_date[0:4] == user_range_date_list[0][ 0:4] or transaction_date[0:4] == user_range_date_list[1][0:4]: if transaction_date[0:4] == user_range_date_list[0][0:4]: if month_of_transaction >= month_of_user_input_start_date: if month_of_transaction > month_of_user_input_start_date: counter += 1 sum_of_transactions = float( sum_of_transactions) + float( lines[line][PRICE_INDEX]) else: if day_of_transaction >= day_of_user_input_start_date: counter += 1 sum_of_transactions = float( sum_of_transactions) + float( lines[line][PRICE_INDEX]) elif transaction_date[0:4] == user_range_date_list[1][0:4]: if month_of_transaction <= month_of_user_input_end_date: if month_of_transaction < month_of_user_input_end_date: counter += 1 sum_of_transactions = float( sum_of_transactions) + float( lines[line][PRICE_INDEX]) else: if day_of_transaction <= day_of_user_input_end_date: counter += 1 sum_of_transactions = float( sum_of_transactions) + float( lines[line][PRICE_INDEX]) return counter, sum_of_transactions
def biggest_revenue_product_and_transaction(): lines = file_handling.read_table_from_file(sales.DATAFILE, separator=';') try: biggest_revenue_transaction = float(lines[ID_INDEX][PRICE_INDEX]) for line in range(len(lines)): if biggest_revenue_transaction < float( lines[line + 1][PRICE_INDEX]): biggest_revenue_transaction = float(lines[line + 1][PRICE_INDEX]) except IndexError: for line in range(len(lines)): if biggest_revenue_transaction == float(lines[line][PRICE_INDEX]): return line
def create_data(id_number, em_name, bday_date, dept, clearance_level): ''' adding a new employee ''' employees_list = data_manager.read_table_from_file(DATAFILE) emp_list = [] for element in employees_list: emp_list.append(element) new_employee = [id_number, em_name, bday_date, dept, clearance_level] emp_list.append(new_employee) data_manager.write_table_to_file(DATAFILE, emp_list) return emp_list
def update_nested_list_and_write_content(number_of_employee, entry): name, birth_date, department, clearance = entry list_of_employes = data_manager.read_table_from_file(DATAFILE) atribute = { "index": int(number_of_employee) - 1, "name": HEADERS.index("Name"), "birth date": HEADERS.index("Date of birth"), "department": HEADERS.index("Department"), "clearance": HEADERS.index("Clearance") } for element in atribute: if element != "index": list_of_employes[atribute["index"]][atribute[element]] = entry[ atribute[element] - 1] write_to_file(list_of_employes)
def next_birthdays(): temp_list = data_manager.read_table_from_file(hr.DATAFILE) dates = [] bdays = [] today = (datetime.datetime.today()).date() two_weeks = (datetime.timedelta(days=14) + datetime.datetime.today()).date() for item in temp_list: dates.append(item[2]) for item in dates: bdays.append(datetime.datetime.strptime(item, "%Y-%m-%d").date()) print('\nBirthdays in the next two weeks: \n') for i in range(len(bdays)): if dates_between(today, bdays[i], two_weeks): for item in temp_list: if datetime.datetime.strptime(item[2], "%Y-%m-%d").date() == bdays[i]: print(item[1], bdays[i])
def get_average_age(): temp_list = data_manager.read_table_from_file(hr.DATAFILE) birthdays = [] dates = [] ages = [] x = 0 for item in temp_list: birthdays.append(item[2]) for item in birthdays: dates.append(datetime.datetime.strptime(item, "%Y-%M-%d").date()) for item in dates: ages.append(calculate_age(item)) for i in range(len(ages)): x += ages[i] avg = x/len(ages) print('\nAvarage Age is : \n') print(avg)
def names_tuple(): ''' Returns the names of the oldest and the youngest employees as a tuple ''' employees_list = data_manager.read_table_from_file(DATAFILE) difference_list = [] for i in range(len(employees_list)): bday_date = date.fromisoformat(employees_list[i][BIRTH_DATE]), employees_list[i][NAME] difference = bday_date[0] - date.today(), bday_date[1] difference_list.append(difference) min_age = max(difference_list) max_age = min(difference_list) youngest_oldest = (min_age[1], max_age[1]) return youngest_oldest