Skip to content

Commit

Permalink
正規表現に変更
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuchiyama-araya committed Jan 20, 2025
1 parent e7dca31 commit dcec96d
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions studio/app/common/core/experiment/experiment_writer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import glob
import os
import pickle
import re
import shutil
from dataclasses import asdict
from datetime import datetime
Expand Down Expand Up @@ -243,7 +244,12 @@ def __copy_data_update_yaml(self, file_path: str, old_id: str, new_id: str) -> N
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()

updated_content = content.replace(old_id, new_id)
# Use the provided regex pattern for replacement
pattern = re.compile(rf"(?:\b|\/){re.escape(old_id)}(?:\b|\/)(?:[^\s]*)")
updated_content = pattern.sub(
lambda match: match.group(0).replace(old_id, new_id), content
)

with open(file_path, "w", encoding="utf-8") as file:
file.write(updated_content)

Expand Down Expand Up @@ -319,7 +325,11 @@ def __replace_ids_recursive(
]
)
elif isinstance(obj, str) and old_id in obj:
return obj.replace(old_id, new_id)
# Use the custom regex pattern for strings
pattern = re.compile(rf"(?:\b|\/){re.escape(old_id)}(?:\b|\/)(?:[^\s]*)")
return pattern.sub(
lambda match: match.group(0).replace(old_id, new_id), obj
)
elif hasattr(obj, "__dict__"):
# Process custom objects
for attr, value in obj.__dict__.items():
Expand Down

0 comments on commit dcec96d

Please sign in to comment.