Skip to content

Pull request from 'feature_main_DKRN' to 'main' #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions extensions/sandbox/docker/pandasai_docker/docker_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import docker

from pandasai.sandbox import Sandbox

from .serializer import ResponseSerializer

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -85,7 +84,7 @@ def start(self):

def stop(self) -> None:
if self._started and self._container:
logger.info(f"Stopping a Docker container with id '{self._container.id}''")
logger.info(f"Stopping a Docker container with id '{self._container.id}'")
self._container.stop()
self._container.remove()
self._container = None
Expand Down
3 changes: 2 additions & 1 deletion pandasai/core/code_execution/code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def add_to_env(self, key: str, value: Any) -> None:

def execute(self, code: str) -> dict:
try:
exec(code, self._environment)
# Use a restricted environment to prevent arbitrary code execution
exec(code, {**self._environment, '__builtins__': {}})
except Exception as e:
raise CodeExecutionError("Code execution failed") from e
return self._environment
Expand Down
35 changes: 9 additions & 26 deletions pandasai/helpers/logger.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
"""
Logger class

This class is used to log messages to the console and/or a file.

Example:
```python
from pandasai.helpers.logger import Logger

logger = Logger()
logger.log("Hello, world!")
# 2021-08-01 12:00:00 [INFO] Hello, world!

logger.logs
#["Hello, world!"]
```
"""

import inspect
import logging
import sys
Expand All @@ -34,6 +16,8 @@ class Log(BaseModel):

msg: str
level: int
time: float
source: str


class Logger:
Expand Down Expand Up @@ -82,14 +66,13 @@ def log(self, message: str, level: int = logging.INFO):
elif level == logging.CRITICAL:
self._logger.critical(message)

self._logs.append(
{
"msg": message,
"level": logging.getLevelName(level),
"time": self._calculate_time_diff(),
"source": self._invoked_from(),
}
log_entry = Log(
msg=message,
level=level,
time=self._calculate_time_diff(),
source=self._invoked_from()
)
self._logs.append(log_entry)

def _invoked_from(self, level: int = 5) -> str:
"""Return the name of the class that invoked the logger"""
Expand All @@ -112,7 +95,7 @@ def _calculate_time_diff(self):
return time_diff

@property
def logs(self) -> List[str]:
def logs(self) -> List[Log]:
"""Return the logs"""
return self._logs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class TestCodeExecutor(unittest.TestCase):
def setUp(self):
self.config = MagicMock(specs=Config)
self.config = MagicMock(spec=Config)
self.executor = CodeExecutor(self.config)

def test_initialization(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/core/prompts/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ class TestPrompt(BasePrompt):
# 1. Use the template from file
# 2. Remove extra newlines
assert result == "Hello\n\nWorld!"
mock_template.render.assert_called_once_with(name="Test")
mock_template.render.assert_called_once_with(name="Test")