Advertisement
mikhailemv

Untitled

Oct 31st, 2022
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. import csv, os, re
  2. from var_dump import var_dump
  3. from typing import Dict, List, Tuple
  4.  
  5. EMPTY_FILE_STRING = 'Пустой файл'
  6.  
  7.  
  8. class DataSet:
  9.     def __init__(self, file_name: str):
  10.         self.file_name = file_name
  11.         self.vacancies_objects = [Vacancy(vacancy) for vacancy in self.csv_filer(*self.csv_reader(file_name))]
  12.  
  13.     @staticmethod
  14.     def clean_string(html_tags: str) -> str:
  15.         cleaned_string = re.sub('<.*?>', '', html_tags)
  16.         return cleaned_string if '\n' in html_tags else ' '.join(cleaned_string.split())
  17.  
  18.     @staticmethod
  19.     def csv_reader(file_name: str) -> Tuple[List[str], List[List[str]]]:
  20.         reader = csv.reader(open(file_name, encoding='utf_8_sig'))
  21.         all_date = [line for line in reader]
  22.         return all_date[0], all_date[1:]
  23.  
  24.     def csv_filer(self, headlines: List[str], reader: List[List[str]]) -> List[Dict[str, str]]:
  25.         updated_vacancies_list = list(filter(lambda vacancy: (
  26.                 len(vacancy) == len(headlines) and
  27.                 vacancy.count('') == 0), reader))
  28.         return [dict(zip(headlines, map(self.clean_string, vacancy))) for vacancy in updated_vacancies_list]
  29.  
  30.  
  31. class Vacancy:
  32.     def __init__(self, vacancies_list: Dict[str, str]):
  33.         self.name = vacancies_list['name']
  34.         self.description = vacancies_list['description']
  35.         self.key_skills = vacancies_list['key_skills'].split('\n')
  36.         self.experience_id = vacancies_list['experience_id']
  37.         self.premium = vacancies_list['premium']
  38.         self.employer_name = vacancies_list['employer_name']
  39.         self.salary = Salary(salary_from=vacancies_list['salary_from'],
  40.                              salary_to=vacancies_list['salary_to'],
  41.                              salary_gross=vacancies_list['salary_gross'],
  42.                              salary_currency=vacancies_list['salary_currency'])
  43.         self.area_name = vacancies_list['area_name']
  44.         self.published_at = vacancies_list['published_at']
  45.  
  46.  
  47. class Salary:
  48.     def __init__(self, salary_from: str, salary_to: str, salary_gross: str, salary_currency: str):
  49.         self.salary_from = salary_from
  50.         self.salary_to = salary_to
  51.         self.salary_gross = salary_gross
  52.         self.salary_currency = salary_currency
  53.  
  54.  
  55. def exit_file(received_message: str) -> None:
  56.     print(received_message)
  57.     exit()
  58.  
  59.  
  60. csv_name = input('Введите название файла: ')
  61. order_option = input('Введите параметр фильтрации: ')
  62. sort_option = input('Введите параметр сортировки: ')
  63. is_reversed = input('Обратный порядок сортировки (Да / Нет): ')
  64. interval = list(map(int, input('Введите диапазон вывода: ').split()))
  65. needed_columns = input('Введите требуемые столбцы: ')
  66. if os.stat(csv_name).st_size == 0:
  67.     exit_file(EMPTY_FILE_STRING)
  68. var_dump(DataSet(file_name=csv_name))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement