File size: 4,700 Bytes
45b200f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "Python Code Executor",
   "id": "f96e89bb67a640f5"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-06-10T15:11:36.621661Z",
     "start_time": "2025-06-10T15:11:36.617741Z"
    }
   },
   "source": [
    "from langchain_core.tools import Tool\n",
    "from langchain_experimental.utilities import PythonREPL\n",
    "import subprocess"
   ],
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-10T14:54:42.141759Z",
     "start_time": "2025-06-10T14:54:42.138854Z"
    }
   },
   "cell_type": "code",
   "source": "file_dir = 'files/f918266a-b3e0-4914-865d-4faa564f1aef.py'",
   "id": "1f0aed71e203012d",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-10T14:44:45.063803Z",
     "start_time": "2025-06-10T14:44:45.061618Z"
    }
   },
   "cell_type": "code",
   "source": "python_repl = PythonREPL()",
   "id": "acea852beeb1035e",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-10T14:55:05.802715Z",
     "start_time": "2025-06-10T14:55:05.797934Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "with open(file_dir, \"r\", encoding=\"utf-8\") as f:\n",
    "    code = f.read()\n",
    "    print(code)\n"
   ],
   "id": "620b184072e95086",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "from random import randint\n",
      "import time\n",
      "\n",
      "class UhOh(Exception):\n",
      "    pass\n",
      "\n",
      "class Hmm:\n",
      "    def __init__(self):\n",
      "        self.value = randint(-100, 100)\n",
      "\n",
      "    def Yeah(self):\n",
      "        if self.value == 0:\n",
      "            return True\n",
      "        else:\n",
      "            raise UhOh()\n",
      "\n",
      "def Okay():\n",
      "    while True:\n",
      "        yield Hmm()\n",
      "\n",
      "def keep_trying(go, first_try=True):\n",
      "    maybe = next(go)\n",
      "    try:\n",
      "        if maybe.Yeah():\n",
      "            return maybe.value\n",
      "    except UhOh:\n",
      "        if first_try:\n",
      "            print(\"Working...\")\n",
      "            print(\"Please wait patiently...\")\n",
      "        time.sleep(0.1)\n",
      "        return keep_trying(go, first_try=False)\n",
      "\n",
      "if __name__ == \"__main__\":\n",
      "    go = Okay()\n",
      "    print(f\"{keep_trying(go)}\")\n",
      "\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-10T14:55:08.665931Z",
     "start_time": "2025-06-10T14:55:08.661026Z"
    }
   },
   "cell_type": "code",
   "source": "print(python_repl.run(code))",
   "id": "c55e78888adc58f2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-10T15:13:00.580769Z",
     "start_time": "2025-06-10T15:12:56.064064Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def run_as_main(filename):\n",
    "    result = subprocess.run([\"python\", filename], capture_output=True, text=True)\n",
    "    return result.stdout\n",
    "print(run_as_main(file_dir))"
   ],
   "id": "c7b145cdb13aea01",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Working...\n",
      "Please wait patiently...\n",
      "0\n",
      "\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "repl_tool = Tool(\n",
    "    name=\"python_repl\",\n",
    "    description=\"A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.\",\n",
    "    func=python_repl.run,\n",
    ")"
   ],
   "id": "1789b291aa070b8b"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}