From 4638ea5ee8b4b31e95e91748691e597c8215626a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=96=D1=83?= =?UTF-8?q?=D1=80=D0=B8=D0=BD?= <2010zalex@gmail.com> Date: Wed, 29 Apr 2020 02:49:11 +0300 Subject: [PATCH 1/6] Add HW2. All tests passed --- homework/config.py | 9 +- homework/patient.py | 238 ++++++++++++++++++++++++++++++++++++++++-- info_log.txt | 162 ++++++++++++++++++++++++++++ tests/test_patient.py | 3 +- 4 files changed, 397 insertions(+), 15 deletions(-) create mode 100644 info_log.txt diff --git a/homework/config.py b/homework/config.py index 955b991..982f963 100644 --- a/homework/config.py +++ b/homework/config.py @@ -1,11 +1,10 @@ -GOOD_LOG_FILE = "good_log.txt" +GOOD_LOG_FILE = "info_log.txt" ERROR_LOG_FILE = "error_log.txt" -CSV_PATH = "csv.csv" -PHONE_FORMAT = "79160000000" # Здесь запишите телефон +7-916-000-00-00 в том формате, в котором вы храните телефоны +CSV_PATH = "data.csv" +PHONE_FORMAT = "+79160000000" # Здесь запишите телефон +7-916-000-00-00 в том формате, в котором вы храните телефоны PASSPORT_TYPE = "паспорт" # тип документа, когда он паспорт -PASSPORT_FORMAT = "0000 000000" # Здесь запишите номер парспорта 0000 000000 в том формате, в котором вы его храните - +PASSPORT_FORMAT = "00 00 000000" # Здесь запишите номер парспорта 0000 000000 в том формате, в котором вы его храните INTERNATIONAL_PASSPORT_TYPE = "заграничный паспорт" # тип документа, если это загран INTERNATIONAL_PASSPORT_FORMAT = "00 0000000" # формат хранения заграна для номера 00 0000000 diff --git a/homework/patient.py b/homework/patient.py index dad2526..fe6f459 100644 --- a/homework/patient.py +++ b/homework/patient.py @@ -1,17 +1,237 @@ +import datetime +import logging +import pandas as pnd + +logger_info = logging.getLogger("patient_log_info") +logger_info.setLevel(logging.INFO) +logger_error = logging.getLogger("patient_log_error") +logger_error.setLevel(logging.ERROR) + +formatter = logging.Formatter("%(filename)s[LINE:%(lineno)d]# %(levelname)-8s [%(asctime)s] %(message)s") + +error_logs = logging.FileHandler('error_log.txt', 'a', 'utf-8') +error_logs.setFormatter(formatter) + +info_logs = logging.FileHandler('info_log.txt', 'a', 'utf-8') +info_logs.setFormatter(formatter) + +logger_info.addHandler(info_logs) +logger_error.addHandler(error_logs) + + +class DataCheck: + def _name_check(self, name): + if not name.isalpha(): + logger_error.error("Name or surname contains invalid characters") + raise ValueError("Name or surname contains invalid characters") + return name.capitalize() + + def _birth_check(self, born): + if len(born) != 10: + logger_error.error("Incorrect date length") + raise ValueError("Incorrect date length") + born = born[:4] + '-' + born[5:7] + '-' + born[8:] + for k, i in enumerate(born): + if i.isdigit() and (0 <= k <= 3 or k == 5 or k == 6 or k == 8 or k == 9): + continue + elif (k == 4 or k == 7) and i == '-': + continue + else: + logger_error.error("Date contains invalid characters") + raise ValueError("Date contains invalid characters") + if str(datetime.date.today()) >= born: + return born + else: + logger_error.error("Date does not exist yet") + raise ValueError("Date does not exist yet") + + def _phone_check(self, phone): + phone = phone.replace('+', '') + phone = phone.replace('(', '') + phone = phone.replace(')', '') + phone = phone.replace('-', '') + phone = phone.replace(' ', '') + if len(phone) == 11: + if phone.isdigit(): + return "+7" + phone[1:] + else: + logger_error.error("Phone number contains invalid characters") + raise ValueError("Phone number contains invalid characters") + else: + logger_error.error("Incorrect phone number length") + raise ValueError("Incorrect phone number length") + + def _doc_check(self, doc): + if doc.lower() != "паспорт" and doc.lower() != "загранпаспорт" and doc.lower() != "заграничный паспорт" and \ + doc.lower() != "водительские права" and doc.lower() != "водительское удостоверение": + logger_error.error("Incorrect document") + raise ValueError("Incorrect document") + return doc.lower() + + def _doc_id_check(self, doc_id): + doc_id = doc_id.replace(' ', '') + doc_id = doc_id.replace('-', '') + doc_id = doc_id.replace('/', '') + doc_id = doc_id.replace('\\', '') + if doc_id.isdigit(): + if len(doc_id) == 10: + return doc_id[:2] + ' ' + doc_id[2:4] + ' ' + doc_id[4:] + elif len(doc_id) == 9: + return doc_id[:2] + ' ' + doc_id[2:] + else: + logger_error.error("Incorrect document's number length") + raise ValueError("Incorrect document's number length") + else: + logger_error.error("Document number contains invalid characters") + raise ValueError("Document number contains invalid characters") + + +class DataAccess(DataCheck): + def __init__(self, name='атрибут', data_get=None, data_set=None, data_del=None): + self.data_get = data_get + self.data_set = data_set + self.data_del = data_del + self.name = name + + def __get__(self, obj, objtype): + return self.data_get(obj, self.name) + + def __set__(self, obj, val): + if type(val) != str: + logger_error.error("Incorrect type of input data") + raise TypeError("Incorrect type of input data") + if self.name == 'first_name' or self.name == 'last_name': + self.data_set(obj, self._name_check(val), self.name) + elif self.name == 'birth_date': + self.data_set(obj, self._birth_check(val), self.name) + elif self.name == 'phone': + self.data_set(obj, self._phone_check(val), self.name) + elif self.name == 'document_type': + self.data_set(obj, self._doc_check(val), self.name) + elif self.name == 'document_id': + self.data_set(obj, self._doc_id_check(val), self.name) + + def __delete__(self, obj): + self.data_del(obj, self.name) + + class Patient: - def __init__(self, *args, **kwargs): - pass + def get_field(self, key): + if key == 'first_name': + return self._first_name + elif key == 'last_name': + return self._last_name + elif key == 'birth_date': + return self._birth_date + elif key == 'phone': + return self._phone + elif key == 'document_type': + return self._document_type + elif key == 'document_id': + return self._document_id - def create(*args, **kwargs): - raise NotImplementedError() + def set_field(self, value, key): + create_flag = True + if not hasattr(self, '_' + key): + create_flag = False + if key == 'first_name' and not hasattr(self, '_first_name'): + self._first_name = value + elif key == 'last_name' and not hasattr(self, '_last_name'): + self._last_name = value + elif (key == 'first_name' and hasattr(self, '_first_name')) or ( + key == 'last_name' and hasattr(self, '_last_name')): + logger_error.error("Fields \"first_name\" and \"last_name\" mustn't be changed") + raise AttributeError("Fields \"first_name\" and \"last_name\" mustn't be changed") + elif key == 'birth_date': + self._birth_date = value + elif key == 'phone': + self._phone = value + elif key == 'document_type': + self._document_type = value + elif key == 'document_id': + if ((self._document_type == "паспорт" or self._document_type == "водительские права" or + self._document_type == "водительское удостоверение") and len(value) == 12) or \ + ((self._document_type == "загранпаспорт" or self._document_type == "заграничный паспорт") + and len(value) == 10): + self._document_id = value + else: + logger_error.error("Number of characters does not match the type of document") + raise Exception("Number of characters does not match the type of document") + if create_flag: + logger_info.info("Field " + key + " were updated") + + def del_field(self, key): + if key == 'first_name': + del self._first_name + elif key == 'last_name': + del self._last_name + elif key == 'birth_date': + del self._birth_date + elif key == 'phone': + del self._phone + elif key == 'document_type': + del self._document_type + elif key == 'document_id': + del self._document_id + + first_name = DataAccess('first_name', get_field, set_field, del_field) + last_name = DataAccess('last_name', get_field, set_field, del_field) + birth_date = DataAccess('birth_date', get_field, set_field, del_field) + phone = DataAccess('phone', get_field, set_field, del_field) + document_type = DataAccess('document_type', get_field, set_field, del_field) + document_id = DataAccess('document_id', get_field, set_field, del_field) + + def __init__(self, name, surname, born, phone, doc, doc_id): + self.first_name = name + self.last_name = surname + self.birth_date = born + self.phone = phone + self.document_type = doc + self.document_id = doc_id + logger_info.info("Patient added") + + def __str__(self): + return (self.first_name + ' ' + self.last_name + ' ' + self.birth_date + ' ' + self.phone + ' ' + + self.document_type + ' ' + self.document_id) def save(self): - pass + df = pnd.DataFrame({'first name': [self.first_name], + 'last name': [self.last_name], + 'birth date': [self.birth_date], + 'phone': [self.phone], + 'document type': [self.document_type], + 'document id': [self.document_id]}) + df.to_csv('data.csv', '|', header=False, index=False, mode='a') + logger_info.info("Patient saved") + + @staticmethod + def create(name, surname, born, phone, doc, number): + return Patient(name, surname, born, phone, doc, number) class PatientCollection: - def __init__(self, log_file): - pass + def __init__(self, path_to_file): + if type(path_to_file) != str: + logger_error.error("Incorrect type of input path") + raise TypeError("Incorrect type of input path") + self.path = path_to_file + + def __iter__(self): + try: + pnd.read_csv(self.path, sep='|', header=None, dtype=str) + except pnd.errors.EmptyDataError: + return + for i in range(0, len(pnd.read_csv(self.path, sep='|', header=None, dtype=str).index)): + try: + pnd.read_csv(self.path, sep='|', header=None, dtype=str) + except pnd.errors.EmptyDataError: + return + yield Patient(*pnd.read_csv(self.path, sep='|', header=None, dtype=str).iloc[i]) - def limit(self, n): - raise NotImplementedError() + def limit(self, limit_val): + for i in range(0, limit_val): + try: + pnd.read_csv(self.path, sep='|', header=None, nrows=limit_val, dtype=str) + except pnd.errors.EmptyDataError: + return + yield Patient(*pnd.read_csv(self.path, sep='|', header=None, nrows=limit_val, dtype=str).iloc[i]) diff --git a/info_log.txt b/info_log.txt new file mode 100644 index 0000000..75d4062 --- /dev/null +++ b/info_log.txt @@ -0,0 +1,162 @@ +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,949] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:56,951] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,951] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:56,953] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,953] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:56,955] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,955] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:56,956] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,956] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:56,958] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,958] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:56,960] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,960] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:56,961] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,961] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:56,963] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,963] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:56,965] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,965] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:56,966] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,966] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:56,968] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,968] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:56,969] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,969] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:56,971] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,977] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,977] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,980] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,980] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,982] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,982] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,985] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,985] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,988] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,988] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,990] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,990] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,993] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,993] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,996] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,996] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,998] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:56,999] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,001] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,001] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,004] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,004] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,007] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,007] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,009] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,009] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,011] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,013] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,013] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,014] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,014] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,016] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,016] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,017] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,018] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,019] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,019] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,021] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,021] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,022] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,022] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,024] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,024] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,025] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,025] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,027] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,027] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,028] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,029] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,030] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,030] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,031] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,034] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,035] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,037] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,037] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,040] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,040] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,043] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,043] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,045] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,045] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,048] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,048] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,051] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,051] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,053] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,053] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,055] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,057] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,057] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,058] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,058] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,060] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,060] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,061] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,062] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,063] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,063] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,065] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,065] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,066] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,066] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,068] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,068] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,069] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,070] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,071] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,071] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,073] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,073] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,075] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,075] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,076] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,079] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,082] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,085] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,087] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,091] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,093] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,096] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,099] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,102] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,105] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,108] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,110] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,113] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,113] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,115] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,117] Patient added +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,119] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,121] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,121] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,123] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,123] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,124] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,124] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,126] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,126] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,127] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,127] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,129] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,129] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,130] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,130] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,132] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,132] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,133] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,133] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,135] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,135] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,136] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,137] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,138] Patient saved +patient.py[LINE:191]# INFO [2020-04-29 02:47:57,138] Patient added +patient.py[LINE:205]# INFO [2020-04-29 02:47:57,139] Patient saved diff --git a/tests/test_patient.py b/tests/test_patient.py index a442599..fe5f507 100644 --- a/tests/test_patient.py +++ b/tests/test_patient.py @@ -2,7 +2,7 @@ import os from datetime import datetime import itertools - +import logging import pytest from homework.config import GOOD_LOG_FILE, ERROR_LOG_FILE, CSV_PATH, PHONE_FORMAT, PASSPORT_TYPE, PASSPORT_FORMAT, \ @@ -39,6 +39,7 @@ def setup_module(__main__): def teardown_module(__name__): for file in [GOOD_LOG_FILE, ERROR_LOG_FILE, CSV_PATH]: + logging.shutdown() os.remove(file) From 0bac75c7bdd4195851d76216dc4078f45897b948 Mon Sep 17 00:00:00 2001 From: N0ktis <45037874+N0ktis@users.noreply.github.com> Date: Wed, 29 Apr 2020 02:54:25 +0300 Subject: [PATCH 2/6] Delete info_log.txt --- info_log.txt | 162 --------------------------------------------------- 1 file changed, 162 deletions(-) delete mode 100644 info_log.txt diff --git a/info_log.txt b/info_log.txt deleted file mode 100644 index 75d4062..0000000 --- a/info_log.txt +++ /dev/null @@ -1,162 +0,0 @@ -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,949] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:56,951] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,951] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:56,953] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,953] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:56,955] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,955] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:56,956] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,956] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:56,958] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,958] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:56,960] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,960] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:56,961] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,961] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:56,963] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,963] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:56,965] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,965] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:56,966] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,966] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:56,968] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,968] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:56,969] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,969] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:56,971] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,977] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,977] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,980] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,980] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,982] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,982] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,985] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,985] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,988] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,988] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,990] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,990] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,993] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,993] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,996] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,996] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,998] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:56,999] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,001] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,001] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,004] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,004] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,007] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,007] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,009] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,009] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,011] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,013] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,013] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,014] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,014] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,016] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,016] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,017] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,018] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,019] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,019] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,021] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,021] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,022] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,022] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,024] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,024] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,025] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,025] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,027] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,027] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,028] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,029] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,030] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,030] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,031] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,034] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,035] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,037] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,037] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,040] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,040] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,043] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,043] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,045] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,045] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,048] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,048] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,051] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,051] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,053] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,053] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,055] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,057] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,057] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,058] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,058] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,060] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,060] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,061] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,062] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,063] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,063] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,065] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,065] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,066] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,066] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,068] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,068] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,069] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,070] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,071] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,071] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,073] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,073] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,075] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,075] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,076] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,079] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,082] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,085] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,087] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,091] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,093] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,096] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,099] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,102] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,105] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,108] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,110] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,113] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,113] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,115] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,117] Patient added -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,119] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,121] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,121] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,123] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,123] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,124] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,124] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,126] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,126] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,127] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,127] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,129] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,129] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,130] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,130] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,132] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,132] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,133] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,133] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,135] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,135] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,136] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,137] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,138] Patient saved -patient.py[LINE:191]# INFO [2020-04-29 02:47:57,138] Patient added -patient.py[LINE:205]# INFO [2020-04-29 02:47:57,139] Patient saved From c2a30edfb48c768fc5c08c6b9438eb832390de63 Mon Sep 17 00:00:00 2001 From: N0ktis <45037874+N0ktis@users.noreply.github.com> Date: Wed, 29 Apr 2020 18:12:08 +0300 Subject: [PATCH 3/6] Modified PatientCollection. --- homework/patient.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/homework/patient.py b/homework/patient.py index fe6f459..d843ad2 100644 --- a/homework/patient.py +++ b/homework/patient.py @@ -218,20 +218,15 @@ def __init__(self, path_to_file): def __iter__(self): try: - pnd.read_csv(self.path, sep='|', header=None, dtype=str) + for i in range(0, len(pnd.read_csv(self.path, sep='|', header=None, dtype=str).index)): + yield Patient(*pnd.read_csv(self.path, sep='|', header=None, dtype=str).iloc[i]) except pnd.errors.EmptyDataError: return - for i in range(0, len(pnd.read_csv(self.path, sep='|', header=None, dtype=str).index)): - try: - pnd.read_csv(self.path, sep='|', header=None, dtype=str) - except pnd.errors.EmptyDataError: - return - yield Patient(*pnd.read_csv(self.path, sep='|', header=None, dtype=str).iloc[i]) + def limit(self, limit_val): - for i in range(0, limit_val): - try: - pnd.read_csv(self.path, sep='|', header=None, nrows=limit_val, dtype=str) - except pnd.errors.EmptyDataError: - return - yield Patient(*pnd.read_csv(self.path, sep='|', header=None, nrows=limit_val, dtype=str).iloc[i]) + try: + for i in range(0, limit_val): + yield Patient(*pnd.read_csv(self.path, sep='|', header=None, nrows=limit_val, dtype=str).iloc[i]) + except pnd.errors.EmptyDataError: + return From d0ec45c3c81b30d83ef83c65f2ce71422d776743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=96=D1=83?= =?UTF-8?q?=D1=80=D0=B8=D0=BD?= <2010zalex@gmail.com> Date: Sat, 2 May 2020 14:48:27 +0300 Subject: [PATCH 4/6] Delete class DataCheck. Add constants. Modified descriptor --- homework/patient.py | 180 ++++++++++++++++++++++---------------------- 1 file changed, 91 insertions(+), 89 deletions(-) diff --git a/homework/patient.py b/homework/patient.py index d843ad2..22eb7f6 100644 --- a/homework/patient.py +++ b/homework/patient.py @@ -18,79 +18,90 @@ logger_info.addHandler(info_logs) logger_error.addHandler(error_logs) - -class DataCheck: - def _name_check(self, name): - if not name.isalpha(): - logger_error.error("Name or surname contains invalid characters") - raise ValueError("Name or surname contains invalid characters") - return name.capitalize() - - def _birth_check(self, born): - if len(born) != 10: - logger_error.error("Incorrect date length") - raise ValueError("Incorrect date length") - born = born[:4] + '-' + born[5:7] + '-' + born[8:] - for k, i in enumerate(born): - if i.isdigit() and (0 <= k <= 3 or k == 5 or k == 6 or k == 8 or k == 9): - continue - elif (k == 4 or k == 7) and i == '-': - continue - else: - logger_error.error("Date contains invalid characters") - raise ValueError("Date contains invalid characters") - if str(datetime.date.today()) >= born: - return born +PASSPORT = 'паспорт' +INTERNATIONAL_PASS_1 = 'загранпаспорт' +INTERNATIONAL_PASS_2 = 'заграничный паспорт' +DRIVER_LICENSE_1 = 'водительское удостоверение' +DRIVER_LICENSE_2 = 'водительские права' + + +def name_check(name): + if not name.isalpha(): + logger_error.error("Name or surname contains invalid characters") + raise ValueError("Name or surname contains invalid characters") + return name.capitalize() + + +def birth_check(born): + if len(born) != 10: + logger_error.error("Incorrect date length") + raise ValueError("Incorrect date length") + born = born[:4] + '-' + born[5:7] + '-' + born[8:] + for k, i in enumerate(born): + if i.isdigit() and (0 <= k <= 3 or k == 5 or k == 6 or k == 8 or k == 9): + continue + elif (k == 4 or k == 7) and i == '-': + continue else: - logger_error.error("Date does not exist yet") - raise ValueError("Date does not exist yet") - - def _phone_check(self, phone): - phone = phone.replace('+', '') - phone = phone.replace('(', '') - phone = phone.replace(')', '') - phone = phone.replace('-', '') - phone = phone.replace(' ', '') - if len(phone) == 11: - if phone.isdigit(): - return "+7" + phone[1:] - else: - logger_error.error("Phone number contains invalid characters") - raise ValueError("Phone number contains invalid characters") + logger_error.error("Date contains invalid characters") + raise ValueError("Date contains invalid characters") + if str(datetime.date.today()) >= born: + return born + else: + logger_error.error("Date does not exist yet") + raise ValueError("Date does not exist yet") + + +def phone_check(phone): + phone = phone.replace('+', '') + phone = phone.replace('(', '') + phone = phone.replace(')', '') + phone = phone.replace('-', '') + phone = phone.replace(' ', '') + if len(phone) == 11: + if phone.isdigit(): + return "+7" + phone[1:] else: - logger_error.error("Incorrect phone number length") - raise ValueError("Incorrect phone number length") - - def _doc_check(self, doc): - if doc.lower() != "паспорт" and doc.lower() != "загранпаспорт" and doc.lower() != "заграничный паспорт" and \ - doc.lower() != "водительские права" and doc.lower() != "водительское удостоверение": - logger_error.error("Incorrect document") - raise ValueError("Incorrect document") - return doc.lower() - - def _doc_id_check(self, doc_id): - doc_id = doc_id.replace(' ', '') - doc_id = doc_id.replace('-', '') - doc_id = doc_id.replace('/', '') - doc_id = doc_id.replace('\\', '') - if doc_id.isdigit(): - if len(doc_id) == 10: - return doc_id[:2] + ' ' + doc_id[2:4] + ' ' + doc_id[4:] - elif len(doc_id) == 9: - return doc_id[:2] + ' ' + doc_id[2:] - else: - logger_error.error("Incorrect document's number length") - raise ValueError("Incorrect document's number length") + logger_error.error("Phone number contains invalid characters") + raise ValueError("Phone number contains invalid characters") + else: + logger_error.error("Incorrect phone number length") + raise ValueError("Incorrect phone number length") + + +def doc_check(doc): + if doc.lower() != PASSPORT and doc.lower() != INTERNATIONAL_PASS_1 and doc.lower() != INTERNATIONAL_PASS_2 and \ + doc.lower() != DRIVER_LICENSE_1 and doc.lower() != DRIVER_LICENSE_2: + logger_error.error("Incorrect document") + raise ValueError("Incorrect document") + return doc.lower() + + +def doc_id_check(doc_id): + doc_id = doc_id.replace(' ', '') + doc_id = doc_id.replace('-', '') + doc_id = doc_id.replace('/', '') + doc_id = doc_id.replace('\\', '') + if doc_id.isdigit(): + if len(doc_id) == 10: + return doc_id[:2] + ' ' + doc_id[2:4] + ' ' + doc_id[4:] + elif len(doc_id) == 9: + return doc_id[:2] + ' ' + doc_id[2:] else: - logger_error.error("Document number contains invalid characters") - raise ValueError("Document number contains invalid characters") + logger_error.error("Incorrect document's number length") + raise ValueError("Incorrect document's number length") + else: + logger_error.error("Document number contains invalid characters") + raise ValueError("Document number contains invalid characters") -class DataAccess(DataCheck): - def __init__(self, name='атрибут', data_get=None, data_set=None, data_del=None): +# Descriptor +class DataAccess: + def __init__(self, name='атрибут', data_get=None, data_set=None, data_del=None, data_check=None): self.data_get = data_get self.data_set = data_set self.data_del = data_del + self.data_check = data_check self.name = name def __get__(self, obj, objtype): @@ -100,16 +111,7 @@ def __set__(self, obj, val): if type(val) != str: logger_error.error("Incorrect type of input data") raise TypeError("Incorrect type of input data") - if self.name == 'first_name' or self.name == 'last_name': - self.data_set(obj, self._name_check(val), self.name) - elif self.name == 'birth_date': - self.data_set(obj, self._birth_check(val), self.name) - elif self.name == 'phone': - self.data_set(obj, self._phone_check(val), self.name) - elif self.name == 'document_type': - self.data_set(obj, self._doc_check(val), self.name) - elif self.name == 'document_id': - self.data_set(obj, self._doc_id_check(val), self.name) + self.data_set(obj, self.data_check(val), self.name) def __delete__(self, obj): self.data_del(obj, self.name) @@ -149,9 +151,9 @@ def set_field(self, value, key): elif key == 'document_type': self._document_type = value elif key == 'document_id': - if ((self._document_type == "паспорт" or self._document_type == "водительские права" or - self._document_type == "водительское удостоверение") and len(value) == 12) or \ - ((self._document_type == "загранпаспорт" or self._document_type == "заграничный паспорт") + if ((self._document_type == PASSPORT or self._document_type == DRIVER_LICENSE_1 or + self._document_type == DRIVER_LICENSE_2) and len(value) == 12) or \ + ((self._document_type == INTERNATIONAL_PASS_1 or self._document_type == INTERNATIONAL_PASS_2) and len(value) == 10): self._document_id = value else: @@ -174,12 +176,12 @@ def del_field(self, key): elif key == 'document_id': del self._document_id - first_name = DataAccess('first_name', get_field, set_field, del_field) - last_name = DataAccess('last_name', get_field, set_field, del_field) - birth_date = DataAccess('birth_date', get_field, set_field, del_field) - phone = DataAccess('phone', get_field, set_field, del_field) - document_type = DataAccess('document_type', get_field, set_field, del_field) - document_id = DataAccess('document_id', get_field, set_field, del_field) + first_name = DataAccess('first_name', get_field, set_field, del_field, name_check) + last_name = DataAccess('last_name', get_field, set_field, del_field, name_check) + birth_date = DataAccess('birth_date', get_field, set_field, del_field, birth_check) + phone = DataAccess('phone', get_field, set_field, del_field, phone_check) + document_type = DataAccess('document_type', get_field, set_field, del_field, doc_check) + document_id = DataAccess('document_id', get_field, set_field, del_field, doc_id_check) def __init__(self, name, surname, born, phone, doc, doc_id): self.first_name = name @@ -223,10 +225,10 @@ def __iter__(self): except pnd.errors.EmptyDataError: return - def limit(self, limit_val): - try: - for i in range(0, limit_val): + + for i in range(0, limit_val): + try: yield Patient(*pnd.read_csv(self.path, sep='|', header=None, nrows=limit_val, dtype=str).iloc[i]) - except pnd.errors.EmptyDataError: - return + except pnd.errors.EmptyDataError: + return \ No newline at end of file From 180dee14ee873e688ae7705eb1acaab4df7f4f3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=96=D1=83?= =?UTF-8?q?=D1=80=D0=B8=D0=BD?= <2010zalex@gmail.com> Date: Sat, 2 May 2020 23:21:16 +0300 Subject: [PATCH 5/6] Move logger to decorator --- homework/patient.py | 68 ++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/homework/patient.py b/homework/patient.py index 22eb7f6..3dbdf1c 100644 --- a/homework/patient.py +++ b/homework/patient.py @@ -1,6 +1,7 @@ import datetime import logging import pandas as pnd +from functools import wraps logger_info = logging.getLogger("patient_log_info") logger_info.setLevel(logging.INFO) @@ -25,16 +26,45 @@ DRIVER_LICENSE_2 = 'водительские права' +def logger_decorator_maker(id=None): + def logger_decorator(fun): + @wraps(fun) + def wrapper(value, *args): + try: + checked_value = fun(value, *args) + except ValueError as error: + logger_error.error(error.args[0]) + raise ValueError(error.args[0]) + except TypeError as error: + logger_error.error(error.args[0]) + raise TypeError(error.args[0]) + except AttributeError as error: + logger_error.error(error.args[0]) + raise AttributeError(error.args[0]) + else: + if id == 'init': + logger_info.info("Patient added") + elif id == 'patient_set' and checked_value: + logger_info.info("Field " + args[1] + " were updated") + elif id == 'save': + logger_info.info("Patient saved") + return checked_value + + return wrapper + + return logger_decorator + + +@logger_decorator_maker() def name_check(name): if not name.isalpha(): - logger_error.error("Name or surname contains invalid characters") raise ValueError("Name or surname contains invalid characters") return name.capitalize() +@logger_decorator_maker() def birth_check(born): if len(born) != 10: - logger_error.error("Incorrect date length") raise ValueError("Incorrect date length") born = born[:4] + '-' + born[5:7] + '-' + born[8:] for k, i in enumerate(born): @@ -43,15 +73,14 @@ def birth_check(born): elif (k == 4 or k == 7) and i == '-': continue else: - logger_error.error("Date contains invalid characters") raise ValueError("Date contains invalid characters") if str(datetime.date.today()) >= born: return born else: - logger_error.error("Date does not exist yet") raise ValueError("Date does not exist yet") +@logger_decorator_maker() def phone_check(phone): phone = phone.replace('+', '') phone = phone.replace('(', '') @@ -62,21 +91,20 @@ def phone_check(phone): if phone.isdigit(): return "+7" + phone[1:] else: - logger_error.error("Phone number contains invalid characters") raise ValueError("Phone number contains invalid characters") else: - logger_error.error("Incorrect phone number length") raise ValueError("Incorrect phone number length") +@logger_decorator_maker() def doc_check(doc): if doc.lower() != PASSPORT and doc.lower() != INTERNATIONAL_PASS_1 and doc.lower() != INTERNATIONAL_PASS_2 and \ doc.lower() != DRIVER_LICENSE_1 and doc.lower() != DRIVER_LICENSE_2: - logger_error.error("Incorrect document") raise ValueError("Incorrect document") return doc.lower() +@logger_decorator_maker() def doc_id_check(doc_id): doc_id = doc_id.replace(' ', '') doc_id = doc_id.replace('-', '') @@ -88,10 +116,8 @@ def doc_id_check(doc_id): elif len(doc_id) == 9: return doc_id[:2] + ' ' + doc_id[2:] else: - logger_error.error("Incorrect document's number length") raise ValueError("Incorrect document's number length") else: - logger_error.error("Document number contains invalid characters") raise ValueError("Document number contains invalid characters") @@ -107,9 +133,9 @@ def __init__(self, name='атрибут', data_get=None, data_set=None, data_del def __get__(self, obj, objtype): return self.data_get(obj, self.name) + @logger_decorator_maker() def __set__(self, obj, val): if type(val) != str: - logger_error.error("Incorrect type of input data") raise TypeError("Incorrect type of input data") self.data_set(obj, self.data_check(val), self.name) @@ -132,6 +158,7 @@ def get_field(self, key): elif key == 'document_id': return self._document_id + @logger_decorator_maker('patient_set') def set_field(self, value, key): create_flag = True if not hasattr(self, '_' + key): @@ -142,7 +169,6 @@ def set_field(self, value, key): self._last_name = value elif (key == 'first_name' and hasattr(self, '_first_name')) or ( key == 'last_name' and hasattr(self, '_last_name')): - logger_error.error("Fields \"first_name\" and \"last_name\" mustn't be changed") raise AttributeError("Fields \"first_name\" and \"last_name\" mustn't be changed") elif key == 'birth_date': self._birth_date = value @@ -157,10 +183,8 @@ def set_field(self, value, key): and len(value) == 10): self._document_id = value else: - logger_error.error("Number of characters does not match the type of document") - raise Exception("Number of characters does not match the type of document") - if create_flag: - logger_info.info("Field " + key + " were updated") + raise ValueError("Number of characters does not match the type of document") + return create_flag def del_field(self, key): if key == 'first_name': @@ -183,6 +207,7 @@ def del_field(self, key): document_type = DataAccess('document_type', get_field, set_field, del_field, doc_check) document_id = DataAccess('document_id', get_field, set_field, del_field, doc_id_check) + @logger_decorator_maker('init') def __init__(self, name, surname, born, phone, doc, doc_id): self.first_name = name self.last_name = surname @@ -190,12 +215,12 @@ def __init__(self, name, surname, born, phone, doc, doc_id): self.phone = phone self.document_type = doc self.document_id = doc_id - logger_info.info("Patient added") def __str__(self): return (self.first_name + ' ' + self.last_name + ' ' + self.birth_date + ' ' + self.phone + ' ' + self.document_type + ' ' + self.document_id) + @logger_decorator_maker('save') def save(self): df = pnd.DataFrame({'first name': [self.first_name], 'last name': [self.last_name], @@ -204,7 +229,6 @@ def save(self): 'document type': [self.document_type], 'document id': [self.document_id]}) df.to_csv('data.csv', '|', header=False, index=False, mode='a') - logger_info.info("Patient saved") @staticmethod def create(name, surname, born, phone, doc, number): @@ -212,23 +236,27 @@ def create(name, surname, born, phone, doc, number): class PatientCollection: + @logger_decorator_maker() def __init__(self, path_to_file): if type(path_to_file) != str: logger_error.error("Incorrect type of input path") raise TypeError("Incorrect type of input path") self.path = path_to_file + @logger_decorator_maker() def __iter__(self): try: for i in range(0, len(pnd.read_csv(self.path, sep='|', header=None, dtype=str).index)): yield Patient(*pnd.read_csv(self.path, sep='|', header=None, dtype=str).iloc[i]) - except pnd.errors.EmptyDataError: + except IOError: return + @logger_decorator_maker() def limit(self, limit_val): - for i in range(0, limit_val): try: yield Patient(*pnd.read_csv(self.path, sep='|', header=None, nrows=limit_val, dtype=str).iloc[i]) + except IndexError: + return except pnd.errors.EmptyDataError: - return \ No newline at end of file + return From 76b5da4ac370e349b95726bb91da7ef6e5afb962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=96=D1=83?= =?UTF-8?q?=D1=80=D0=B8=D0=BD?= <2010zalex@gmail.com> Date: Mon, 4 May 2020 15:09:31 +0300 Subject: [PATCH 6/6] Fix code --- homework/patient.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homework/patient.py b/homework/patient.py index 3dbdf1c..a656c20 100644 --- a/homework/patient.py +++ b/homework/patient.py @@ -74,7 +74,7 @@ def birth_check(born): continue else: raise ValueError("Date contains invalid characters") - if str(datetime.date.today()) >= born: + if str(datetime.date.today()) >= born: #колхозное сравнение) return born else: raise ValueError("Date does not exist yet") @@ -135,7 +135,7 @@ def __get__(self, obj, objtype): @logger_decorator_maker() def __set__(self, obj, val): - if type(val) != str: + if not isinstance(val,str): raise TypeError("Incorrect type of input data") self.data_set(obj, self.data_check(val), self.name) @@ -238,7 +238,7 @@ def create(name, surname, born, phone, doc, number): class PatientCollection: @logger_decorator_maker() def __init__(self, path_to_file): - if type(path_to_file) != str: + if isinstance(path_to_file,str): logger_error.error("Incorrect type of input path") raise TypeError("Incorrect type of input path") self.path = path_to_file