peek-python 1.4.3.post0__tar.gz → 1.4.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: peek-python
3
- Version: 1.4.3.post0
3
+ Version: 1.4.4
4
4
  Summary: peek - debugging and benchmarking made easy
5
5
  Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/salabim/ycecream
@@ -4,7 +4,7 @@
4
4
  # | .__/ \___| \___||_|\_\
5
5
  # |_| like print, but easy.
6
6
 
7
- __version__ = "1.4.3"
7
+ __version__ = "1.4.4"
8
8
 
9
9
  """
10
10
  See https://github.com/salabim/peek for details
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: peek-python
3
- Version: 1.4.3.post0
3
+ Version: 1.4.4
4
4
  Summary: peek - debugging and benchmarking made easy
5
5
  Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/salabim/ycecream
@@ -7,5 +7,4 @@ peek_python.egg-info/PKG-INFO
7
7
  peek_python.egg-info/SOURCES.txt
8
8
  peek_python.egg-info/dependency_links.txt
9
9
  peek_python.egg-info/top_level.txt
10
- tests/test_peek.py
11
- tests/test_peek1.py
10
+ tests/test_peek.py
@@ -8,7 +8,7 @@ authors = [
8
8
  {name = "Ruud van der Ham", email = "rt.van.der.ham@gmail.com"}
9
9
  ]
10
10
  description = "peek - debugging and benchmarking made easy"
11
- version = "1.4.3-0"
11
+ version = "1.4.4"
12
12
  readme = "README.md"
13
13
  requires-python = ">=3.6"
14
14
  dependencies = [
@@ -1,1205 +0,0 @@
1
- from __future__ import print_function
2
- from __future__ import division
3
-
4
- import sys
5
- import datetime
6
- import time
7
- import pytest
8
- import os
9
- from pathlib import Path
10
-
11
- file_folder = os.path.dirname(__file__)
12
- os.chdir(file_folder)
13
- sys.path.insert(0, file_folder + "/../peek")
14
-
15
- import peek
16
- import peek as peek_module
17
-
18
- import tempfile
19
-
20
-
21
- class g:
22
- pass
23
-
24
-
25
- context_start = "peek| #"
26
-
27
- Pythonista = sys.platform == "ios"
28
-
29
- FAKE_TIME = datetime.datetime(2021, 1, 1, 0, 0, 0)
30
-
31
-
32
- @pytest.fixture
33
- def patch_datetime_now(monkeypatch):
34
- class mydatetime:
35
- @classmethod
36
- def now(cls):
37
- return FAKE_TIME
38
-
39
- monkeypatch.setattr(datetime, "datetime", mydatetime)
40
-
41
-
42
- def test_time(patch_datetime_now):
43
- hello = "world"
44
- s = peek(hello, show_time=True, as_str=True)
45
- assert s == "peek| @ 00:00:00.000000 ==> hello: 'world'\n"
46
-
47
-
48
- def test_no_arguments(capsys):
49
- result = peek()
50
- out, err = capsys.readouterr()
51
- assert out.startswith(context_start)
52
- assert out.endswith(" in test_no_arguments()\n")
53
- assert result is None
54
-
55
-
56
- def test_one_arguments(capsys):
57
- hello = "world"
58
- result = peek(hello)
59
- peek(hello)
60
- out, err = capsys.readouterr()
61
- assert (
62
- out
63
- == """\
64
- peek| hello: 'world'
65
- peek| hello: 'world'
66
- """
67
- )
68
- assert result == hello
69
-
70
-
71
- def test_two_arguments(capsys):
72
- hello = "world"
73
- ll = [1, 2, 3]
74
- result = peek(hello, ll)
75
- out, err = capsys.readouterr()
76
- assert out == "peek| hello: 'world', ll: [1, 2, 3]\n"
77
- assert result == (hello, ll)
78
-
79
-
80
- def test_in_function(capsys):
81
- def hello(val):
82
- peek(val, show_line_number=True)
83
-
84
- hello("world")
85
- out, err = capsys.readouterr()
86
- assert out.startswith(context_start)
87
- assert out.endswith(" in test_in_function.hello() ==> val: 'world'\n")
88
-
89
-
90
- def test_in_function_no_parent(capsys):
91
- def hello(val):
92
- peek(val, show_line_number="n")
93
-
94
- hello("world")
95
- out, err = capsys.readouterr()
96
- assert out.startswith(context_start)
97
- assert not out.endswith(" in test_in_function_no_parent.hello() ==> val: 'world'\n")
98
-
99
-
100
- def test_prefix(capsys):
101
- hello = "world"
102
- peek(hello, prefix="==> ")
103
- out, err = capsys.readouterr()
104
- assert out == "==> hello: 'world'\n"
105
-
106
-
107
- def test_time_delta():
108
- sdelta0 = peek(1, show_delta=True, as_str=True)
109
- stime0 = peek(1, show_time=True, as_str=True)
110
- time.sleep(0.001)
111
- sdelta1 = peek(1, show_delta=True, as_str=True)
112
- stime1 = peek(1, show_time=True, as_str=True)
113
- assert sdelta0 != sdelta1
114
- assert stime0 != stime1
115
- peek.delta = 10
116
- time.sleep(0.1)
117
- assert 10.05 < peek.delta < 11
118
-
119
-
120
- def test_dynamic_prefix(capsys):
121
- g.i = 0
122
-
123
- def prefix():
124
- g.i += 1
125
- return str(g.i) + ")"
126
-
127
- hello = "world"
128
- peek(hello, prefix=prefix)
129
- peek(hello, prefix=prefix)
130
- out, err = capsys.readouterr()
131
- assert out == "1)hello: 'world'\n2)hello: 'world'\n"
132
-
133
-
134
- def test_values_only():
135
- with peek.preserve():
136
- peek.configure(values_only=True)
137
- hello = "world"
138
- s = peek(hello, as_str=True)
139
- assert s == "peek| 'world'\n"
140
-
141
-
142
- def test_calls():
143
- with pytest.raises(TypeError):
144
- peek.new(a=1)
145
- with pytest.raises(TypeError):
146
- peek.clone(a=1)
147
- with pytest.raises(TypeError):
148
- peek.configure(a=1)
149
- with pytest.raises(TypeError):
150
- peek(12, a=1)
151
- with pytest.raises(TypeError):
152
- peek(a=1)
153
-
154
-
155
- def test_output(capsys):
156
- with tempfile.TemporaryDirectory() as tmpdir: # we can't use tmpdir from pytest because of Python 2.7 compatibity
157
- g.result = ""
158
-
159
- def my_output(s):
160
- g.result += s + "\n"
161
-
162
- hello = "world"
163
- peek(hello, output=print)
164
- out, err = capsys.readouterr()
165
- assert out == "peek| hello: 'world'\n"
166
- assert err == ""
167
- peek(hello, output=sys.stdout)
168
- out, err = capsys.readouterr()
169
- assert out == "peek| hello: 'world'\n"
170
- assert err == ""
171
- peek(hello, output="stdout")
172
- out, err = capsys.readouterr()
173
- assert out == "peek| hello: 'world'\n"
174
- assert err == ""
175
- peek(hello, output="")
176
- out, err = capsys.readouterr()
177
- assert out == ""
178
- assert err == ""
179
- peek(hello, output="null")
180
- out, err = capsys.readouterr()
181
- assert out == ""
182
- assert err == ""
183
- peek(hello, output=print)
184
- out, err = capsys.readouterr()
185
- assert out == "peek| hello: 'world'\n"
186
- assert err == ""
187
-
188
- if True:
189
- path = Path(tmpdir) / "x0"
190
- peek(hello, output=path)
191
- out, err = capsys.readouterr()
192
- assert out == ""
193
- assert err == ""
194
- with path.open("r") as f:
195
- assert f.read() == "peek| hello: 'world'\n"
196
-
197
- path = Path(tmpdir) / "x1"
198
- peek(hello, output=path)
199
- out, err = capsys.readouterr()
200
- assert out == ""
201
- assert err == ""
202
- with path.open("r") as f:
203
- assert f.read() == "peek| hello: 'world'\n"
204
-
205
- path = Path(tmpdir) / "x2"
206
- with path.open("a+") as f:
207
- peek(hello, output=f)
208
- with pytest.raises(TypeError): # closed file
209
- peek(hello, output=f)
210
- out, err = capsys.readouterr()
211
- assert out == ""
212
- assert err == ""
213
- with path.open("r") as f:
214
- assert f.read() == "peek| hello: 'world'\n"
215
-
216
- with pytest.raises(TypeError):
217
- peek(hello, output=1)
218
-
219
- peek(hello, output=my_output)
220
- peek(1, output=my_output)
221
- out, err = capsys.readouterr()
222
- assert out == ""
223
- assert out == ""
224
- assert g.result == "peek| hello: 'world'\npeek| 1\n"
225
-
226
- def test_serialize(capsys):
227
- def serialize(s):
228
- return repr(s) + " [len=" + str(len(s)) + "]"
229
-
230
- hello = "world"
231
- peek(hello, serialize=serialize)
232
- out, err = capsys.readouterr()
233
- assert out == "peek| hello: 'world' [len=5]\n"
234
-
235
- def test_show_time(capsys):
236
- hello = "world"
237
- peek(hello, show_time=True)
238
- out, err = capsys.readouterr()
239
- assert out.endswith("hello: 'world'\n")
240
- assert "@ " in err
241
-
242
- def test_show_delta(capsys):
243
- hello = "world"
244
- peek(hello, show_delta=True)
245
- out, err = capsys.readouterr()
246
- assert out.endswith("hello: 'world'\n")
247
- assert "delta=" in err
248
-
249
- def test_as_str(capsys):
250
- hello = "world"
251
- s = peek(hello, as_str=True)
252
- peek(hello)
253
- out, err = capsys.readouterr()
254
- assert out == s
255
-
256
- with pytest.raises(TypeError):
257
-
258
- @peek(as_str=True)
259
- def add2(x):
260
- return x + 2
261
-
262
- with pytest.raises(TypeError):
263
- with peek(as_str=True):
264
- pass
265
-
266
-
267
- def test_clone():
268
- hello = "world"
269
- z = peek.clone()
270
- z.configure(prefix="z| ")
271
- sy = peek(hello, as_str=True)
272
- with peek.preserve():
273
- peek.configure(show_line_number=True)
274
- sz = z(hello, as_str=True)
275
- assert sy.replace("peek", "z") == sz
276
-
277
-
278
- def test_sort_dicts():
279
- world = {"EN": "world", "NL": "wereld", "FR": "monde", "DE": "Welt"}
280
- s0 = peek(world, as_str=True)
281
- s1 = peek(world, sort_dicts=False, as_str=True)
282
- s2 = peek(world, sort_dicts=True, as_str=True)
283
- if sys.version_info >= (3, 8):
284
- assert s0 == s1 == "peek| world: {'EN': 'world', 'NL': 'wereld', 'FR': 'monde', 'DE': 'Welt'}\n"
285
- assert s2 == "peek| world: {'DE': 'Welt', 'EN': 'world', 'FR': 'monde', 'NL': 'wereld'}\n"
286
- else:
287
- assert s0 == s1 == s2 == "peek| world: {'DE': 'Welt', 'EN': 'world', 'FR': 'monde', 'NL': 'wereld'}\n"
288
-
289
-
290
- def test_underscore_numbers():
291
- numbers = dict(x1=1, x2=1000, x3=1000000, x4=1234567890)
292
- s0 = peek(numbers, as_str=True)
293
- s1 = peek(numbers, underscore_numbers=True, as_str=True)
294
- s2 = peek(numbers, un=False, as_str=True)
295
-
296
- if sys.version_info >= (3, 8):
297
- assert s0 == s2 == "peek| numbers: {'x1': 1, 'x2': 1000, 'x3': 1000000, 'x4': 1234567890}\n"
298
- assert s1 == "peek| numbers: {'x1': 1, 'x2': 1_000, 'x3': 1_000_000, 'x4': 1_234_567_890}\n"
299
- else:
300
- assert s0 == s1 == s2 == "peek| numbers: {'x1': 1, 'x2': 1000, 'x3': 1000000, 'x4': 1234567890}\n"
301
-
302
-
303
- @pytest.mark.skipif(Pythonista, reason="Pythonista problem")
304
- def test_multiline():
305
- a = 1
306
- b = 2
307
- ll = list(range(15))
308
- # fmt: off
309
- s = peek((a, b),
310
- [ll,
311
- ll], as_str=True)
312
- # fmt: on
313
- assert (
314
- s
315
- == """\
316
- peek|
317
- (a, b): (1, 2)
318
- [ll,
319
- ll]:
320
- [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
321
- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]]
322
- """
323
- )
324
-
325
- lines = "\n".join("line{i}".format(i=i) for i in range(4))
326
- result = peek(lines, as_str=True)
327
- assert (
328
- result
329
- == """\
330
- peek|
331
- lines:
332
- 'line0
333
- line1
334
- line2
335
- line3'
336
- """
337
- )
338
-
339
-
340
- def test_decorator(capsys):
341
- peek_module.fix_perf_counter(0)
342
-
343
- @peek
344
- def mul(x, y):
345
- return x * y
346
-
347
- @peek()
348
- def div(x, y):
349
- return x / y
350
-
351
- @peek(show_enter=False)
352
- def add(x, y):
353
- return x + y
354
-
355
- @peek(show_exit=False)
356
- def sub(x, y):
357
- return x - y
358
-
359
- @peek(show_enter=False, show_exit=False)
360
- def pos(x, y):
361
- return x**y
362
-
363
- assert mul(2, 3) == 2 * 3
364
- assert div(10, 2) == 10 / 2
365
- assert add(2, 3) == 2 + 3
366
- assert sub(10, 2) == 10 - 2
367
- assert pow(10, 2) == 10**2
368
- out, err = capsys.readouterr()
369
- assert (
370
- out
371
- == """\
372
- peek| called mul(2, 3)
373
- peek| returned 6 from mul(2, 3) in 0.000000 seconds
374
- peek| called div(10, 2)
375
- peek| returned 5.0 from div(10, 2) in 0.000000 seconds
376
- peek| returned 5 from add(2, 3) in 0.000000 seconds
377
- peek| called sub(10, 2)
378
- """
379
- )
380
- peek_module.fix_perf_counter(None)
381
-
382
-
383
- def test_decorator_edge_cases(capsys):
384
- peek_module.fix_perf_counter(0)
385
-
386
- @peek
387
- def mul(x, y, factor=1):
388
- return x * y * factor
389
-
390
- assert mul(5, 6) == 30
391
- assert mul(5, 6, 10) == 300
392
- assert mul(5, 6, factor=10) == 300
393
- out, err = capsys.readouterr()
394
- assert (
395
- out
396
- == """\
397
- peek| called mul(5, 6)
398
- peek| returned 30 from mul(5, 6) in 0.000000 seconds
399
- peek| called mul(5, 6, 10)
400
- peek| returned 300 from mul(5, 6, 10) in 0.000000 seconds
401
- peek| called mul(5, 6, factor=10)
402
- peek| returned 300 from mul(5, 6, factor=10) in 0.000000 seconds
403
- """
404
- )
405
- peek_module.fix_perf_counter(None)
406
-
407
-
408
- def test_decorator_with_methods(capsys):
409
- class Number:
410
- def __init__(self, value):
411
- self.value = value
412
-
413
- @peek(show_exit=False)
414
- def __mul__(self, other):
415
- if isinstance(other, Number):
416
- return self.value * other.value
417
- else:
418
- return self.value * other
419
-
420
- def __repr__(self):
421
- return self.__class__.__name__ + "(" + str(self.value) + ")"
422
-
423
- with peek.preserve():
424
- peek.output = "stderr"
425
- a = Number(2)
426
- b = Number(3)
427
- print(a * 2)
428
- print(a * b)
429
- out, err = capsys.readouterr()
430
- assert (
431
- err
432
- == """\
433
- peek| called __mul__(Number(2), 2)
434
- peek| called __mul__(Number(2), Number(3))
435
- """
436
- )
437
- assert (
438
- out
439
- == """4
440
- 6
441
- """
442
- )
443
-
444
-
445
- @pytest.mark.skipif(Pythonista, reason="Pythonista problem")
446
- def test_context_manager(capsys):
447
- peek_module.fix_perf_counter(0)
448
- with peek():
449
- peek(3)
450
- out, err = capsys.readouterr()
451
- assert (
452
- out
453
- == """\
454
- peek| enter
455
- peek| 3
456
- peek| exit in 0.000000 seconds
457
- """
458
- )
459
- peek_module.fix_perf_counter(None)
460
-
461
-
462
- def test_return_none(capsys):
463
- a = 2
464
- result = peek(a, a)
465
- assert result == (a, a)
466
- result = peek(a, a, return_none=True)
467
- assert result is None
468
- out, err = capsys.readouterr()
469
- assert (
470
- out
471
- == """\
472
- peek| a: 2, a: 2
473
- peek| a: 2, a: 2
474
- """
475
- )
476
-
477
-
478
- def test_read_json1():
479
- with tempfile.TemporaryDirectory() as tmpdir: # we can't use tmpdir from pytest because of Python 2.7 compatibity
480
- json_filename0 = Path(tmpdir) / "peek.json"
481
- with open(str(json_filename0), "w") as f:
482
- print('{"line_length":-1}', file=f)
483
- tmpdir1 = Path(tmpdir) / "peek"
484
- tmpdir1.mkdir()
485
- json_filename1 = Path(tmpdir1) / "peek.json"
486
- with open(str(json_filename1), "w") as f:
487
- print('{"line_length":-2}', file=f)
488
- save_sys_path = sys.path
489
-
490
- sys.path = [tmpdir] + [tmpdir1]
491
- peek_module.set_defaults()
492
- peek_module.apply_json()
493
- assert peek_module.default.line_length == -1
494
-
495
- sys.path = [str(tmpdir1)] + [tmpdir]
496
- peek_module.set_defaults()
497
- peek_module.apply_json()
498
- assert peek_module.default.line_length == -2
499
-
500
- sys.path = []
501
- peek_module.set_defaults()
502
- peek_module.apply_json()
503
- assert peek_module.default.line_length == 80
504
-
505
- with open(str(json_filename0), "w") as f:
506
- print('{"error":0}', file=f)
507
-
508
- sys.path = [tmpdir]
509
- with pytest.raises(ValueError):
510
- peek_module.set_defaults()
511
- peek_module.apply_json()
512
-
513
- sys.path = save_sys_path
514
-
515
-
516
- def test_read_json2():
517
- with tempfile.TemporaryDirectory() as tmpdir:
518
- json_filename = Path(tmpdir) / "peek.json"
519
- with open(str(json_filename), "w") as f:
520
- print('{"prefix": "xxx", "delta": 10}', file=f)
521
-
522
- sys.path = [tmpdir] + sys.path
523
- peek_module.set_defaults()
524
- peek_module.apply_json()
525
- sys.path.pop(0)
526
-
527
- y1 = peek.new()
528
-
529
- s = y1(3, as_str=True)
530
- assert s == "xxx3\n"
531
- assert 10 < y1.delta < 11
532
-
533
- with open(str(json_filename), "w") as f:
534
- print('{"prefix1": "xxx"}', file=f)
535
-
536
- sys.path = [tmpdir] + sys.path
537
- with pytest.raises(ValueError):
538
- peek_module.set_defaults()
539
- peek_module.apply_json()
540
- sys.path.pop(0)
541
-
542
- with open(str(json_filename), "w") as f:
543
- print('{"serialize": "xxx"}', file=f)
544
-
545
- sys.path = [tmpdir] + sys.path
546
- with pytest.raises(ValueError):
547
- peek_module.set_defaults()
548
- peek_module.apply_json()
549
-
550
- sys.path.pop(0)
551
-
552
- tmpdir = Path(tmpdir) / "peek"
553
- tmpdir.mkdir()
554
- json_filename = Path(tmpdir) / "peek.json"
555
- with open(str(json_filename), "w") as f:
556
- print('{"prefix": "yyy"}', file=f)
557
-
558
- sys.path = [str(tmpdir)] + sys.path
559
- peek_module.set_defaults()
560
- peek_module.apply_json()
561
- sys.path.pop(0)
562
-
563
- y1 = peek.new()
564
-
565
- s = y1(3, as_str=True)
566
- assert s == "yyy3\n"
567
-
568
- tmpdir = Path(tmpdir) / "peek"
569
- tmpdir.mkdir()
570
- json_filename = Path(tmpdir) / "peek.json"
571
- with open(str(json_filename), "w") as f:
572
- print("{}", file=f)
573
-
574
- sys.path = [str(tmpdir)] + sys.path
575
- peek_module.set_defaults()
576
- peek_module.apply_json()
577
- sys.path.pop(0)
578
-
579
-
580
- @pytest.mark.skipif(Pythonista, reason="Pythonista problem")
581
- def test_wrapping(capsys):
582
- l0 = "".join(" {c}".format(c=c) for c in "12345678") + "\n" + "".join(".........0" for c in "12345678")
583
-
584
- print(l0)
585
- ccc = cccc = 3 * ["12345678123456789012"]
586
- ccc0 = [cccc[0] + "0"] + cccc[1:]
587
- peek(ccc)
588
- peek(cccc)
589
- peek(ccc0)
590
-
591
- out, err = capsys.readouterr()
592
- assert (
593
- out
594
- == """\
595
- 1 2 3 4 5 6 7 8
596
- .........0.........0.........0.........0.........0.........0.........0.........0
597
- peek|
598
- ccc:
599
- ['12345678123456789012', '12345678123456789012', '12345678123456789012']
600
- peek|
601
- cccc:
602
- ['12345678123456789012', '12345678123456789012', '12345678123456789012']
603
- peek|
604
- ccc0:
605
- ['123456781234567890120',
606
- '12345678123456789012',
607
- '12345678123456789012']
608
- """
609
- )
610
- a = "1234"
611
- b = bb = 9 * ["123"]
612
- print(l0)
613
- peek(a, b)
614
- peek(a, bb)
615
- out, err = capsys.readouterr()
616
- assert (
617
- out
618
- == """\
619
- 1 2 3 4 5 6 7 8
620
- .........0.........0.........0.........0.........0.........0.........0.........0
621
- peek|
622
- a: '1234'
623
- b: ['123', '123', '123', '123', '123', '123', '123', '123', '123']
624
- peek|
625
- a: '1234'
626
- bb: ['123', '123', '123', '123', '123', '123', '123', '123', '123']
627
- """
628
- )
629
- dddd = 10 * ["123"]
630
- dddd = ddddd = 10 * ["123"]
631
- e = "a\nb"
632
- print(l0)
633
- peek(a, dddd)
634
- peek(a, ddddd)
635
- peek(e)
636
- out, err = capsys.readouterr()
637
- assert (
638
- out
639
- == """\
640
- 1 2 3 4 5 6 7 8
641
- .........0.........0.........0.........0.........0.........0.........0.........0
642
- peek|
643
- a: '1234'
644
- dddd: ['123', '123', '123', '123', '123', '123', '123', '123', '123', '123']
645
- peek|
646
- a: '1234'
647
- ddddd:
648
- ['123', '123', '123', '123', '123', '123', '123', '123', '123', '123']
649
- peek|
650
- e:
651
- 'a
652
- b'
653
- """
654
- )
655
- a = aa = 2 * ["0123456789ABC"]
656
- print(l0)
657
- peek(a, line_length=40)
658
- peek(aa, line_length=40)
659
- peek(aa, line_length=41)
660
- out, err = capsys.readouterr()
661
- assert (
662
- out
663
- == """\
664
- 1 2 3 4 5 6 7 8
665
- .........0.........0.........0.........0.........0.........0.........0.........0
666
- peek|
667
- a:
668
- ['0123456789ABC',
669
- '0123456789ABC']
670
- peek|
671
- aa:
672
- ['0123456789ABC',
673
- '0123456789ABC']
674
- peek|
675
- aa:
676
- ['0123456789ABC',
677
- '0123456789ABC']
678
- """
679
- )
680
-
681
-
682
- def test_compact(capsys):
683
- a = 9 * ["0123456789"]
684
- peek(a)
685
- peek(a, compact=True)
686
- out, err = capsys.readouterr()
687
- assert (
688
- out
689
- == """\
690
- peek|
691
- a:
692
- ['0123456789',
693
- '0123456789',
694
- '0123456789',
695
- '0123456789',
696
- '0123456789',
697
- '0123456789',
698
- '0123456789',
699
- '0123456789',
700
- '0123456789']
701
- peek|
702
- a:
703
- ['0123456789', '0123456789', '0123456789', '0123456789', '0123456789',
704
- '0123456789', '0123456789', '0123456789', '0123456789']
705
- """
706
- )
707
-
708
-
709
- def test_depth_indent(capsys):
710
- s = "=============================================="
711
- a = [s + "1", [s + "2", [s + "3", [s + "4"]]], s + "1"]
712
- peek(a, indent=4)
713
- peek(a, depth=2, indent=4)
714
- out, err = capsys.readouterr()
715
- assert (
716
- out
717
- == """\
718
- peek|
719
- a:
720
- [ '==============================================1',
721
- [ '==============================================2',
722
- [ '==============================================3',
723
- ['==============================================4']]],
724
- '==============================================1']
725
- peek|
726
- a:
727
- [ '==============================================1',
728
- ['==============================================2', [...]],
729
- '==============================================1']
730
- """
731
- )
732
-
733
-
734
- def test_enabled(capsys):
735
- with peek.preserve():
736
- peek("One")
737
- peek.configure(enabled=False)
738
- peek("Two")
739
- s = peek("Two", as_str=True)
740
- assert s == ""
741
- peek.configure(enabled=True)
742
- peek("Three")
743
-
744
- out, err = capsys.readouterr()
745
- assert (
746
- out
747
- == """\
748
- peek| 'One'
749
- peek| 'Three'
750
- """
751
- )
752
-
753
-
754
- def test_enabled2(capsys):
755
- with peek.preserve():
756
- peek.configure(enabled=False)
757
- line0 = peek("line0")
758
- noline0 = peek(prefix="no0")
759
- pair0 = peek("p0", "p0")
760
- s0 = peek("s0", as_str=True)
761
- peek.configure(enabled=[])
762
- line1 = peek("line1")
763
- noline1 = peek(prefix="no1")
764
- pair1 = peek("p1", "p1")
765
- s1 = peek("s1", as_str=True)
766
- peek.configure(enabled=True)
767
- line2 = peek("line2")
768
- noline2 = peek(prefix="no2")
769
- pair2 = peek("p2", "p2")
770
- s2 = peek("s2", as_str=True)
771
- out, err = capsys.readouterr()
772
- assert "line0" not in out and "p0" not in out and "no0" not in out
773
- assert "line1" not in out and "p1" not in out and "no1" not in out
774
- assert "line2" in out and "p2" in out and "no2" in out
775
- assert line0 == "line0"
776
- assert line1 == "line1"
777
- assert line2 == "line2"
778
- assert noline0 is None
779
- assert noline1 is None
780
- assert noline2 is None
781
- assert pair0 == ("p0", "p0")
782
- assert pair1 == ("p1", "p1")
783
- assert pair2 == ("p2", "p2")
784
- assert s0 == ""
785
- assert s1 == ""
786
- assert s2 == "peek| 's2'\n"
787
-
788
-
789
- def test_enabled3(capsys):
790
- with peek.preserve():
791
- peek.configure(enabled=[])
792
- peek(2)
793
- with pytest.raises(TypeError):
794
-
795
- @peek()
796
- def add2(x):
797
- return x + 2
798
-
799
- with pytest.raises((AttributeError, TypeError)):
800
- with peek():
801
- pass
802
-
803
- @peek(decorator=True)
804
- def add2(x):
805
- return x + 2
806
-
807
- with peek(context_manager=True):
808
- pass
809
-
810
-
811
- def test_multiple_as():
812
- with pytest.raises(TypeError):
813
- peek(1, decorator=True, context_manager=True)
814
- with pytest.raises(TypeError):
815
- peek(1, decorator=True, as_str=True)
816
- with pytest.raises(TypeError):
817
- peek(1, context_manager=True, as_str=True)
818
-
819
-
820
- def test_wrap_indent():
821
- s = 4 * ["*******************"]
822
- res = peek(s, compact=True, as_str=True)
823
- assert res.splitlines()[1].startswith(" s")
824
- res = peek(s, compact=True, as_str=True, wrap_indent="....")
825
- assert res.splitlines()[1].startswith("....s")
826
- res = peek(s, compact=True, as_str=True, wrap_indent=2)
827
- assert res.splitlines()[1].startswith(" s")
828
- res = peek(s, compact=True, as_str=True, wrap_indent=[])
829
- assert res.splitlines()[1].startswith("[]s")
830
-
831
-
832
- def test_traceback(capsys):
833
- with peek.preserve():
834
- peek.show_traceback = True
835
- peek()
836
- out, err = capsys.readouterr()
837
- assert out.count("traceback") == 2
838
-
839
- @peek
840
- def p():
841
- pass
842
-
843
- p()
844
- out, err = capsys.readouterr()
845
- assert out.count("traceback") == 2
846
- with peek():
847
- pass
848
- out, err = capsys.readouterr()
849
- assert out.count("traceback") == 2
850
-
851
-
852
- @pytest.mark.skipif(Pythonista, reason="Pythonista problem")
853
- def test_enforce_line_length(capsys):
854
- s = 80 * "*"
855
- peek(s)
856
- peek(s, enforce_line_length=True)
857
- out, err = capsys.readouterr()
858
- assert (
859
- out
860
- == """\
861
- peek|
862
- s: '********************************************************************************'
863
- peek|
864
- s: '************************************************************************
865
- """
866
- )
867
- with peek.preserve():
868
- peek.configure(line_length=20, show_line_number=True)
869
- peek()
870
- out1, err = capsys.readouterr()
871
- peek(enforce_line_length=True)
872
- out2, err = capsys.readouterr()
873
- out1 = out1.rstrip("\n")
874
- out2 = out2.rstrip("\n")
875
- assert len(out2) == 20
876
- assert out1[10:20] == out2[10:20]
877
- assert len(out1) > 20
878
- res = peek("abcdefghijklmnopqrstuvwxyz", pr="", ell=1, ll=20, as_str=True).rstrip("\n")
879
- assert res == "'abcdefghijklmnopqrs"
880
- assert len(res) == 20
881
-
882
-
883
- def test_check_output(capsys):
884
- """special Pythonista code, as that does not reload x1 and x2"""
885
- if "x1" in sys.modules:
886
- del sys.modules["x1"]
887
- if "x2" in sys.modules:
888
- del sys.modules["x2"]
889
- del sys.modules["peek"]
890
- import peek
891
-
892
- """ end of special Pythonista code """
893
- with peek.preserve():
894
- with tempfile.TemporaryDirectory() as tmpdir:
895
- x1_file = Path(tmpdir) / "x1.py"
896
- with open(str(x1_file), "w") as f:
897
- print(
898
- """\
899
- def check_output():
900
- import peek
901
- import x2
902
-
903
- peek.configure(show_line_number=True, show_exit= False)
904
- x2.test()
905
- peek(1)
906
- peek(
907
- 1
908
- )
909
- with peek(prefix="==>"):
910
- peek()
911
-
912
- with peek(
913
-
914
-
915
-
916
- prefix="==>"
917
-
918
- ):
919
- peek()
920
-
921
- @peek
922
- def x(a, b=1):
923
- pass
924
- x(2)
925
-
926
- @peek()
927
-
928
-
929
-
930
-
931
- def x(
932
-
933
-
934
- ):
935
- pass
936
-
937
- x()
938
- """,
939
- file=f,
940
- )
941
-
942
- x2_file = Path(tmpdir) / "x2.py"
943
- with open(str(x2_file), "w") as f:
944
- print(
945
- """\
946
- import peek
947
-
948
- def test():
949
- @peek()
950
- def myself(x):
951
- peek(x)
952
- return x
953
-
954
- myself(6)
955
- with peek():
956
- pass
957
- """,
958
- file=f,
959
- )
960
- sys.path = [tmpdir] + sys.path
961
- import x1
962
-
963
- x1.check_output()
964
- sys.path.pop(0)
965
- out, err = capsys.readouterr()
966
- assert (
967
- out
968
- == """\
969
- peek| #5[x2.py] in test() ==> called myself(6)
970
- peek| #6[x2.py] in test.myself() ==> x: 6
971
- peek| #10[x2.py] in test() ==> enter
972
- peek| #7[x1.py] in check_output() ==> 1
973
- peek| #8[x1.py] in check_output() ==> 1
974
- ==>#11[x1.py] in check_output() ==> enter
975
- peek| #12[x1.py] in check_output()
976
- ==>#14[x1.py] in check_output() ==> enter
977
- peek| #21[x1.py] in check_output()
978
- peek| #24[x1.py] in check_output() ==> called x(2)
979
- peek| #33[x1.py] in check_output() ==> called x()
980
- """
981
- )
982
-
983
-
984
- def test_provided(capsys):
985
- with peek.preserve():
986
- peek("1")
987
- peek("2", provided=True)
988
- peek("3", provided=False)
989
- peek.enabled = False
990
- peek("4")
991
- peek("5", provided=True)
992
- peek("6", provided=False)
993
- out, err = capsys.readouterr()
994
- assert (
995
- out
996
- == """\
997
- peek| '1'
998
- peek| '2'
999
- """
1000
- )
1001
-
1002
-
1003
- def test_assert_():
1004
- peek.assert_(True)
1005
- with pytest.raises(AssertionError):
1006
- peek.assert_(False)
1007
-
1008
- with peek.preserve():
1009
- peek.enabled = False
1010
- peek.assert_(True)
1011
- peek.assert_(False)
1012
-
1013
-
1014
- def test_propagation():
1015
- with peek.preserve():
1016
- y0 = peek.fork()
1017
- y1 = y0.fork()
1018
- peek.prefix = "x"
1019
- y2 = peek.clone()
1020
-
1021
- assert peek.prefix == "x"
1022
- assert y0.prefix == "x"
1023
- assert y1.prefix == "x"
1024
- assert y2.prefix == "x"
1025
-
1026
- y1.prefix = "xx"
1027
- assert peek.prefix == "x"
1028
- assert y0.prefix == "x"
1029
- assert y1.prefix == "xx"
1030
- assert y2.prefix == "x"
1031
-
1032
- y1.prefix = None
1033
- assert peek.prefix == "x"
1034
- assert y0.prefix == "x"
1035
- assert y1.prefix == "x"
1036
- assert y2.prefix == "x"
1037
-
1038
- peek.prefix = None
1039
- assert peek.prefix == "peek| "
1040
- assert y0.prefix == "peek| "
1041
- assert y1.prefix == "peek| "
1042
- assert y2.prefix == "x"
1043
-
1044
-
1045
- def test_delta_propagation():
1046
- with peek.preserve():
1047
- y_delta_start = peek.delta
1048
- y0 = peek.fork()
1049
- y1 = y0.fork()
1050
- peek.dl = 100
1051
- y2 = peek.clone()
1052
-
1053
- assert 100 < peek.delta < 110
1054
- assert 100 < y0.delta < 110
1055
- assert 100 < y1.delta < 110
1056
- assert 100 < y2.delta < 110
1057
-
1058
- y1.delta = 200
1059
- assert 100 < peek.delta < 110
1060
- assert 100 < y0.delta < 110
1061
- assert 200 < y1.delta < 210
1062
- assert 100 < y2.delta < 110
1063
-
1064
- y1.delta = None
1065
- assert 100 < peek.delta < 110
1066
- assert 100 < y0.delta < 110
1067
- assert 100 < y1.delta < 110
1068
- assert 100 < y2.delta < 110
1069
-
1070
- peek.delta = None
1071
- assert 0 < peek.delta < y_delta_start + 10
1072
- assert 0 < y0.delta < y_delta_start + 10
1073
- assert 0 < y1.delta < y_delta_start + 10
1074
- assert 100 < y2.delta < 110
1075
-
1076
-
1077
- def test_separator(capsys):
1078
- a = 12
1079
- b = 4 * ["test"]
1080
- peek(a, b)
1081
- peek(a, b, sep="")
1082
- peek(a, separator="")
1083
- out, err = capsys.readouterr()
1084
- assert (
1085
- out
1086
- == """\
1087
- peek| a: 12, b: ['test', 'test', 'test', 'test']
1088
- peek|
1089
- a: 12
1090
- b: ['test', 'test', 'test', 'test']
1091
- peek| a: 12
1092
- """
1093
- )
1094
-
1095
-
1096
- def test_equals_separator(capsys):
1097
- a = 12
1098
- b = 4 * ["test"]
1099
- peek(a, b)
1100
- peek(a, b, equals_separator=" ==> ")
1101
- peek(a, b, es=" = ")
1102
-
1103
- out, err = capsys.readouterr()
1104
- assert (
1105
- out
1106
- == """\
1107
- peek| a: 12, b: ['test', 'test', 'test', 'test']
1108
- peek| a ==> 12, b ==> ['test', 'test', 'test', 'test']
1109
- peek| a = 12, b = ['test', 'test', 'test', 'test']
1110
- """
1111
- )
1112
-
1113
-
1114
- def test_context_separator(capsys):
1115
- a = 12
1116
- b = 2 * ["test"]
1117
- peek(a, b, show_line_number=True)
1118
- peek(a, b, sln=1, context_separator=" ... ")
1119
-
1120
- out, err = capsys.readouterr()
1121
- lines = out.split("\n")
1122
- assert lines[0].endswith(" ==> a: 12, b: ['test', 'test']")
1123
- assert lines[1].endswith(" ... a: 12, b: ['test', 'test']")
1124
-
1125
-
1126
- def test_wrap_indent1(capsys):
1127
- with peek.preserve():
1128
- peek.separator = ""
1129
- peek(1, 2)
1130
- peek(1, 2, prefix="p| ")
1131
- peek(1, 2, prefix="mypeek| ")
1132
- peek.wrap_indent = "...."
1133
- peek(1, 2, prefix="p| ")
1134
- peek(1, 2, prefix="mypeek| ")
1135
- out, err = capsys.readouterr()
1136
- assert (
1137
- out
1138
- == """\
1139
- peek|
1140
- 1
1141
- 2
1142
- p| 1
1143
- 2
1144
- mypeek|
1145
- 1
1146
- 2
1147
- p| 1
1148
- ....2
1149
- mypeek|
1150
- ....1
1151
- ....2
1152
- """
1153
- )
1154
-
1155
-
1156
- def test_fstrings(capsys):
1157
- hello = "world"
1158
-
1159
- with peek.preserve():
1160
- peek("hello, world")
1161
- peek(hello)
1162
- peek(f"hello={hello}")
1163
-
1164
- with peek.preserve():
1165
- peek.values_only = True
1166
- peek("hello, world")
1167
- peek(hello)
1168
- peek(f"hello={hello}")
1169
-
1170
- with peek.preserve():
1171
- peek.values_only_for_fstrings = True
1172
- peek("hello, world")
1173
- peek(hello)
1174
- peek(f"hello={hello}")
1175
-
1176
- with peek.preserve():
1177
- peek.voff = True
1178
- peek.vo = True
1179
- peek("hello, world")
1180
- peek(hello)
1181
- peek(f"hello={hello}")
1182
-
1183
- out, err = capsys.readouterr()
1184
- assert (
1185
- out
1186
- == """\
1187
- peek| 'hello, world'
1188
- peek| hello: 'world'
1189
- peek| f"hello={hello}": 'hello=world'
1190
- peek| 'hello, world'
1191
- peek| 'world'
1192
- peek| 'hello=world'
1193
- peek| 'hello, world'
1194
- peek| hello: 'world'
1195
- peek| 'hello=world'
1196
- peek| 'hello, world'
1197
- peek| 'world'
1198
- peek| 'hello=world'
1199
- """
1200
- )
1201
-
1202
-
1203
- if __name__ == "__main__":
1204
- pytest.main(["-vv", "-s", "-x", __file__])
1205
-
File without changes
File without changes