social-clusters/src/raw_data.py

51 lines
1.3 KiB
Python
Raw Normal View History

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 -1
result = value[attr]
if result is None:
return -1
if not str(result).isnumeric():
print(f'The value {result} is not a number')
return -1
return result
@staticmethod
def get_date_st(value):
if value is None:
return ''
try:
return datetime.strptime(value, '%d.%m.%Y').date()
except ValueError:
try:
return datetime.strptime(value, '%d.%m.%y').date()
except ValueError:
print(f'Invalid date {value}')
return ''
@staticmethod
def get_collection_st(collection, function):
return list(map(lambda item: function(item), [] if collection is None else collection))