Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.99 KB | None | 0 0
  1. """ User Interface (UI) module """
  2. def find_longest_width(table, title_list):
  3.     width_list = []
  4.     for title in title_list:
  5.         width_list.append(len(title))
  6.     for line in table:
  7.         for num, col in enumerate(line):
  8.             if len(str(col)) > width_list[num]:
  9.                 width_list[num] = len(str(col))
  10.     return width_list
  11.  
  12.  
  13. def copy_table(table):
  14.     copied_table = []
  15.     copied_line = []
  16.     ID_POSITION = 0
  17.    
  18.     for index, record in enumerate(table):
  19.         copied_line = record[:]
  20.         copied_line[ID_POSITION] = str(index+1)
  21.         copied_table.append(copied_line)
  22.     return copied_table
  23.  
  24.  
  25. def sum_list_of_nums(list_to_sum):
  26.     count = 0
  27.     for num in list_to_sum:
  28.         count += num
  29.    
  30.     return count
  31.  
  32. def print_table(table, title_list):
  33.     """
  34.    Prints table with data.
  35.  
  36.    Example:
  37.        /-----------------------------------\
  38.        |   id   |      title     |  type   |
  39.        |--------|----------------|---------|
  40.        |   0    | Counter strike |    fps  |
  41.        |--------|----------------|---------|
  42.        |   1    |       fo       |    fps  |
  43.        \-----------------------------------/
  44.  
  45.    Args:
  46.        table (list): list of lists - table to display
  47.        title_list (list): list containing table headers
  48.  
  49.    Returns:
  50.        None: This function doesn't return anything it only prints to console.
  51.    """
  52.     table_copy = copy_table(table)
  53.     width_list = find_longest_width(table_copy, title_list)
  54.     top = '-' * (sum_list_of_nums(width_list)+len(width_list)*2+len(width_list)+1-2)
  55.     spacer = top + 2 * '-'
  56.     print(f'/{top}\\')
  57.     print('|', end='')
  58.     for num, title in enumerate(title_list):
  59.         print(f"{title.center(width_list[num]+2)}|", end='')
  60.     print()
  61.     print(spacer)
  62.     for record in table_copy:
  63.         print('|', end='')
  64.         for col in range(len(record)):
  65.             print(f"{str(record[col]).center(width_list[col]+2)}|", end='')
  66.         if table_copy.index(record) == len(table_copy)-1:
  67.             print()
  68.             print(f'\\{top}/')
  69.         else:
  70.             print()
  71.             print(spacer)    
  72.        
  73.     # your goes code bal balalsad
  74.     # testing merging
  75.  
  76.  
  77. def print_result(result, label):
  78.     """
  79.    Displays results of the special functions.
  80.  
  81.    Args:
  82.        result: result of the special function (string, number, list or dict)
  83.        label (str): label of the result
  84.  
  85.    Returns:
  86.        None: This function doesn't return anything it only prints to console.
  87.    """
  88.  
  89.     # your code
  90.     print(label)
  91.     print()
  92.     print(result)
  93.  
  94. def print_menu(title, list_options, exit_message):
  95.     """
  96.    Displays a menu. Sample output:
  97.        Main menu:
  98.            (1) Store manager
  99.            (2) Human resources manager
  100.            (3) Inventory manager
  101.            (4) Accounting manager
  102.            (5) Sales manager
  103.            (6) Customer relationship management (CRM)
  104.            (0) Exit program
  105.  
  106.    Args:
  107.        title (str): menu title
  108.        list_options (list): list of strings - options that will be shown in menu
  109.        exit_message (str): the last option with (0) (example: "Back to main menu")
  110.  
  111.    Returns:
  112.        None: This function doesn't return anything it only prints to console.
  113.    """
  114.     print()
  115.     print(title)
  116.     for index, element in enumerate(list_options):
  117.         print('    ({}) {}'.format(index + 1, element))
  118.     print('    (0) {}'.format(exit_message))
  119.     # your code
  120.     # Line added to test development branch!
  121.  
  122.  
  123. def get_inputs(list_labels, title):
  124.     """
  125.    Gets list of inputs from the user.
  126.    Sample call:
  127.        get_inputs(["Name","Surname","Age"],"Please provide your personal information")
  128.    Sample display:
  129.        Please provide your personal information
  130.        Name <user_input_1>
  131.        Surname <user_input_2>
  132.        Age <user_input_3>
  133.    Args:
  134.        list_labels (list): labels of inputs
  135.        title (string): title of the "input section"
  136.  
  137.    Returns:
  138.        list: List of data given by the user. Sample return:
  139.            [<user_input_1>, <user_input_2>, <user_input_3>]
  140.    """
  141.     print()
  142.     print(title)
  143.     inputs = []
  144.     for label in list_labels:
  145.         print(label)
  146.         user_input = input()
  147.         inputs.append(user_input)
  148.     # print(inputs)
  149.     return inputs
  150.  
  151.  
  152. def print_error_message(message):
  153.     """
  154.    Displays an error message (example: ``Error: @message``)
  155.  
  156.    Args:
  157.        message (str): error message to be displayed
  158.  
  159.    Returns:
  160.        None: This function doesn't return anything it only prints to console.
  161.    """
  162.  
  163.     # your code
  164.  
  165.  
  166. def headline(head):
  167.     headline = '\033[1;34;49m {}'.format(head)
  168.     headline_alignment = headline.center(60)
  169.     print(headline_alignment, '\033[0;37;49m ')
  170.     # print('\033[0;37;49m ')
  171.  
  172.  
  173. def print_enumerate_table(table):
  174.     for i, item in enumerate(table, 1):
  175.         print(i, '.', item)
  176.         # print(i, '.', item, end='')
  177.  
  178.  
  179. def blank_line():
  180.     print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement