Skip to content

Commit

Permalink
Feat: 역직렬화 시 네스팅된 자료형을 핸들링할 수 있도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ProtossDragoon committed Jun 22, 2024
1 parent 659dc1f commit e00f381
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
42 changes: 15 additions & 27 deletions autosink_data_elt/log/filehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,20 @@ def _generate_dishwashing_id(self):
hash_input = (self.user_id + str(datetime.now())).encode('utf-8')
return hashlib.sha1(hash_input).hexdigest()[:10]

def read_file(self):
try:
with open(self.filename, 'r') as file:
data = json.load(file)
version = data.get('version', 1)
if version == 1:
logger.info(f'Read data with version 1 from {self.filename}')
return DishwashingDataV1(**data)
elif version == 2:
logger.info(f'Read data with version 2 from {self.filename}')
return DishwashingDataV2(**data)
else:
logger.error(f'Unsupported version: {version}')
raise ValueError(f'Unsupported version: {version}')
except FileNotFoundError:
logger.error(f'File {self.filename} not found')
raise
except json.JSONDecodeError:
logger.error(f'Error decoding JSON from file {self.filename}')
raise
@classmethod
def read_file(cls, json_path):
with open(json_path, 'r', encoding='utf-8') as file:
d = json.load(file)
version = d.get('version', 1)
if version == 1:
return DishwashingDataV1(**d)
elif version == 2:
return DishwashingDataV2(**d)
else:
raise ValueError(f'Unsupported version: {version}')

def write_file(self, data):
with open(self.filename, 'w') as file:
with open(self.filename, 'w', encoding='utf-8') as file:
json.dump(data.to_dict(), file, indent=4)
self.image_counter = 0 # Reset the image counter after writing to file
logger.info(f'Wrote data to {self.filename} and reset image counter')
Expand All @@ -64,15 +55,13 @@ def create_default_data(self, dishwashing_start=None, version=2, deque_size=10):
dishwashing_start = datetime.now(self.timezone).isoformat(timespec='seconds')

if version == 1:
logger.info(f'Creating default data with version 1')
return DishwashingDataV1(
version=1,
user_id=self.user_id,
dishwashing_id=self.dishwashing_id,
dishwashing_start=dishwashing_start
)
elif version == 2:
logger.info(f'Creating default data with version 2')
return DishwashingDataV2(
version=2,
user_id=self.user_id,
Expand All @@ -81,19 +70,16 @@ def create_default_data(self, dishwashing_start=None, version=2, deque_size=10):
interactions=deque(maxlen=deque_size)
)
else:
logger.error(f'Unsupported version: {version}')
raise ValueError(f'Unsupported version: {version}')

def add_interaction(self, data, **kwargs):
timestamp = datetime.now().isoformat(timespec='seconds')
# logger.info(f'Adding interaction with image from kwargs')

if data.version == 1:
interaction = InteractionV1.create(timestamp, kwargs.pop('image'), **kwargs)
elif data.version == 2:
interaction = InteractionV2.create(timestamp, kwargs.pop('image'), **kwargs)
else:
logger.error(f'Unsupported version: {data.version}')
raise ValueError(f'Unsupported version: {data.version}')

data.interactions.append(interaction)
Expand All @@ -118,5 +104,7 @@ def add_interaction(self, data, **kwargs):
file_handler.write_file(data)

# 파일에서 읽기
read_data = file_handler.read_file()
read_data = JSONFileHandler.read_file(
'volume/data-lake/extract/20240615_180539/interactions.json'
)
print(read_data)
8 changes: 8 additions & 0 deletions autosink_data_elt/log/template/dishwashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def to_dict(self):
data_dict['interactions'] = [interaction.to_dict() for interaction in self.interactions]
return data_dict

def __post_init__(self):
if self.interactions and isinstance(self.interactions[0], dict):
self.interactions = [InteractionV1(**interaction) for interaction in self.interactions]


@dataclass
class DishwashingDataV2:
Expand All @@ -33,3 +37,7 @@ def to_dict(self):
data_dict = asdict(self)
data_dict['interactions'] = [interaction.to_dict() for interaction in self.interactions]
return data_dict

def __post_init__(self):
if self.interactions and isinstance(self.interactions[0], dict):
self.interactions = [InteractionV2(**interaction) for interaction in self.interactions]

0 comments on commit e00f381

Please sign in to comment.