Skip to content

Commit

Permalink
Add print to file with filename (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian authored Mar 15, 2021
1 parent 8fa5e18 commit 92ee878
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ with open("watch.log", "w") as f:
a = 1
```

Or you could just give a filename to ``watch``. It will append to the file.

```python
watch(a, file="watch.log")
```

Use config if you want to make it global

```python
watch.config(file="watch.log")
```

### alias

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
Expand Down
14 changes: 11 additions & 3 deletions src/watchpoints/watch_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ def getsourceline(self, exec_info):
return "unable to locate the source"

def printer(self, obj):
if type(obj) is str:
print(obj, file=self.file)

def do_print(obj, stream):
if type(obj) is str:
print(obj, file=stream)
else:
pprint.pprint(obj, stream=stream)

if isinstance(self.file, str):
with open(self.file, "a") as f:
do_print(obj, f)
else:
pprint.pprint(obj, stream=self.file)
do_print(obj, self.file)
14 changes: 13 additions & 1 deletion tests/test_watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from contextlib import redirect_stdout
import io
import os
import sys
import unittest
from watchpoints import watch, unwatch
Expand Down Expand Up @@ -235,7 +236,7 @@ def test_stack_limit_local(self):
unwatch()
self.assertEqual(s.getvalue().count("> "), 1)

def test_write_to_file(self):
def test_write_to_file_stream(self):
f = open("tmp_test.log", "w")
a = [1, 2, 3]
watch(a, file=f, stack_limit=1)
Expand All @@ -244,4 +245,15 @@ def test_write_to_file(self):
f.close()
with open("tmp_test.log") as f:
data = f.read()
os.remove("tmp_test.log")
self.assertEqual(data.count("> "), 1)

def test_write_to_file(self):
a = [1, 2, 3]
watch(a, file="tmp_test.log")
a[0] = 2
unwatch()
with open("tmp_test.log") as f:
data = f.read()
os.remove("tmp_test.log")
self.assertIn("a[0] = 2", data)
23 changes: 17 additions & 6 deletions tests/test_watch_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
from watchpoints.watch_print import WatchPrint


class Elem:
def __init__(self):
self.alias = None
self.default_alias = "a"
self.prev_obj = ""
self.obj = ""


class TestWatchPrint(unittest.TestCase):
def test_basic(self):
class Elem:
def __init__(self):
self.alias = None
self.default_alias = "a"
self.prev_obj = ""
self.obj = ""
s = io.StringIO()
with redirect_stdout(s):
wp = WatchPrint(file=sys.stdout)
Expand All @@ -44,3 +46,12 @@ def test_getsourceline(self):

line = wp.getsourceline((None, "file/not/exist", 100))
self.assertEqual(line, "unable to locate the source")

def test_print_to_file(self):
wp = WatchPrint(file="tmp_test.log")
wp(inspect.currentframe(), Elem(), ("function", "filename", "c"))
with open("tmp_test.log") as f:
data = f.read()
os.remove("tmp_test.log")
self.assertIn("function", data)
self.assertIn("filename", data)

0 comments on commit 92ee878

Please sign in to comment.