Skip to navigation

nemo_gym.verifiers.files

View as Markdown

File checks for Python verifiers: the agent’s workspace is a directory, these read it.

Standard library only (see nemo_gym.verifiers). Paths are relative to the workspace; a path that escapes it (.. or an absolute path outside it) reads as absent, so a task cannot be graded on files the agent never owned.

Module Contents

Functions

NameDescription
_resolve-
_without_trailing_newline-
file_existsWhether path under the workspace is a regular file.
read_textThe text of path under the workspace, or None when it is absent or not a file.
text_file_equals1.0 when path under the workspace holds exactly content (one trailing newline tolerated).

API

nemo_gym.verifiers.files._resolve(
workspace: pathlib.Path | str,
path: str | pathlib.Path
) -> pathlib.Path | None
nemo_gym.verifiers.files._without_trailing_newline(
text: str
) -> str
nemo_gym.verifiers.files.file_exists(
workspace: pathlib.Path | str,
path: str | pathlib.Path
) -> bool

Whether path under the workspace is a regular file.

nemo_gym.verifiers.files.read_text(
workspace: pathlib.Path | str,
path: str | pathlib.Path,
encoding: str = 'utf-8',
strip_trailing_newline: bool = True
) -> str | None

The text of path under the workspace, or None when it is absent or not a file.

One trailing newline is dropped by default (echo and most editors end a file with one, and a verifier comparing read_text(...) == "answer" should not fail on it); pass strip_trailing_newline=False for the bytes as written. Undecodable bytes are replaced rather than raised, so a verifier comparing text never crashes on binary output.

nemo_gym.verifiers.files.text_file_equals(
workspace: pathlib.Path | str,
path: str | pathlib.Path,
content: str,
strip_trailing_newline: bool = True
) -> float

1.0 when path under the workspace holds exactly content (one trailing newline tolerated).

A complete verifier: verify = functools.partial(text_file_equals, path="hello.txt", content="hello").