Advertisement
mikhailemv

Untitled

Dec 30th, 2022
1,149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.22 KB | None | 0 0
  1. from csv import reader
  2. from math import floor
  3. import re
  4. from collections import Counter
  5. import numpy as np
  6.  
  7.  
  8. def print_vacancies_by_salaries(list_vacancies: list, is_high_salary: bool):
  9.     list_vacancies = sorted(list_vacancies,
  10.                             key=lambda x: x['avg_salary'],
  11.                             reverse=is_high_salary)
  12.     print('Самые высокие зарплаты:' if is_high_salary else 'Самые низкие зарплаты:')
  13.     counter = 0
  14.     for index, value in enumerate(list_vacancies):
  15.         if value['salary_currency'] != 'RUR':
  16.             continue
  17.         if counter == 10:
  18.             break
  19.         counter += 1
  20.         print(
  21.             f'    {counter}) {value["name"]} в компании \"{value["employer_name"]}\" - {value["avg_salary"]} '
  22.             f'{get_ruble_suffix(value["avg_salary"])} (г. {value["area_name"]})')
  23.     print()
  24.  
  25.  
  26. def get_vacancies_suffix(count: int) -> str:
  27.     if 11 <= count % 100 <= 19:
  28.         return "вакансий"
  29.     elif count % 10 == 0 or 5 <= count % 10 <= 9:
  30.         return "вакансий"
  31.     elif 2 <= count % 10 <= 4:
  32.         return "вакансии"
  33.     else:
  34.         return "вакансия"
  35.  
  36.  
  37. def get_times_suffix(count: int) -> str:
  38.     if 11 <= count % 100 <= 19:
  39.         return "раз"
  40.     elif 2 <= count % 10 <= 4:
  41.         return "раза"
  42.     else:
  43.         return "раз"
  44.  
  45.  
  46. def get_ruble_suffix(count: int) -> str:
  47.     if count % 10 == 0 or \
  48.             5 <= count % 10 <= 9 or \
  49.             10 <= count % 100 <= 19:
  50.         return 'рублей'
  51.     elif 2 <= count % 10 <= 4:
  52.         return 'рубля'
  53.     else:
  54.         return 'рубль'
  55.  
  56.  
  57. def print_top_skills(list_vacancies: list):
  58.     list_with_right_vacancies = list()
  59.     for index, value in enumerate(list_vacancies):
  60.         for second_index, second_value in enumerate(value["key_skills"]):
  61.             list_with_right_vacancies.append(second_value.strip())
  62.     counter = Counter(list_with_right_vacancies).most_common()
  63.     print(f'Из {len(dict(counter))} скиллов, самыми популярными являются:')
  64.     dict_with_right_skills, count = dict(counter), 0
  65.     for key, value in dict(dict_with_right_skills).items():
  66.         if count == 10:
  67.             break
  68.         count += 1
  69.         print(f'    {count}) {key} - упоминается {value} {get_times_suffix(value)}')
  70.     print()
  71.  
  72.  
  73. def to_dictionary(titles, vacancies_list):
  74.     dictionary = dict()
  75.     for index, headline in enumerate(titles):
  76.         dictionary[headline] = vacancies_list[index]
  77.     dictionary['avg_salary'] = floor((float(dictionary['salary_to']) + float(dictionary['salary_from'])) / 2)
  78.     return dictionary
  79.  
  80.  
  81. def print_cities_by_salaries(input_list: list):
  82.     list_vacancies = sorted([vacancy for vacancy in input_list if vacancy['salary_currency'] == 'RUR'],
  83.                             key=lambda x: x["area_name"])
  84.     temporal_list, cities_dictionary, counter = list(), dict(), 0
  85.  
  86.     for i in range(len(list_vacancies) + 1):
  87.         if i == 0:
  88.             continue
  89.         if i == len(list_vacancies):
  90.             temporal_list.append(float(list_vacancies[i - 1]["avg_salary"]))
  91.             counter += 1
  92.             if counter <= 0:
  93.                 cities_dictionary[list_vacancies[i - 1]["area_name"]] = \
  94.                     (list_vacancies[i - 1]["avg_salary"], counter)
  95.             else:
  96.                 cities_dictionary[list_vacancies[i - 1]["area_name"]] = \
  97.                     (floor(np.average(temporal_list)), counter)
  98.             temporal_list, counter = list(), 0
  99.         else:
  100.             if i == len(list_vacancies) - 1:
  101.                 if counter <= 0:
  102.                     cities_dictionary[list_vacancies[i - 1]["area_name"]] = \
  103.                         (list_vacancies[i - 1]["avg_salary"], counter)
  104.                 else:
  105.                     cities_dictionary[list_vacancies[i - 1]["area_name"]] = \
  106.                         (floor(np.average(temporal_list)), counter)
  107.                 temporal_list, counter = list(), 0
  108.             if list_vacancies[i - 1]["area_name"] != list_vacancies[i]["area_name"]:
  109.                 temporal_list.append(float(list_vacancies[i - 1]["avg_salary"]))
  110.                 counter += 1
  111.                 if counter > 0:
  112.                     cities_dictionary[list_vacancies[i - 1]["area_name"]] = \
  113.                         (floor(np.average(temporal_list)), counter)
  114.                 else:
  115.                     cities_dictionary[list_vacancies[i - 1]["area_name"]] = \
  116.                         (list_vacancies[i - 1]["avg_salary"], counter)
  117.                 temporal_list, counter = list(), 0
  118.             else:
  119.                 temporal_list.append(float(list_vacancies[i - 1]["avg_salary"]))
  120.                 counter += 1
  121.  
  122.     big_cities_dictionary = dict()
  123.     for key, value in cities_dictionary.items():
  124.         if value[1] * 100 / len(list_vacancies) >= 0.9:
  125.             big_cities_dictionary[key] = value
  126.  
  127.     sorted_tuple = sorted(big_cities_dictionary.items(),
  128.                           key=lambda x: (x[1][0], x[0]),
  129.                           reverse=True)
  130.  
  131.     for index, value in enumerate(sorted_tuple):
  132.         try:
  133.             if sorted_tuple[index - 1][1][0] == sorted_tuple[index][1][0]:
  134.                 temp1, temp2 = sorted_tuple[index - 1], sorted_tuple[index]
  135.                 sorted_tuple[index - 1] = temp2
  136.                 sorted_tuple[index] = temp1
  137.         except:
  138.             pass
  139.  
  140.     print(f'Из {len(cities_dictionary)} городов, самые высокие средние ЗП:')
  141.     index = 0
  142.     for key, value in sorted_tuple:
  143.         if index >= 10:
  144.             break
  145.         print(f'    {index + 1}) {key} - средняя зарплата {value[0]} {get_ruble_suffix(value[0])} '
  146.               f'({value[1]} {get_vacancies_suffix(value[1])})')
  147.         index += 1
  148.     print()
  149.  
  150.  
  151. def make_final_list_with_dictionaries(csv_file):
  152.     with open(csv_file, encoding='utf-8-sig') as f:
  153.         csv_reader = reader(f, delimiter=',')
  154.         all_vacancies_list, headline = list(), next(csv_reader)
  155.         html_tags = re.compile('<.*?>')
  156.         for current_vacancy in csv_reader:
  157.             if current_vacancy[headline.index('salary_currency')] != 'RUR':
  158.                 continue
  159.             try:
  160.                 current_vacancy.remove('')
  161.             except ValueError:
  162.                 pass
  163.             for i in range(len(current_vacancy)):
  164.                 if i == headline.index('key_skills'):
  165.                     current_vacancy[i] = current_vacancy[i].strip()
  166.                     current_vacancy[i] = re.split("\n|\n\r", current_vacancy[i])
  167.                 else:
  168.                     current_vacancy[i] = re.sub(html_tags, '', current_vacancy[i]).strip()
  169.                     current_vacancy[i] = ' '.join(current_vacancy[i].split())
  170.  
  171.             if len(current_vacancy) == len(headline):
  172.                 all_vacancies_list.append(to_dictionary(headline, current_vacancy))
  173.  
  174.     print_vacancies_by_salaries(all_vacancies_list, True)
  175.     print_vacancies_by_salaries(all_vacancies_list, False)
  176.     print_top_skills(list_vacancies=all_vacancies_list)
  177.     print_cities_by_salaries(input_list=all_vacancies_list)
  178.  
  179.  
  180. make_final_list_with_dictionaries(input())
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement