peek-python 26.0.3__tar.gz → 26.0.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.4
2
2
  Name: peek-python
3
- Version: 26.0.3
3
+ Version: 26.0.4
4
4
  Summary: peek - like print, but easy
5
5
  Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/salabim/peek
@@ -23,6 +23,10 @@ Do you debug your code with `print()` or `log()`?
23
23
  If so, peek will make printing debug information really easy.
24
24
  And on top of that, you get some basic benchmarking functionality.
25
25
 
26
+ ## Changelog
27
+
28
+ For the changelog, see www.salabim.org/peek/changelog .
29
+
26
30
  ## Table of contents
27
31
 
28
32
  * [Installation](#installation)
@@ -75,7 +79,6 @@ And on top of that, you get some basic benchmarking functionality.
75
79
 
76
80
  * [Contact info](#contact-info)
77
81
 
78
-
79
82
  ## Installation
80
83
 
81
84
  Installing peek with pip is easy.
@@ -6,6 +6,10 @@ Do you debug your code with `print()` or `log()`?
6
6
  If so, peek will make printing debug information really easy.
7
7
  And on top of that, you get some basic benchmarking functionality.
8
8
 
9
+ ## Changelog
10
+
11
+ For the changelog, see www.salabim.org/peek/changelog .
12
+
9
13
  ## Table of contents
10
14
 
11
15
  * [Installation](#installation)
@@ -58,7 +62,6 @@ And on top of that, you get some basic benchmarking functionality.
58
62
 
59
63
  * [Contact info](#contact-info)
60
64
 
61
-
62
65
  ## Installation
63
66
 
64
67
  Installing peek with pip is easy.
@@ -34,7 +34,7 @@ import pprint
34
34
  import builtins
35
35
  import shutil
36
36
 
37
- __version__ = "26.0.3"
37
+ __version__ = "26.0.4"
38
38
 
39
39
  from pathlib import Path
40
40
 
@@ -46,6 +46,7 @@ if Pythonista:
46
46
  import console
47
47
  else:
48
48
  import colorama
49
+
49
50
  colorama.just_fix_windows_console()
50
51
 
51
52
  try:
@@ -169,75 +170,75 @@ class _Peek:
169
170
 
170
171
  if value is None:
171
172
  return
172
- if name == "output":
173
- if callable(value):
174
- return
175
- if isinstance(value, (str, Path)):
176
- return
177
- try:
178
- value.write("")
179
- return
180
- except Exception:
181
- pass
182
- raise AttributeError("output should be a callable, str, Path or open text file.")
173
+ match name:
174
+ case "output":
175
+ if callable(value):
176
+ return
177
+ if isinstance(value, (str, Path)):
178
+ return
179
+ try:
180
+ value.write("")
181
+ return
182
+ except Exception:
183
+ pass
184
+ raise AttributeError("output should be a callable, str, Path or open text file.")
183
185
 
184
- elif name == "serialize":
185
- if callable(value):
186
- return
186
+ case "serialize":
187
+ if callable(value):
188
+ return
187
189
 
188
- elif name in ("color", "color_value"):
189
- if isinstance(value, str) and value in _Peek._color_name_to_ANSI:
190
- return
191
- if isinstance(value, int) and value in _Peek.id_to_color:
192
- return
190
+ case "color" | "color_value":
191
+ if isinstance(value, str) and value in _Peek._color_name_to_ANSI:
192
+ return
193
+ if isinstance(value, int) and value in _Peek.id_to_color:
194
+ return
193
195
 
194
- elif name == "delta":
195
- if isinstance(value, numbers.Number):
196
- return
196
+ case "delta":
197
+ if isinstance(value, numbers.Number):
198
+ return
197
199
 
198
- elif name == "line_length":
199
- if (isinstance(value, numbers.Number) and value >= 0) or value == "terminal_width":
200
- return
200
+ case "line_length":
201
+ if (isinstance(value, numbers.Number) and value >= 0) or value == "terminal_width":
202
+ return
201
203
 
202
- elif name == "indent":
203
- if isinstance(value, numbers.Number) and value >= 0:
204
- return
204
+ case "indent":
205
+ if isinstance(value, numbers.Number) and value >= 0:
206
+ return
205
207
 
206
- elif name == "level":
207
- if isinstance(value, numbers.Number):
208
- return
208
+ case "level":
209
+ if isinstance(value, numbers.Number):
210
+ return
209
211
 
210
- elif name == "max_lines":
211
- if isinstance(value, numbers.Number) and value > 0:
212
- return
212
+ case "max_lines":
213
+ if isinstance(value, numbers.Number) and value > 0:
214
+ return
213
215
 
214
- elif name == "wrap_indent":
215
- if isinstance(value, str):
216
- return
217
- if isinstance(value, numbers.Number):
218
- if value > 0:
216
+ case "wrap_indent":
217
+ if isinstance(value, str):
219
218
  return
219
+ if isinstance(value, numbers.Number):
220
+ if value > 0:
221
+ return
220
222
 
221
- elif name == "format":
222
- if isinstance(value, str):
223
- return
224
- try:
225
- if all(isinstance(sub_format, str) for sub_format in value):
223
+ case "format":
224
+ if isinstance(value, str):
226
225
  return
227
- except TypeError:
228
- ...
226
+ try:
227
+ if all(isinstance(sub_format, str) for sub_format in value):
228
+ return
229
+ except TypeError:
230
+ ...
229
231
 
230
- elif name == "filter":
231
- if value.strip() == "":
232
- return
233
- try:
234
- eval(value, _Peek.name_and_alias_default)
232
+ case "filter":
233
+ if value.strip() == "":
234
+ return
235
+ try:
236
+ eval(value, _Peek.name_and_alias_default)
237
+ return
238
+ except Exception:
239
+ ...
240
+ case _:
235
241
  return
236
- except Exception:
237
- ...
238
-
239
- else:
240
- return
241
242
 
242
243
  raise AttributeError(f"incorrect {name_org}: {repr(value)}{_Peek.in_read_toml_message}")
243
244
 
@@ -322,11 +323,13 @@ class _Peek:
322
323
  def return_args(args, return_none):
323
324
  if return_none:
324
325
  return None
325
- if len(args) == 0:
326
- return None
327
- if len(args) == 1:
328
- return args[0]
329
- return args
326
+ match len(args):
327
+ case 0:
328
+ return None
329
+ case 1:
330
+ return args[0]
331
+ case _:
332
+ return args
330
333
 
331
334
  @staticmethod
332
335
  def perf_counter():
@@ -359,20 +362,21 @@ class _Peek:
359
362
  node = self
360
363
  while item not in node._attributes or node._attributes[item] is None:
361
364
  node = node._parent
362
- if item == "delta":
363
- return _Peek.perf_counter() - node._attributes["delta1"] + node._attributes["delta"]
364
- elif item == "delta1":
365
- return node._attributes["delta"]
366
- elif item == "prefix":
367
- prefix = node._attributes[item]
368
- return str(prefix() if callable(prefix) else prefix)
369
- else:
370
- return node._attributes[item]
365
+ match item:
366
+ case "delta":
367
+ return _Peek.perf_counter() - node._attributes["delta1"] + node._attributes["delta"]
368
+ case "delta1":
369
+ return node._attributes["delta"]
370
+ case "prefix":
371
+ prefix = node._attributes[item]
372
+ return str(prefix() if callable(prefix) else prefix)
373
+ case _:
374
+ return node._attributes[item]
371
375
  else:
372
376
  return self.__getattribute__(item)
373
377
 
374
378
  def __setattr__(self, item, value):
375
- if item in ("_parent", "_is_context_manager", "_line_number_with_filename_and_parent", "_save_traceback", "_enter_time", "_as_str", "_attributes"):
379
+ if item in ("_parent", "_line_number_with_filename_and_parent", "_save_traceback", "_enter_time", "_as_str", "_attributes"):
376
380
  return super().__setattr__(item, value)
377
381
  self._attributes.update(_Peek.spec_to_attributes(**{item: value}))
378
382
 
@@ -393,10 +397,12 @@ class _Peek:
393
397
  _Peek._fixed_perf_counter = val
394
398
 
395
399
  def do_show(self):
400
+ if not self.enabled:
401
+ return False
396
402
  if self.filter.strip() != "":
397
403
  if not eval(self.filter, {name: getattr(self, name) for name in list(_Peek.name_default) + list(_Peek.alias_default)}):
398
404
  return False
399
- return self.enabled
405
+ return True
400
406
 
401
407
  def print(self, *args, as_str=False, **kwargs):
402
408
  if "print" in kwargs and "print_like" in kwargs:
@@ -417,8 +423,19 @@ class _Peek:
417
423
  else:
418
424
  pairs.append(Pair(left=left, right=right))
419
425
 
420
- any_args = bool(args)
421
426
  this = self.fork(**kwargs)
427
+ if not this.do_show():
428
+ if this.decorator:
429
+ return lambda x: x
430
+ if this.context_manager:
431
+ return this
432
+ if as_str:
433
+ return ""
434
+ else:
435
+ return _Peek.return_args(args, this.return_none)
436
+
437
+ any_args = bool(args)
438
+
422
439
  if this.line_length in (0, "terminal_width"):
423
440
  this.line_length = shutil.get_terminal_size().columns
424
441
 
@@ -445,14 +462,6 @@ class _Peek:
445
462
  if this.decorator and this.context_manager:
446
463
  raise AttributeError("not allowed to specify both decorator and context_manager")
447
464
 
448
- if not this.decorator and not this.do_show():
449
- if as_str:
450
- return ""
451
- else:
452
- return _Peek.return_args(args, this.return_none)
453
-
454
- self._is_context_manager = False
455
-
456
465
  Pair = collections.namedtuple("Pair", "left right")
457
466
 
458
467
  call_frame = inspect.currentframe()
@@ -513,22 +522,19 @@ class _Peek:
513
522
  args_kwargs = [repr(arg) for arg in args] + [f"{k}={repr(v)}" for k, v in kwargs.items()]
514
523
  function_arguments = function.__name__ + "(" + (", ".join(args_kwargs)) + ")"
515
524
 
516
- if this.show_enter and this.do_show():
525
+ if this.show_enter:
517
526
  this.do_output(f"{context}called {function_arguments}{this.traceback()}")
518
527
  result = function(*args, **kwargs)
519
528
  duration = _Peek.perf_counter() - enter_time
520
529
 
521
530
  context = this.context()
522
- if this.show_exit and this.do_show():
531
+ if this.show_exit:
523
532
  this.do_output(f"{context}returned {repr(result)} from {function_arguments} in {duration:.6f} seconds{this.traceback()}")
524
533
 
525
534
  return result
526
535
 
527
536
  return wrapper
528
537
 
529
- if not this.do_show() or (not this.show_enter and not this.show_exit):
530
- return lambda x: x
531
-
532
538
  if len(args) == 0:
533
539
  return real_decorator
534
540
 
@@ -551,8 +557,6 @@ class _Peek:
551
557
  raise TypeError("as_str may not be True when peek used as context manager")
552
558
  if any_args:
553
559
  raise TypeError("non-keyword arguments are not allowed when peek used as context manager")
554
-
555
- this._is_context_manager = True
556
560
  return this
557
561
 
558
562
  out = ""
@@ -687,8 +691,8 @@ class _Peek:
687
691
  self._attributes = save
688
692
 
689
693
  def __enter__(self):
690
- if not hasattr(self, "_is_context_manager"):
691
- raise ValueError("not allowed as context_manager")
694
+ if not self.do_show():
695
+ return self
692
696
  self._save_traceback = self.traceback()
693
697
  self._enter_time = _Peek.perf_counter()
694
698
  if self.show_enter:
@@ -697,18 +701,19 @@ class _Peek:
697
701
  return self
698
702
 
699
703
  def __exit__(self, *args):
700
- if self.show_exit and self.do_show():
704
+ if not self.do_show():
705
+ return self
706
+ if self.show_exit:
701
707
  context = self.context()
702
708
  duration = _Peek.perf_counter() - self._enter_time
703
709
  self.do_output(f"{context}exit in {duration:.6f} seconds{self._save_traceback}")
704
- self._is_context_manager = False
705
710
 
706
711
  def context(self, omit_line_number=False, omit_context_separator=False):
707
712
  if not omit_line_number and self.show_line_number and self._line_number_with_filename_and_parent != "":
708
713
  parts = [self._line_number_with_filename_and_parent]
709
714
  else:
710
715
  parts = []
711
- if self.show_time and self.do_show():
716
+ if self.show_time:
712
717
  parts.append("@ " + str(datetime.datetime.now().strftime("%H:%M:%S.%f")))
713
718
 
714
719
  if self.show_delta:
@@ -749,38 +754,39 @@ class _Peek:
749
754
  self.output(s_end, end="")
750
755
  else:
751
756
  self.output(s_end)
752
- elif self.output in ("stdout", "stderr"):
753
- file = sys.stdout if self.output == "stdout" else sys.stderr
754
- if Pythonista:
755
- _Peek.print_pythonista_color(s_end, end="", file=file)
756
- # elif Pyodide: # not handled via use_color
757
- # _Peek.print_without_color(s, end=self.end, file=file)
758
- else:
759
- print(s_end, end="", file=file)
760
- elif self.output == "logging.debug":
761
- logging.debug(s)
762
- elif self.output == "logging.info":
763
- logging.info(s)
764
- elif self.output == "logging.warning":
765
- logging.warning(s)
766
- elif self.output == "logging.error":
767
- logging.error(s)
768
- elif self.output == "logging.critical":
769
- logging.critical(s)
770
- elif self.output in ("", "null"):
771
- pass
772
- elif isinstance(self.output, str):
773
- with open(self.output, "a+", encoding="utf-8") as f:
774
- print(s_end, file=f, end="")
775
- elif isinstance(self.output, Path):
776
- with self.output.open("a+", encoding="utf-8") as f:
777
- print(s_end, file=f, end="")
778
757
  else:
779
- print(s_end, file=self.output, end="")
758
+ match self.output:
759
+ case "stdout" | "stderr":
760
+ file = sys.stdout if self.output == "stdout" else sys.stderr
761
+ if Pythonista:
762
+ _Peek.print_pythonista_color(s_end, end="", file=file)
763
+ else:
764
+ print(s_end, end="", file=file)
765
+ case "logging.debug":
766
+ logging.debug(s)
767
+ case "logging.info":
768
+ logging.info(s)
769
+ case "logging.warning":
770
+ logging.warning(s)
771
+ case "logging.error":
772
+ logging.error(s)
773
+ case "logging.critical":
774
+ logging.critical(s)
775
+ case "" | "null":
776
+ pass
777
+ case _ if isinstance(self.output, str):
778
+ with open(self.output, "a+", encoding="utf-8") as f:
779
+ print(s_end, file=f, end="")
780
+ case _ if isinstance(self.output, Path):
781
+ with self.output.open("a+", encoding="utf-8") as f:
782
+ print(s_end, file=f, end="")
783
+ case _:
784
+ print(s_end, file=self.output, end="")
780
785
 
781
786
  def copy_to_clipboard(self, value, confirm=True):
782
787
  if Pythonista:
783
788
  import clipboard
789
+
784
790
  clipboard.set(str(value))
785
791
  else:
786
792
  try:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: peek-python
3
- Version: 26.0.3
3
+ Version: 26.0.4
4
4
  Summary: peek - like print, but easy
5
5
  Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/salabim/peek
@@ -23,6 +23,10 @@ Do you debug your code with `print()` or `log()`?
23
23
  If so, peek will make printing debug information really easy.
24
24
  And on top of that, you get some basic benchmarking functionality.
25
25
 
26
+ ## Changelog
27
+
28
+ For the changelog, see www.salabim.org/peek/changelog .
29
+
26
30
  ## Table of contents
27
31
 
28
32
  * [Installation](#installation)
@@ -75,7 +79,6 @@ And on top of that, you get some basic benchmarking functionality.
75
79
 
76
80
  * [Contact info](#contact-info)
77
81
 
78
-
79
82
  ## Installation
80
83
 
81
84
  Installing peek with pip is easy.
@@ -10,7 +10,7 @@ authors = [
10
10
  { name = "Ruud van der Ham", email = "rt.van.der.ham@gmail.com" },
11
11
  ]
12
12
  description = "peek - like print, but easy"
13
- version = "26.0.3"
13
+ version = "26.0.4"
14
14
  readme = "README.md"
15
15
  requires-python = ">=3.9"
16
16
  dependencies = [
File without changes