diff options
author | 2023-09-16 11:57:53 -0700 | |
---|---|---|
committer | 2023-10-27 11:59:01 -0700 | |
commit | 3909703efaf625add82fd0065c4967ff05a6ee16 (patch) | |
tree | 7b3e36947e907b6ef5f9624fd915db16d7b03548 | |
parent | Refactor modules into subpackages (diff) |
Move iter_lstrip to separate utils module
-rw-r--r-- | snekbox/nsjail.py | 16 | ||||
-rw-r--r-- | snekbox/utils/__init__.py | 3 | ||||
-rw-r--r-- | snekbox/utils/iter.py | 16 |
3 files changed, 21 insertions, 14 deletions
diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py index 285a5bc..1d7565d 100644 --- a/snekbox/nsjail.py +++ b/snekbox/nsjail.py @@ -2,11 +2,10 @@ import logging import re import subprocess import sys -from collections.abc import Generator +from collections.abc import Iterable from contextlib import nullcontext from pathlib import Path from tempfile import NamedTemporaryFile -from typing import Iterable, TypeVar from google.protobuf import text_format @@ -16,29 +15,18 @@ from snekbox.limits.timed import time_limit from snekbox.process import EvalResult from snekbox.snekio import FileAttachment, MemFS from snekbox.snekio.filesystem import Size +from snekbox.utils.iter import iter_lstrip __all__ = ("NsJail",) log = logging.getLogger(__name__) -_T = TypeVar("_T") - # [level][timestamp][PID]? function_signature:line_no? message LOG_PATTERN = re.compile( r"\[(?P<level>(I)|[DWEF])\]\[.+?\](?(2)|(?P<func>\[\d+\] .+?:\d+ )) ?(?P<msg>.+)" ) -def iter_lstrip(iterable: Iterable[_T]) -> Generator[_T, None, None]: - """Remove leading falsy objects from an iterable.""" - it = iter(iterable) - for item in it: - if item: - yield item - break - yield from it - - class NsJail: """ Core Snekbox functionality, providing safe execution of Python code. diff --git a/snekbox/utils/__init__.py b/snekbox/utils/__init__.py new file mode 100644 index 0000000..16aade7 --- /dev/null +++ b/snekbox/utils/__init__.py @@ -0,0 +1,3 @@ +from . import iter + +__all__ = ("iter",) diff --git a/snekbox/utils/iter.py b/snekbox/utils/iter.py new file mode 100644 index 0000000..841ce57 --- /dev/null +++ b/snekbox/utils/iter.py @@ -0,0 +1,16 @@ +from collections.abc import Generator, Iterable +from typing import TypeVar + +__all__ = ("iter_lstrip",) + +_T = TypeVar("_T") + + +def iter_lstrip(iterable: Iterable[_T]) -> Generator[_T, None, None]: + """Remove leading falsy objects from an iterable.""" + it = iter(iterable) + for item in it: + if item: + yield item + break + yield from it |