Skip to content

Commit 9dc1c85

Browse files
Add Filesystem.find method (#18)
1 parent 29ce7f7 commit 9dc1c85

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,5 @@ submission = Submission(
347347

348348
result = judge0.run(submissions=submission)
349349
print(result.stdout)
350-
351-
matches = [f for f in result.post_execution_filesystem if f.name == "my_dir2/my_file2.txt"]
352-
f = matches[0] if matches else None
353-
print(f)
350+
print(result.post_execution_filesystem.find("./my_dir2/my_file2.txt"))
354351
```

src/judge0/filesystem.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ def encode(self) -> bytes:
9292
zip_file.writestr(file.name, file.content)
9393
return zip_buffer.getvalue()
9494

95+
def find(self, name: str) -> Optional[File]:
96+
"""Find file by name in Filesystem object.
97+
98+
Parameters
99+
----------
100+
name : str
101+
File name to find.
102+
103+
Returns
104+
-------
105+
File or None
106+
Found File object or None if not found.
107+
"""
108+
if name.startswith("./"):
109+
name = name[2:]
110+
elif name.startswith("/"):
111+
name = name[1:]
112+
113+
matches = [f for f in self.files if f.name == name]
114+
return matches[0] if matches else None
115+
95116
def __str__(self) -> str:
96117
"""Create string representation of Filesystem object."""
97118
return b64encode(self.encode()).decode()

0 commit comments

Comments
 (0)