def test_track(self): cb = CB() a = [1, 2, 3] b = [1, 2, 3] watch(a, callback=cb, track="object") a[0] = 2 self.assertEqual(cb.counter, 1) a = {} self.assertEqual(cb.counter, 1) watch(b, callback=cb, track=["variable"]) b[0] = 2 self.assertEqual(cb.counter, 1) b = {} self.assertEqual(cb.counter, 2) with self.assertRaises(ValueError): c = [] watch(c, track=["invalid"]) with self.assertRaises(ValueError): c = [] watch(c, track="invalid") with self.assertRaises(ValueError): c = [] watch(c, track=[]) with self.assertRaises(TypeError): c = [] watch(c, track={}) unwatch()
def test_watch_print(self): class Elem: def __init__(self): self.alias = None self.default_alias = "a" self.prev_obj = "" self.obj = "" a = [0] # Create new threads thread1 = myThread(1, "Thread-1", a) thread2 = myThread(2, "Thread-2", a) # Start new Threads thread1.start() thread2.start() s = io.StringIO() with redirect_stdout(s): wp = WatchPrint(file=sys.stdout) wp(inspect.currentframe(), Elem(), ("a", "b", "c")) self.assertIn("Thread", s.getvalue()) thread1.join() thread2.join() unwatch()
def test_subscr(self): cb = CB() watch.config(callback=cb) a = [1, 2, 3] watch(a[1]) a[0] = 2 a[1] = 3 self.assertEqual(cb.counter, 1) with self.assertRaises(ValueError): watch(a[0:2]) def val(arg): return 1 a[val(3)] = 4 self.assertEqual(cb.counter, 2) unwatch() a = {"a": 1} watch(a["a"]) a["a"] = 2 a["b"] = 3 self.assertEqual(cb.counter, 3) unwatch()
def test_stack_limit_local(self): s = io.StringIO() with redirect_stdout(s): a = [1, 2, 3] watch(a, file=sys.stdout, stack_limit=1) a[0] = 2 unwatch() self.assertEqual(s.getvalue().count("> "), 1)
def test_printer(self): s = io.StringIO() with redirect_stdout(s): watch.config(file=sys.stdout) a = [1, 2, 3] watch(a) a[0] = 2 unwatch() self.assertNotEqual(s.getvalue(), "")
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)
def test_alias(self): cb = CB() watch.config(callback=cb) a = [1, 2, 3] watch(a, alias="a") a[1] = 4 self.assertEqual(cb.counter, 1) unwatch("a") a[1] = 5 self.assertEqual(cb.counter, 1)
def test_noargs(self): cb = CB() watch.config(callback=cb) a = [1, 2, 3] watch(a) a[1] = 0 self.assertEqual(cb.counter, 1) unwatch() a[1] = 2 self.assertEqual(cb.counter, 1)
def test_stack_limit_global(self): watch.config(stack_limit=1) s = io.StringIO() with redirect_stdout(s): watch.config(file=sys.stdout) a = [1, 2, 3] watch(a) a[0] = 2 unwatch() self.assertEqual(s.getvalue().count("> "), 1)
def test_when(self): cb = CB() a = 0 watch(a, callback=cb, when=lambda x: x > 0) a = -1 a = 1 a = 2 a = -3 self.assertEqual(cb.counter, 2) unwatch()
def test_deepcopy(self): cb = CB() a = {"a": [0]} watch(a, callback=cb) a["a"][0] = 1 self.assertEqual(cb.counter, 0) unwatch() watch(a, callback=cb, deepcopy=True) a["a"][0] = 2 self.assertEqual(cb.counter, 1) unwatch()
def test_write_to_file_stream(self): f = open("tmp_test.log", "w") a = [1, 2, 3] watch(a, file=f, stack_limit=1) a[0] = 2 unwatch() f.close() with open("tmp_test.log") as f: data = f.read() os.remove("tmp_test.log") self.assertEqual(data.count("> "), 1)
def test_custom_cmp(self): def cmp(obj1, obj2): return False cb = CB() a = {"a": 0} watch(a, callback=cb, cmp=cmp) a["a"] = 1 self.assertEqual(cb.counter, 0) a["a"] = 2 self.assertEqual(cb.counter, 0) unwatch()
def test_custom_copy(self): def copy(obj): return {"a": 0} cb = CB() a = {"a": 0} watch(a, callback=cb, copy=copy) a["a"] = 1 self.assertEqual(cb.counter, 1) a["a"] = 1 self.assertGreater(cb.counter, 2) unwatch()
def test_long(self): cb = CB() watch.config(callback=cb) a = [1, 2, 3] watch(a) a[1] = 0 self.assertEqual(cb.counter, 1) unwatch(a) a[1] = 1 self.assertEqual(cb.counter, 1) watch(a) a[1] = 0 self.assertEqual(cb.counter, 2) unwatch() a[1] = 2 self.assertEqual(cb.counter, 2) watch(a) a[1] = 0 self.assertEqual(cb.counter, 3) unwatch() a[1] = 2 self.assertEqual(cb.counter, 3) watch(a, alias="a") a[1] = 4 self.assertEqual(cb.counter, 4) unwatch("a") a[1] = 5 self.assertEqual(cb.counter, 4)
def test_basic(self): cb = CB() a = [0] watch(a, callback=cb) # Create new threads thread1 = myThread(1, "Thread-1", a) thread2 = myThread(2, "Thread-2", a) # Start new Threads thread1.start() thread2.start() thread1.join() thread2.join() unwatch() self.assertEqual(cb.counter, 10)
def test_custom_printer(self): s = io.StringIO() with redirect_stdout(s): watch.config(file=sys.stdout) a = [i for i in range(100)] watch(a, custom_printer=print) a = [i + 1 for i in range(100)] unwatch() self.assertLess(s.getvalue().count("\n"), 100) with redirect_stdout(s): watch.config(file=sys.stdout, custom_printer=print) a = [i for i in range(100)] watch(a) a = [i + 1 for i in range(100)] unwatch() self.assertLess(s.getvalue().count("\n"), 100)
def test_series(self): def __comparison_series__(obj1, obj2): return not obj1.equals(obj2) cb = CB() ss = pd.Series(data=[1, 2, 3], index=list("abc")) watch(ss, cmp=__comparison_series__, callback=cb) # Should watch here self.assertEqual(cb.counter, 0) ss.loc["a"] = 10 self.assertEqual(cb.counter, 1) unwatch()
def test_element_callback(self): cb = CB() a = [1, 2, 3] watch(a, callback=cb) a[0] = 2 a.append(4) b = a b.append(5) a = {"a": 1} a["b"] = 2 def change(d): d["c"] = 3 change(a) self.assertEqual(cb.counter, 6) unwatch()
def test_attr(self): class MyObj: def __init__(self): self.a = 0 cb = CB() watch.config(callback=cb) obj = MyObj() watch(obj.a) obj.a = 1 self.assertEqual(cb.counter, 1) unwatch(obj.a) obj.a = 2 self.assertEqual(cb.counter, 1) watch(obj) obj.a = 3 self.assertEqual(cb.counter, 2) obj.a = 3 self.assertEqual(cb.counter, 2)
def test_basic(self): cb = CB() watch.config(callback=cb) a = [1, 2, 3] watch(a) a[0] = 2 a.append(4) b = a b.append(5) a = {"a": 1} a["b"] = 2 def change(d): d["c"] = 3 change(a) self.assertEqual(cb.counter, 6) unwatch()
def test_dataframe(self): cb = CB() df = pd.DataFrame(data=[[1, 2], [3, 4], [5, 6]], index=list("abc"), columns=list("AB")) watch(df, callback=cb) # Other stuff happens a = 2 _ = a + 5 self.assertEqual(cb.counter, 0) df.loc["a", "B"] = 10 self.assertEqual(cb.counter, 1) unwatch()
def test_dataframe(self): def __comparison_dataframe__(obj1, obj2): return not obj1.equals(obj2) cb = CB() df = pd.DataFrame(data=[[1, 2], [3, 4], [5, 6]], index=list("abc"), columns=list("AB")) watch(df, cmp=__comparison_dataframe__, callback=cb) # Other stuff happens a = 2 b = a + 5 self.assertEqual(cb.counter, 0) df.loc["a", "B"] = 10 self.assertEqual(cb.counter, 1) unwatch()
def setUp(self): unwatch() watch.restore()