Skip to content

Commit d4789ef

Browse files
committed
Updates
1 parent dfb3b0f commit d4789ef

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ pip install safeexecute
1818

1919
## Usage
2020

21-
Set your working directory which is where any potential outputs of the code will be stored. This is also where the code will be executed.
21+
You can pass an entire message from a langauge model into the `code` field and it will parse out any Python code blocks and execute them. If anywhere in the `code` says `pip install <package>`, it will install the package in the container before executing the code.
2222

2323
```python
24-
import os
2524
from safeexecute import execute_python_code
2625

27-
working_directory = os.path.join(os.getcwd(), "WORKSPACE")
2826
code = "print('Hello, World!')"
2927

30-
result = execute_python_code(code=code, working_directory=working_directory)
28+
result = execute_python_code(code=code)
3129
print(result)
3230
```

safeexecute/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ def install_docker_image():
1818
return client
1919

2020

21-
def execute_python_code(code: str, working_directory: str) -> str:
22-
# Create working directory if it doesn't exist
21+
def execute_python_code(code: str, working_directory: str = None) -> str:
22+
if working_directory is None:
23+
working_directory = os.path.join(os.getcwd(), "WORKSPACE")
2324
if not os.path.exists(working_directory):
2425
os.makedirs(working_directory)
2526
# Check if there are any package requirements in the code to install
2627
package_requirements = re.findall(r"pip install (.*)", code)
28+
# Strip out python code blocks if they exist in the code
2729
if "```python" in code:
2830
code = code.split("```python")[1].split("```")[0]
29-
# Create a temporary Python file in the WORKSPACE directory
3031
temp_file = os.path.join(working_directory, "temp.py")
3132
with open(temp_file, "w") as f:
3233
f.write(code)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setup(
1010
name="safeexecute",
11-
version="0.0.5",
11+
version="0.0.6",
1212
description="Safe way to execute Python code with containerization.",
1313
long_description=long_description,
1414
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)