-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
38 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
from uuid import uuid4 | ||
from pathlib import Path | ||
from itertools import zip_longest | ||
from contextlib_chdir import chdir | ||
|
||
from programlib import Agent, language_, Terminal | ||
|
||
|
@@ -51,12 +52,13 @@ def __init__(self, source=None, name=None, language='C++', | |
self.workdir = Path(__file__).parent / 'programs' | ||
self.ephemeral = True | ||
|
||
if source: | ||
self.language.write_source(self.workdir, self.name, source) | ||
else: | ||
source = self.language.read_source(self.workdir, self.name) | ||
with chdir(self.workdir): | ||
if source: | ||
self.language.write_source(self.name, source) | ||
else: | ||
source = self.language.read_source(self.name) | ||
|
||
self.stdout, self.exitstatus = self.language.build(self.workdir, self.name) | ||
self.stdout, self.exitstatus = self.language.build(self.name) | ||
|
||
# Please send your opinions on 'not not' vs 'bool()' to [email protected] | ||
self.compile_error = not not self.exitstatus | ||
|
@@ -66,8 +68,9 @@ def __lt__(self, other): | |
|
||
def read(self): | ||
"""Obtain the source code of the program""" | ||
|
||
return self.language.read_source(self.workdir, self.name) | ||
|
||
with chdir(self.workdir): | ||
return self.language.read_source(self.name) | ||
|
||
def run(self, input_lines=[], force=True): | ||
""" | ||
|
@@ -81,7 +84,8 @@ def run(self, input_lines=[], force=True): | |
Raw results will always be stored in `program.stdout` and `program.exitstatus` attributes | ||
""" | ||
|
||
self.stdout, self.exitstatus = self.language.run(self.workdir, self.name, input_lines) | ||
with chdir(self.workdir): | ||
self.stdout, self.exitstatus = self.language.run(self.name, input_lines) | ||
assert force or not self.exitstatus, f'Exit status {self.exitstatus}' | ||
return self.term.emulate(self.stdout) | ||
|
||
|
@@ -90,7 +94,8 @@ def spawn(self, delimiter='\n'): | |
Launch the program and get a process object to communicate with it interactively | ||
""" | ||
|
||
process = self.language.spawn(self.workdir, self.name) | ||
with chdir(self.workdir): | ||
process = self.language.spawn(self.name) | ||
return Agent(self, process, delimiter=delimiter) | ||
|
||
def test(self, test_cases, force=True): | ||
|
@@ -127,8 +132,12 @@ def test(self, test_cases, force=True): | |
return test_runs | ||
|
||
def save(self, path): | ||
self.language.copy_source(self.workdir, self.name, path) | ||
path = Path(path).absolute() | ||
|
||
with chdir(self.workdir): | ||
self.language.copy_source(self.name, path) | ||
|
||
def __del__(self): | ||
if self.ephemeral: | ||
self.language.cleanup(self.workdir, self.name) | ||
with chdir(self.workdir): | ||
self.language.cleanup(self.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "programlib" | ||
version = "7.2.0" | ||
version = "8.0.0" | ||
description = "Programs as Objects" | ||
authors = ["Vadim Liventsev <[email protected]>"] | ||
license = "MIT" | ||
|
@@ -17,6 +17,8 @@ pexpect = "^4.8.0" | |
gym = "^0.26.2" | ||
numpy = "^1.24.2" | ||
pyte = "^0.8.0" | ||
contextlib-chdir = "^1.0.2" | ||
|
||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
|