from datetime import datetime class RawData: def __init__(self, data): self.__dict__.update(data) def get_str(self, attr): return RawData.get_str_st(self.__dict__, attr) @staticmethod def get_str_st(value, attr): if value is None: return '' result = value[attr] if result is None: return '' return result def get_int(self, attr): return RawData.get_int_st(self.__dict__, attr) @staticmethod def get_int_st(value, attr): if value is None: return '' result = value[attr] if result is None: return '' if not str(result).isnumeric(): print(f'The value {result} is not a number') return '' return result @staticmethod def str_to_date(value, str_format): return datetime.strptime(value, str_format).date().strftime('%d.%m.%Y') @staticmethod def get_date_st(value): if value is None: return '' try: return RawData.str_to_date(value, '%d.%m.%Y') except ValueError: try: return RawData.str_to_date(value, '%d.%m.%y') except ValueError: print(f'Invalid date {value}') return '' @staticmethod def get_collection_st(collection, function): if collection is None: return '' result_list = list(map(lambda item: function(item), collection)) if len(result_list) == 0: return '' return result_list