Advertisement
VssA

python_urfu2

Nov 17th, 2022 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.78 KB | None | 0 0
  1. import csv
  2. from openpyxl import Workbook
  3. from openpyxl.utils import get_column_letter
  4. from openpyxl.styles import Font, Border, Side
  5.  
  6.  
  7. class InputConnect:
  8. def __init__(self):
  9. self.file_name = input('Введите название файла: ')
  10. self.vacancy_name = input('Введите название профессии: ')
  11.  
  12. dataset = DataSet(self.file_name, self.vacancy_name)
  13. salary_dynamics_city, vacancy_dynamics_year, vacancy_dynamics_salary, \
  14. vacancy_dynamics_years, salary_level, vacancy_share = dataset.get_statistic()
  15. dataset.print_statistic(salary_dynamics_city, vacancy_dynamics_year,
  16. vacancy_dynamics_salary, vacancy_dynamics_years, salary_level, vacancy_share)
  17.  
  18. report = Report(self.vacancy_name, salary_dynamics_city, vacancy_dynamics_year, vacancy_dynamics_salary,
  19. vacancy_dynamics_years, salary_level, vacancy_share)
  20. report.generate_excel()
  21.  
  22.  
  23. class Report:
  24. def __init__(self, vacancy_name, salary_dynamics_city, vacancy_dynamics_year,
  25. vacancy_dynamics_salary, vacancy_dynamics_years, salary_level, vacancy_share):
  26. self.wb = Workbook()
  27. self.vacancy_name = vacancy_name
  28. self.stats1 = salary_dynamics_city
  29. self.stats2 = vacancy_dynamics_year
  30. self.stats3 = vacancy_dynamics_salary
  31. self.stats4 = vacancy_dynamics_years
  32. self.stats5 = salary_level
  33. self.stats6 = vacancy_share
  34.  
  35. def generate_excel(self):
  36. workbook1 = self.wb.active
  37. workbook1.title = 'Статистика по годам'
  38. workbook1.append(['Год', 'Средняя зарплата', 'Средняя зарплата - ' + self.vacancy_name, 'Количество вакансий',
  39. 'Количество вакансий - ' + self.vacancy_name])
  40. for year in self.stats1.keys():
  41. workbook1.append([year, self.stats1[year], self.stats3[year], self.stats2[year], self.stats4[year]])
  42.  
  43. data = [['Год ', 'Средняя зарплата ', ' Средняя зарплата - ' + self.vacancy_name, ' Количество вакансий',
  44. ' Количество вакансий - ' + self.vacancy_name]]
  45. column_widths = []
  46. column_widths = self.make_columns(column_widths, data)
  47.  
  48. for i, column_width in enumerate(column_widths, 1): # ,1 to start at 1
  49. workbook1.column_dimensions[get_column_letter(i)].width = column_width + 2
  50.  
  51. data = [['Город', 'Уровень зарплат', '', 'Город', 'Доля вакансий']]
  52. for (city1, value1), (city2, value2) in zip(self.stats5.items(), self.stats6.items()):
  53. data.append([city1, value1, '', city2, value2])
  54. workbook2 = self.wb.create_sheet('Статистика по городам')
  55. for row in data:
  56. workbook2.append(row)
  57. column_widths = []
  58. column_widths = self.make_columns(column_widths, data)
  59. for i, column_width in enumerate(column_widths, 1): # ,1 to start at 1
  60. workbook2.column_dimensions[get_column_letter(i)].width = column_width + 2
  61.  
  62. font_bold = Font(bold=True)
  63. for col in 'ABCDE':
  64. workbook1[col + '1'].font = font_bold
  65. workbook2[col + '1'].font = font_bold
  66.  
  67. for index, _ in enumerate(self.stats5):
  68. workbook2['E' + str(index + 2)].number_format = '0.00%'
  69.  
  70. thin = Side(border_style='thin', color='00000000')
  71.  
  72. for row in range(len(data)):
  73. for col in 'ABDE':
  74. workbook2[col + str(row + 1)].border = Border(left=thin, bottom=thin, right=thin, top=thin)
  75.  
  76. self.stats1[1] = 1
  77. for row, _ in enumerate(self.stats1):
  78. for col in 'ABCDE':
  79. workbook1[col + str(row + 1)].border = Border(left=thin, bottom=thin, right=thin, top=thin)
  80.  
  81. self.wb.save('report.xlsx')
  82.  
  83. def make_columns(self, column_widths, data):
  84. for row in data:
  85. for i, cell in enumerate(row):
  86. cell = str(cell)
  87. if len(column_widths) > i:
  88. if len(cell) > column_widths[i]:
  89. column_widths[i] = len(cell)
  90. else:
  91. column_widths += [len(cell)]
  92. return column_widths
  93.  
  94.  
  95. class Vacancy:
  96. currency_in_rub = {
  97. "AZN": 35.68,
  98. "BYR": 23.91,
  99. "EUR": 59.90,
  100. "GEL": 21.74,
  101. "KGS": 0.76,
  102. "KZT": 0.13,
  103. "RUR": 1,
  104. "UAH": 1.64,
  105. "USD": 60.66,
  106. "UZS": 0.0055,
  107. }
  108.  
  109. def __init__(self, vacancy):
  110. self.name = vacancy['name']
  111. self.salary_from = int(float(vacancy['salary_from']))
  112. self.salary_to = int(float(vacancy['salary_to']))
  113. self.salary_currency = vacancy['salary_currency']
  114. salary_rub = self.currency_in_rub[self.salary_currency]
  115. salary_average_value = (self.salary_from + self.salary_to) / 2
  116. self.salary_average = salary_rub * salary_average_value
  117. self.area_name = vacancy['area_name']
  118. self.year = int(vacancy['published_at'][:4])
  119.  
  120.  
  121. class DataSet:
  122. def __init__(self, file_name, vacancy_name):
  123. self.file_name = file_name
  124. self.vacancy_name = vacancy_name
  125.  
  126. @staticmethod
  127. def increment(dictionary, key, amount):
  128. if key in dictionary:
  129. dictionary[key] += amount
  130. else:
  131. dictionary[key] = amount
  132.  
  133. @staticmethod
  134. def average(dictionary):
  135. new_dictionary = {}
  136. for key, values in dictionary.items():
  137. new_dictionary[key] = int(sum(values) / len(values))
  138. return new_dictionary
  139.  
  140. def work_with_file(self):
  141. with open(self.file_name, mode='r', encoding='utf-8-sig') as file:
  142. reader = csv.reader(file)
  143. header = next(reader)
  144. header_length = len(header)
  145. for row in reader:
  146. if '' not in row and len(row) == header_length:
  147. yield dict(zip(header, row))
  148.  
  149. def get_statistic(self):
  150. salary = {}
  151. salary_of_vacancy_name = {}
  152. salary_city = {}
  153. count_of_vacancies = 0
  154.  
  155. for vacancy_dictionary in self.work_with_file():
  156. vacancy = Vacancy(vacancy_dictionary)
  157. self.increment(salary, vacancy.year, [vacancy.salary_average])
  158. if vacancy.name.find(self.vacancy_name) != -1:
  159. self.increment(salary_of_vacancy_name, vacancy.year, [vacancy.salary_average])
  160. self.increment(salary_city, vacancy.area_name, [vacancy.salary_average])
  161. count_of_vacancies += 1
  162. vacancies_number = dict([(key, len(value)) for key, value in salary.items()])
  163. vacancies_number_by_name = dict([(key, len(value)) for key, value in salary_of_vacancy_name.items()])
  164. if not salary_of_vacancy_name:
  165. salary_of_vacancy_name = dict([(key, [0]) for key, value in salary.items()])
  166. vacancies_number_by_name = dict([(key, 0) for key, value in vacancies_number.items()])
  167. salary_dynamics_city, salary_level, vacancy_dynamics_salary, vacancy_dynamics_year = self.make_analytics(
  168. count_of_vacancies, salary, salary_city, salary_of_vacancy_name)
  169. return salary_dynamics_city, vacancies_number, vacancy_dynamics_year, \
  170. vacancies_number_by_name, vacancy_dynamics_salary, salary_level
  171.  
  172. def make_analytics(self, count_of_vacancies, salary, salary_city, salary_of_vacancy_name):
  173. salary_dynamics_city = self.average(salary)
  174. vacancy_dynamics_year = self.average(salary_of_vacancy_name)
  175. vacancy_dynamics_salary = self.average(salary_city)
  176. vacancy_dynamics_years = {}
  177. for year, salaries in salary_city.items():
  178. vacancy_dynamics_years[year] = round(len(salaries) / count_of_vacancies, 4)
  179. vacancy_dynamics_years = list(
  180. filter(lambda a: a[-1] >= 0.01, [(key, value) for key, value in vacancy_dynamics_years.items()]))
  181. vacancy_dynamics_years.sort(key=lambda a: a[-1], reverse=True)
  182. salary_level = vacancy_dynamics_years.copy()
  183. vacancy_dynamics_years = dict(vacancy_dynamics_years)
  184. vacancy_dynamics_salary = list(filter(lambda a: a[0] in list(vacancy_dynamics_years.keys()),
  185. [(key, value) for key, value in vacancy_dynamics_salary.items()]))
  186. vacancy_dynamics_salary.sort(key=lambda a: a[-1], reverse=True)
  187. vacancy_dynamics_salary = dict(vacancy_dynamics_salary[:10])
  188. salary_level = dict(salary_level[:10])
  189. return salary_dynamics_city, salary_level, vacancy_dynamics_salary, vacancy_dynamics_year
  190.  
  191. @staticmethod
  192. def print_statistic(salary_dynamics_city, vacancy_dynamics_year, vacancy_dynamics_salary,
  193. vacancy_dynamics_years, salary_level, vacancy_share):
  194. print('Динамика уровня зарплат по годам: {0}'.format(salary_dynamics_city))
  195. print('Динамика количества вакансий по годам: {0}'.format(vacancy_dynamics_year))
  196. print('Динамика уровня зарплат по годам для выбранной профессии: {0}'.format(vacancy_dynamics_salary))
  197. print('Динамика количества вакансий по годам для выбранной профессии: {0}'.format(vacancy_dynamics_years))
  198. print('Уровень зарплат по городам (в порядке убывания): {0}'.format(salary_level))
  199. print('Доля вакансий по городам (в порядке убывания): {0}'.format(vacancy_share))
  200.  
  201.  
  202. if __name__ == '__main__':
  203. InputConnect()
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement