-
Notifications
You must be signed in to change notification settings - Fork 97
6211 Васильченко Данила Александрович Лаб.1 Вар.4 #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danonestope
wants to merge
21
commits into
itsecd:main
Choose a base branch
from
danonestope:lab1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
dcdfe3a
Сделал 1 лр
danonestope a51b8a6
Update lab1_var4.py
danonestope d0bb973
Merge branch 'itsecd:main' into main
danonestope 2288362
лаба1
danonestope 3c9f2bf
лаба1
danonestope 00448d8
лаба
danonestope e3a2464
лаба1
danonestope afb6216
лаба1
danonestope abcff57
fixed lab1 ;)
danonestope dfc72b2
lab2
danonestope 95cd68b
лаба1
danonestope 92dc0e1
Merge branch 'main' of https://github.com/danonestope/application-pro…
danonestope 4623a0e
лаба 1
danonestope 47d367d
лаба 2
danonestope 5dee339
лаба 3
danonestope 4979240
лаба 4
danonestope 53d6ebb
лаба 1
danonestope 45f35f8
Delete lab2.md
danonestope d6f6696
Delete lab2_var4.py
danonestope 699f3e6
Delete lab3_var4.py
danonestope c9993b7
Delete lab4_var4.py
danonestope File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| import sys | ||
| import argparse | ||
| import re | ||
| from datetime import datetime | ||
| from typing import List, Optional, Tuple | ||
|
|
||
|
|
||
| class Person: | ||
| def __init__(self, last_name: str, first_name: str, gender: str, | ||
| birth_date: str, contact: str, city: str) -> None: | ||
| self.last_name = last_name | ||
| self.first_name = first_name | ||
| self.gender = gender | ||
| self.birth_date = birth_date | ||
| self.contact = contact | ||
| self.city = city | ||
|
|
||
| def get_birth_date_object(self) -> Optional[datetime]: | ||
| try: | ||
| # Паттерн для даты в форматах ДД/ММ/ГГГГ, ДД.ММ.ГГГГ, ДД-ММ-ГГГГ | ||
| pattern = r'(\d{1,2})[/\.\-](\d{1,2})[/\.\-](\d{4})' | ||
| match = re.match(pattern, self.birth_date) | ||
|
|
||
| if match: | ||
| day, month, year = map(int, match.groups()) | ||
| return datetime(year, month, day) | ||
| return None | ||
| except (ValueError, AttributeError) as e: | ||
| raise ValueError(f"Неверный формат даты: {self.birth_date}") from e | ||
|
|
||
| def calculate_age(self) -> Optional[int]: | ||
| """Вычисляет возраст""" | ||
| try: | ||
| birth_date = self.get_birth_date_object() | ||
| if not birth_date: | ||
| return None | ||
|
|
||
| today = datetime.now() | ||
| age = today.year - birth_date.year | ||
|
|
||
| # Проверяем, был ли уже день рождения в этом году | ||
| if today.month < birth_date.month or (today.month == birth_date.month and today.day < birth_date.day): | ||
| age -= 1 | ||
|
|
||
| return age | ||
| except Exception as e: | ||
| raise ValueError(f"Ошибка при вычислении возраста для {self.last_name} {self.first_name}") from e | ||
|
|
||
| def __str__(self) -> str: | ||
| try: | ||
| age = self.calculate_age() | ||
| age_str = f", {age} лет" if age else "" | ||
| return f"{self.last_name} {self.first_name}{age_str}, {self.city}" | ||
| except Exception: | ||
| return f"{self.last_name} {self.first_name}, {self.city} (возраст не определен)" | ||
|
|
||
|
|
||
| def read_people_from_file(filename: str) -> List[Person]: | ||
| try: | ||
| with open(filename, 'r', encoding='utf-8') as file: | ||
| content = file.read() | ||
|
|
||
| people = [] | ||
| profiles = content.strip().split('\n\n') | ||
|
|
||
| for profile in profiles: | ||
| if not profile.strip(): | ||
| continue | ||
|
|
||
| data = {} | ||
| # Паттерн для поиска "Поле: значение" (игнорируем пробелы вокруг двоеточия) | ||
| pattern = r'^\s*([^:\n]+?)\s*:\s*(.+?)\s*$' | ||
|
|
||
| for line in profile.split('\n'): | ||
| match = re.match(pattern, line) | ||
| if match: | ||
| key, value = match.groups() | ||
| data[key.strip().lower()] = value.strip() | ||
|
|
||
| # Создаем человека если есть все необходимые поля | ||
| if all(field in data for field in ['фамилия', 'имя', 'дата рождения']): | ||
| person = Person( | ||
| last_name=data['фамилия'], | ||
| first_name=data['имя'], | ||
| gender=data.get('пол', ''), | ||
| birth_date=data['дата рождения'], | ||
| contact=data.get('номер телефона или email', ''), | ||
| city=data.get('город', '') | ||
| ) | ||
| people.append(person) | ||
|
|
||
| if not people: | ||
| raise ValueError(f"В файле {filename} не найдено валидных анкет") | ||
|
|
||
| return people | ||
|
|
||
| except FileNotFoundError as e: | ||
| raise FileNotFoundError(f"Файл {filename} не найден") from e | ||
| except PermissionError as e: | ||
| raise PermissionError(f"Нет прав для чтения файла {filename}") from e | ||
| except UnicodeDecodeError as e: | ||
| raise UnicodeDecodeError(f"Ошибка кодировки файла {filename}") from e | ||
| except Exception as e: | ||
| raise Exception(f"Ошибка при чтении файла {filename}: {e}") from e | ||
|
|
||
|
|
||
| def find_oldest_and_youngest(people: List[Person]) -> Tuple[Optional[Person], Optional[Person]]: | ||
| """Находит самого старого и самого молодого человека""" | ||
| if not people: | ||
| return None, None | ||
|
|
||
| try: | ||
| # Фильтруем людей с валидными датами | ||
| people_with_dates = [] | ||
| for person in people: | ||
| try: | ||
| if person.get_birth_date_object(): | ||
| people_with_dates.append(person) | ||
| except ValueError: | ||
| # Пропускаем людей с невалидными датами | ||
| continue | ||
|
|
||
| if not people_with_dates: | ||
| return None, None | ||
|
|
||
| # Сортируем по дате рождения | ||
| people_sorted = sorted(people_with_dates, key=lambda p: p.get_birth_date_object()) | ||
|
|
||
| oldest = people_sorted[0] # Первый в отсортированном списке (самый старый) | ||
| youngest = people_sorted[-1] # Последный в отсортированном списке (самый молодой) | ||
|
|
||
| return oldest, youngest | ||
| except Exception as e: | ||
| raise Exception("Ошибка при определении самого старого и самого молодого человека") from e | ||
|
|
||
|
|
||
| def print_results(oldest: Optional[Person], youngest: Optional[Person], input_file: str) -> None: | ||
| """Печатает результаты в консоль""" | ||
| print(f"Обработан файл: {input_file}") | ||
| print("\n" + "=" * 40) | ||
| print("РЕЗУЛЬТАТЫ:") | ||
| print("=" * 40) | ||
|
|
||
| if oldest and youngest: | ||
| print(f"\nСамый старший человек:") | ||
| print(f" {oldest}") | ||
| print(f" Дата рождения: {oldest.birth_date}") | ||
|
|
||
| print(f"\nСамый младший человек:") | ||
| print(f" {youngest}") | ||
| print(f" Дата рождения: {youngest.birth_date}") | ||
|
|
||
| if oldest == youngest: | ||
| print("\n⚠️ Это один и тот же человек!") | ||
| else: | ||
| print("\nНе удалось определить возраст людей.") | ||
|
|
||
|
|
||
| def main() -> None: | ||
| """Основная функция программы""" | ||
| try: | ||
| parser = argparse.ArgumentParser(description="Выгрузка пиплов") | ||
| parser.add_argument('input_file', type=str, help="Вводные данные") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # Чтение и обработка данных | ||
| people = read_people_from_file(args.input_file) | ||
| print(f"Найдено анкет: {len(people)}") | ||
|
|
||
| oldest, youngest = find_oldest_and_youngest(people) | ||
|
|
||
| # Вывод результатов в консоль | ||
| print_results(oldest, youngest, args.input_file) | ||
|
|
||
| except FileNotFoundError as e: | ||
| print(f"Ошибка: {e}") | ||
| print("Убедитесь, что файл data.txt находится в той же папке, что и программа") | ||
| sys.exit(1) | ||
| except PermissionError as e: | ||
| print(f"Ошибка доступа: {e}") | ||
| sys.exit(1) | ||
| except UnicodeDecodeError as e: | ||
| print(f"Ошибка кодировки: {e}") | ||
| sys.exit(1) | ||
| except ValueError as e: | ||
| print(f"Ошибка данных: {e}") | ||
| sys.exit(1) | ||
| except Exception as e: | ||
| print(f"Неожиданная ошибка: {e}") | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Неправильный порядок импортов (см. PEP8)