Skip to content

Commit

Permalink
exp_dir sanitization (ServiceNow#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
gasse authored and qipeng committed Nov 20, 2024
1 parent 4fac0b0 commit 586b615
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion browsergym/experiments/src/browsergym/experiments/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import os
import pickle
import re
import sys
import time
import traceback
Expand Down Expand Up @@ -179,13 +180,16 @@ def prepare(self, exp_root):
def _make_dir(self, exp_root):
"""Create a unique directory for the experiment."""
date_str = self.exp_date.strftime("%Y-%m-%d_%H-%M-%S")
exp_str = re.sub(
r"[\/:*?<>|]", "_", self.exp_name
) # sanitize exp_name to be used as a file name (substitute forbidden characters)

for i in range(1000):
if i >= 999: # make sure we don't loop forever
raise ValueError("Could not find a unique name for the experiment directory.")

tag = f"_{i}" if i > 0 else ""
self.exp_dir = Path(exp_root) / f"{date_str}_{self.exp_name}{tag}"
self.exp_dir = Path(exp_root) / f"{date_str}_{exp_str}{tag}"
if not self.exp_dir.exists():
break

Expand Down

0 comments on commit 586b615

Please sign in to comment.