Skip to content

Commit 92ee878

Browse files
Add print to file with filename (#18)
1 parent 8fa5e18 commit 92ee878

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ with open("watch.log", "w") as f:
164164
a = 1
165165
```
166166

167+
Or you could just give a filename to ``watch``. It will append to the file.
168+
169+
```python
170+
watch(a, file="watch.log")
171+
```
172+
173+
Use config if you want to make it global
174+
175+
```python
176+
watch.config(file="watch.log")
177+
```
178+
167179
### alias
168180

169181
You can give an alias to a monitored variable, so you can unwatch it anywhere. And the alias will be printed instead of the variable name

src/watchpoints/watch_print.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,15 @@ def getsourceline(self, exec_info):
7575
return "unable to locate the source"
7676

7777
def printer(self, obj):
78-
if type(obj) is str:
79-
print(obj, file=self.file)
78+
79+
def do_print(obj, stream):
80+
if type(obj) is str:
81+
print(obj, file=stream)
82+
else:
83+
pprint.pprint(obj, stream=stream)
84+
85+
if isinstance(self.file, str):
86+
with open(self.file, "a") as f:
87+
do_print(obj, f)
8088
else:
81-
pprint.pprint(obj, stream=self.file)
89+
do_print(obj, self.file)

tests/test_watch.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from contextlib import redirect_stdout
66
import io
7+
import os
78
import sys
89
import unittest
910
from watchpoints import watch, unwatch
@@ -235,7 +236,7 @@ def test_stack_limit_local(self):
235236
unwatch()
236237
self.assertEqual(s.getvalue().count("> "), 1)
237238

238-
def test_write_to_file(self):
239+
def test_write_to_file_stream(self):
239240
f = open("tmp_test.log", "w")
240241
a = [1, 2, 3]
241242
watch(a, file=f, stack_limit=1)
@@ -244,4 +245,15 @@ def test_write_to_file(self):
244245
f.close()
245246
with open("tmp_test.log") as f:
246247
data = f.read()
248+
os.remove("tmp_test.log")
247249
self.assertEqual(data.count("> "), 1)
250+
251+
def test_write_to_file(self):
252+
a = [1, 2, 3]
253+
watch(a, file="tmp_test.log")
254+
a[0] = 2
255+
unwatch()
256+
with open("tmp_test.log") as f:
257+
data = f.read()
258+
os.remove("tmp_test.log")
259+
self.assertIn("a[0] = 2", data)

tests/test_watch_print.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
from watchpoints.watch_print import WatchPrint
1212

1313

14+
class Elem:
15+
def __init__(self):
16+
self.alias = None
17+
self.default_alias = "a"
18+
self.prev_obj = ""
19+
self.obj = ""
20+
21+
1422
class TestWatchPrint(unittest.TestCase):
1523
def test_basic(self):
16-
class Elem:
17-
def __init__(self):
18-
self.alias = None
19-
self.default_alias = "a"
20-
self.prev_obj = ""
21-
self.obj = ""
2224
s = io.StringIO()
2325
with redirect_stdout(s):
2426
wp = WatchPrint(file=sys.stdout)
@@ -44,3 +46,12 @@ def test_getsourceline(self):
4446

4547
line = wp.getsourceline((None, "file/not/exist", 100))
4648
self.assertEqual(line, "unable to locate the source")
49+
50+
def test_print_to_file(self):
51+
wp = WatchPrint(file="tmp_test.log")
52+
wp(inspect.currentframe(), Elem(), ("function", "filename", "c"))
53+
with open("tmp_test.log") as f:
54+
data = f.read()
55+
os.remove("tmp_test.log")
56+
self.assertIn("function", data)
57+
self.assertIn("filename", data)

0 commit comments

Comments
 (0)