mikhailemv

Untitled

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