peek-python 25.0.6__tar.gz → 25.0.8__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.2
2
2
  Name: peek-python
3
- Version: 25.0.6
3
+ Version: 25.0.8
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/peek
@@ -343,7 +343,7 @@ a number of configuration attributes:
343
343
  attribute alternative default
344
344
  ------------------------------------------------------
345
345
  color col "-"
346
- color_value col_val "-"
346
+ color_value col_val ""
347
347
  compact - False
348
348
  context_separator cs " ==> "
349
349
  depth - 1000000
@@ -372,7 +372,8 @@ show_time st False
372
372
  show_traceback - False
373
373
  sort_dicts - False
374
374
  to_clipboard clip False
375
- underscore_numbers un False
375
+ underscore_numbers un False
376
+ use_color - True
376
377
  values_only vo False
377
378
  value_only_for_fstrings voff False
378
379
  wrap_indent - " "
@@ -691,11 +692,6 @@ will result in:
691
692
 
692
693
  Of course, color and color_value may be specified in a peek.toml file, to make all peek output in a specified color.
693
694
 
694
- > [!NOTE]
695
- >
696
- > The color and color_value attributes are only applied when using stdout as output.
697
- >
698
- > Colors can be ignored completely by using `peek.output = "stdout_nocolor"`.
699
695
 
700
696
  ------
701
697
 
@@ -717,8 +713,24 @@ repr(peek.ANSI.red)='\x1b[1;31m'
717
713
 
718
714
  ------
719
715
 
716
+ ## use_color
717
+
718
+ Colors can be ignored completely by using `peek.use_color = False`.
719
+
720
+ So,
721
+
722
+ ```
723
+ peek(hello, color="red")
724
+ peek.use_color = False
725
+ peek(hello, color="red")
726
+ ```
727
+
728
+ will print `hello=world` once in red and once without color.
729
+
730
+ Of course, `use_color` can be specified in a peek.toml file.
720
731
 
721
732
  ## compact
733
+
722
734
  This attribute is used to specify the compact parameter for `pformat` (see the pprint documentation
723
735
  for details). `compact` is False by default.
724
736
 
@@ -1025,7 +1037,9 @@ for i in range(50):
1025
1037
  time.sleep(0.1)
1026
1038
  peek('')
1027
1039
  ```
1028
- The `end` parameter will be only applied when output is stdout, stdout_nocolor or stderr.
1040
+ > [!NOTE]
1041
+ >
1042
+ > The `end` parameter will not be only applied when output is "logging.debug", "logging.info", "logging.warning", "logging.error" or "logging.critical".
1029
1043
 
1030
1044
  > [!NOTE]
1031
1045
  >
@@ -1077,7 +1091,7 @@ You can also use peek.print (see below).
1077
1091
 
1078
1092
  > [!TIP]
1079
1093
  >
1080
- > Of course, print_only can be put in a **peek.toml** file.
1094
+ > Of course, `print_like` can be put in a **peek.toml** file.
1081
1095
 
1082
1096
  # Use peek.print to use peek like print with extras
1083
1097
  The method `peek.print` allows peek to be used as alternative to print. Note that `peek.print` applies the `color`, `context_separator`, `enabled`, `end`, `filter` and `output`, `separator_print`, `show_delta` and `show_time`. It is also possible to redirect the output to a string with `as_str`.
@@ -1173,23 +1187,22 @@ hello='world'
1173
1187
 
1174
1188
  Note that if enabled=False, the call will return the null string (`""`).
1175
1189
 
1176
- It is also possible to return a string with the embedded ANSI color escape strings, which can be useful to process the output in another program that supports ANSI colors, like salabim. This is done by setting the `as_colored_str` argument to True:
1190
+ By default, a string will contain embedded ANSI color escape strings if either `color` or `color_value` specifies a color. By setting `use_color` to False, these escape sequences will be suppressed.
1191
+
1177
1192
  ```
1178
1193
  hello = "world"
1179
- s = peek(hello, color="red", color_value="green", as_colored_str=True)
1180
- print(repr(s), end="")
1194
+ s = peek(hello, color="red", color_value="green", as_str=True)
1195
+ print(repr(s))
1196
+ peek.use_color = False
1197
+ s = peek(hello, color="red", color_value="green", as_str=True)
1198
+ print(repr(s))
1181
1199
  ```
1182
1200
  prints
1183
1201
  ```
1184
- "\x1b[1;31mhello=\x1b[1;32m'world'\x1b[1;31m\n\x1b[0m"
1202
+ '\x1b[1;31mhello=\x1b[1;32mworld\x1b[1;31m\x1b[0m\n'
1203
+ 'hello=world\n'
1185
1204
  ```
1186
1205
 
1187
- > [!NOTE]
1188
- >
1189
- > Specifying both `as_str` and `as_colored_str` is not allowed.
1190
-
1191
-
1192
-
1193
1206
  # Disabling peek's output
1194
1207
 
1195
1208
  ```
@@ -323,7 +323,7 @@ a number of configuration attributes:
323
323
  attribute alternative default
324
324
  ------------------------------------------------------
325
325
  color col "-"
326
- color_value col_val "-"
326
+ color_value col_val ""
327
327
  compact - False
328
328
  context_separator cs " ==> "
329
329
  depth - 1000000
@@ -352,7 +352,8 @@ show_time st False
352
352
  show_traceback - False
353
353
  sort_dicts - False
354
354
  to_clipboard clip False
355
- underscore_numbers un False
355
+ underscore_numbers un False
356
+ use_color - True
356
357
  values_only vo False
357
358
  value_only_for_fstrings voff False
358
359
  wrap_indent - " "
@@ -671,11 +672,6 @@ will result in:
671
672
 
672
673
  Of course, color and color_value may be specified in a peek.toml file, to make all peek output in a specified color.
673
674
 
674
- > [!NOTE]
675
- >
676
- > The color and color_value attributes are only applied when using stdout as output.
677
- >
678
- > Colors can be ignored completely by using `peek.output = "stdout_nocolor"`.
679
675
 
680
676
  ------
681
677
 
@@ -697,8 +693,24 @@ repr(peek.ANSI.red)='\x1b[1;31m'
697
693
 
698
694
  ------
699
695
 
696
+ ## use_color
697
+
698
+ Colors can be ignored completely by using `peek.use_color = False`.
699
+
700
+ So,
701
+
702
+ ```
703
+ peek(hello, color="red")
704
+ peek.use_color = False
705
+ peek(hello, color="red")
706
+ ```
707
+
708
+ will print `hello=world` once in red and once without color.
709
+
710
+ Of course, `use_color` can be specified in a peek.toml file.
700
711
 
701
712
  ## compact
713
+
702
714
  This attribute is used to specify the compact parameter for `pformat` (see the pprint documentation
703
715
  for details). `compact` is False by default.
704
716
 
@@ -1005,7 +1017,9 @@ for i in range(50):
1005
1017
  time.sleep(0.1)
1006
1018
  peek('')
1007
1019
  ```
1008
- The `end` parameter will be only applied when output is stdout, stdout_nocolor or stderr.
1020
+ > [!NOTE]
1021
+ >
1022
+ > The `end` parameter will not be only applied when output is "logging.debug", "logging.info", "logging.warning", "logging.error" or "logging.critical".
1009
1023
 
1010
1024
  > [!NOTE]
1011
1025
  >
@@ -1057,7 +1071,7 @@ You can also use peek.print (see below).
1057
1071
 
1058
1072
  > [!TIP]
1059
1073
  >
1060
- > Of course, print_only can be put in a **peek.toml** file.
1074
+ > Of course, `print_like` can be put in a **peek.toml** file.
1061
1075
 
1062
1076
  # Use peek.print to use peek like print with extras
1063
1077
  The method `peek.print` allows peek to be used as alternative to print. Note that `peek.print` applies the `color`, `context_separator`, `enabled`, `end`, `filter` and `output`, `separator_print`, `show_delta` and `show_time`. It is also possible to redirect the output to a string with `as_str`.
@@ -1153,23 +1167,22 @@ hello='world'
1153
1167
 
1154
1168
  Note that if enabled=False, the call will return the null string (`""`).
1155
1169
 
1156
- It is also possible to return a string with the embedded ANSI color escape strings, which can be useful to process the output in another program that supports ANSI colors, like salabim. This is done by setting the `as_colored_str` argument to True:
1170
+ By default, a string will contain embedded ANSI color escape strings if either `color` or `color_value` specifies a color. By setting `use_color` to False, these escape sequences will be suppressed.
1171
+
1157
1172
  ```
1158
1173
  hello = "world"
1159
- s = peek(hello, color="red", color_value="green", as_colored_str=True)
1160
- print(repr(s), end="")
1174
+ s = peek(hello, color="red", color_value="green", as_str=True)
1175
+ print(repr(s))
1176
+ peek.use_color = False
1177
+ s = peek(hello, color="red", color_value="green", as_str=True)
1178
+ print(repr(s))
1161
1179
  ```
1162
1180
  prints
1163
1181
  ```
1164
- "\x1b[1;31mhello=\x1b[1;32m'world'\x1b[1;31m\n\x1b[0m"
1182
+ '\x1b[1;31mhello=\x1b[1;32mworld\x1b[1;31m\x1b[0m\n'
1183
+ 'hello=world\n'
1165
1184
  ```
1166
1185
 
1167
- > [!NOTE]
1168
- >
1169
- > Specifying both `as_str` and `as_colored_str` is not allowed.
1170
-
1171
-
1172
-
1173
1186
  # Disabling peek's output
1174
1187
 
1175
1188
  ```
@@ -4,7 +4,7 @@
4
4
  # | .__/ \___| \___||_|\_\
5
5
  # |_| like print, but easy.
6
6
 
7
- __version__ = "25.0.6"
7
+ __version__ = "25.0.8"
8
8
 
9
9
  """
10
10
  See https://github.com/salabim/peek for details
@@ -86,6 +86,7 @@ class _Peek:
86
86
  ("sort_dicts", "", False),
87
87
  ("to_clipboard", "clip", False),
88
88
  ("underscore_numbers", "un", False),
89
+ ("use_color", "", True),
89
90
  ("values_only", "vo", False),
90
91
  ("values_only_for_fstrings", "voff", False),
91
92
  ("wrap_indent", "", " "),
@@ -94,7 +95,7 @@ class _Peek:
94
95
  name_alias = {name: alias for (name, alias, default) in name_alias_default}
95
96
  name_default = {name: default for (name, alias, default) in name_alias_default}
96
97
  alias_default = {alias: default for (name, alias, default) in name_alias_default if alias}
97
- name_and_alias_default = {**name_default, **alias_default}
98
+ name_and_alias_default = name_default | alias_default
98
99
 
99
100
  _fixed_perf_counter = None
100
101
 
@@ -140,10 +141,14 @@ class _Peek:
140
141
  "\033[0m": (),
141
142
  }
142
143
 
143
- ANSI=types.SimpleNamespace(**_color_name_to_ANSI)
144
+ ANSI = types.SimpleNamespace(**_color_name_to_ANSI)
144
145
 
145
146
  codes = {}
146
147
 
148
+ # @staticmethod
149
+ # def reset(): # for Pythonista problems. Solved?
150
+ # _Peek.codes={}
151
+
147
152
  @staticmethod
148
153
  def de_alias(name):
149
154
  return _Peek.alias_name.get(name, name)
@@ -153,7 +158,7 @@ class _Peek:
153
158
  name_org = name
154
159
  name = _Peek.de_alias(name)
155
160
  if name not in _Peek.name_default:
156
- raise AttributeError(f"attribute {name} not allowed{_Peek.in_read_toml_message}")
161
+ raise AttributeError(f"attribute {name_org} not allowed{_Peek.in_read_toml_message}")
157
162
 
158
163
  if value is None:
159
164
  return
@@ -169,17 +174,17 @@ class _Peek:
169
174
  pass
170
175
  raise AttributeError("output should be a callable, str, Path or open text file.")
171
176
 
172
- if name == "serialize":
177
+ elif name == "serialize":
173
178
  if callable(value):
174
179
  return
175
180
 
176
- if name in ("color", "color_value"):
177
- if isinstance(value, str) and value.lower() in _Peek._color_name_to_ANSI:
181
+ elif name in ("color", "color_value"):
182
+ if isinstance(value, str) and value in _Peek._color_name_to_ANSI:
178
183
  return
179
184
 
180
- elif name == "delta":
181
- if isinstance(value, numbers.Number):
182
- return
185
+ elif name == "delta":
186
+ if isinstance(value, numbers.Number):
187
+ return
183
188
 
184
189
  elif name == "line_length":
185
190
  if isinstance(value, numbers.Number) and value > 0:
@@ -249,7 +254,7 @@ class _Peek:
249
254
  return {}
250
255
 
251
256
  @staticmethod
252
- def print_pythonista_color(s, end="\n"):
257
+ def print_pythonista_color(s, end="\n", file=sys.stdout):
253
258
  while s:
254
259
  for ansi, rgb in _Peek._ANSI_to_rgb.items():
255
260
  if s.startswith(ansi):
@@ -257,9 +262,9 @@ class _Peek:
257
262
  s = s[len(ansi) :]
258
263
  break
259
264
  else:
260
- print(s[0], end="")
265
+ print(s[0], end="", file=file)
261
266
  s = s[1:]
262
- print("", end=end)
267
+ print("", end=end, file=file)
263
268
 
264
269
  @staticmethod
265
270
  def return_args(args, return_none):
@@ -290,7 +295,7 @@ class _Peek:
290
295
 
291
296
  def clone(self, **kwargs):
292
297
  clone = _Peek(parent=self._parent)
293
- clone._attributes = {**self._attributes, **_Peek.spec_to_attributes(**kwargs)}
298
+ clone._attributes = self._attributes | _Peek.spec_to_attributes(**kwargs)
294
299
  return clone
295
300
 
296
301
  def configure(self, **kwargs):
@@ -315,16 +320,7 @@ class _Peek:
315
320
  return self.__getattribute__(item)
316
321
 
317
322
  def __setattr__(self, item, value):
318
- if item in (
319
- "_parent",
320
- "_is_context_manager",
321
- "_line_number_with_filename_and_parent",
322
- "_save_traceback",
323
- "_enter_time",
324
- "_as_str",
325
- "_as_colored_str",
326
- "_attributes",
327
- ):
323
+ if item in ("_parent", "_is_context_manager", "_line_number_with_filename_and_parent", "_save_traceback", "_enter_time", "_as_str", "_attributes"):
328
324
  return super().__setattr__(item, value)
329
325
  self._attributes.update(_Peek.spec_to_attributes(**{item: value}))
330
326
 
@@ -338,6 +334,7 @@ class _Peek:
338
334
  pairs = [
339
335
  str(name) + "=" + repr(getattr(self, "delta1") if name == "delta" else getattr(self, name)) for name in _Peek.name_default if name != "serialize"
340
336
  ]
337
+
341
338
  return "peek with attributes:\n " + "\n ".join(pairs) + ")"
342
339
 
343
340
  def fix_perf_counter(self, val): # for tests
@@ -349,14 +346,14 @@ class _Peek:
349
346
  return False
350
347
  return self.enabled
351
348
 
352
- def print(self, *args, as_str=False, as_colored_str=False, **kwargs):
349
+ def print(self, *args, as_str=False, **kwargs):
353
350
  if "print" in kwargs and "print_like" in kwargs:
354
351
  raise AttributeError("both print_like and print specified")
355
352
  if not "print" in kwargs and not "print_like" in kwargs:
356
353
  kwargs["print_like"] = True
357
- return self(*args, as_str=as_str, as_colored_str=as_colored_str, **kwargs)
354
+ return self(*args, as_str=as_str, **kwargs)
358
355
 
359
- def __call__(self, *args, as_str=False, as_colored_str=False, _via_module=False, **kwargs):
356
+ def __call__(self, *args, as_str=False, _via_module=False, **kwargs):
360
357
  def add_to_pairs(pairs, left, right):
361
358
  if right in (locals, globals, vars):
362
359
  frame = inspect.currentframe().f_back.f_back
@@ -371,11 +368,7 @@ class _Peek:
371
368
  any_args = bool(args)
372
369
  this = self.fork(**kwargs)
373
370
 
374
- if as_str and as_colored_str:
375
- raise ValueError("not allowed to use both as_str and as_colored_str")
376
-
377
371
  this._as_str = as_str
378
- this._as_colored_str = as_colored_str
379
372
 
380
373
  if this.print_like:
381
374
  seps = [name for name in ("sep", "separator") if name in kwargs]
@@ -394,7 +387,8 @@ class _Peek:
394
387
  args = [this.separator_print.join(map(str, args))]
395
388
 
396
389
  if len(args) != 0 and not this.do_show():
397
- if as_str or as_colored_str:
390
+ # if there are no args, the checks for decorator and context manager always to be done
391
+ if as_str:
398
392
  return ""
399
393
  else:
400
394
  return _Peek.return_args(args, this.return_none)
@@ -447,24 +441,23 @@ class _Peek:
447
441
  else:
448
442
  line_number = frame_info.lineno
449
443
 
450
- # parent_function = frame_info.function
451
- parent_function = executing.Source.executing(call_frame).code_qualname() # changed in version 1.3.10 to include class name
444
+ parent_function = executing.Source.executing(call_frame).code_qualname()
452
445
  parent_function = parent_function.replace(".<locals>.", ".")
453
446
  if parent_function == "<module>" or str(this.show_line_number) in ("n", "no parent"):
454
447
  parent_function = ""
455
448
  else:
456
449
  parent_function = f" in {parent_function}()"
457
- if 0 <= line_number - 1 < len(code):
450
+ if 0 <= line_number - 1 < len(code) - 1:
458
451
  this_line = code[line_number - 1].strip()
459
452
  else:
460
453
  this_line = ""
461
- if 0 <= line_number - 2 < len(code):
454
+ if 0 <= line_number - 2 < len(code) - 2:
462
455
  this_line_prev = code[line_number - 2].strip()
463
456
  else:
464
457
  this_line_prev = ""
465
458
  if this_line.startswith("@") or this_line_prev.startswith("@"):
466
- if as_str or as_colored_str:
467
- raise TypeError("as_str or as_colored_str may not be True when peek used as decorator")
459
+ if as_str:
460
+ raise TypeError("as_str may not be True when peek used as decorator")
468
461
  if any_args:
469
462
  raise TypeError("non-keyword arguments are not allowed when peek used as decorator")
470
463
 
@@ -513,8 +506,8 @@ class _Peek:
513
506
  this._line_number_with_filename_and_parent = f"#{line_number}{filename_name}{parent_function}"
514
507
 
515
508
  if this_line.startswith("with ") or this_line.startswith("with\t"):
516
- if as_str or as_colored_str:
517
- raise TypeError("as_str or as_colored_str may not be True when peek used as context manager")
509
+ if as_str:
510
+ raise TypeError("as_str may not be True when peek used as context manager")
518
511
  if any_args:
519
512
  raise TypeError("non-keyword arguments are not allowed when peek used as context manager")
520
513
 
@@ -522,7 +515,7 @@ class _Peek:
522
515
  return this
523
516
 
524
517
  if not this.do_show():
525
- if as_str or as_colored_str:
518
+ if as_str:
526
519
  return ""
527
520
  else:
528
521
  return _Peek.return_args(args, this.return_none)
@@ -560,12 +553,17 @@ class _Peek:
560
553
  add_to_pairs(pairs, left, right)
561
554
 
562
555
  just_one_line = False
556
+ if (not this.use_color) or this.color in ("", "-"):
557
+ out = ""
558
+ else:
559
+ out = _Peek._color_name_to_ANSI[this.color.lower()]
560
+
563
561
  if not (len(pairs) > 1 and this.separator == ""):
564
562
  if not any("\n" in pair.left for pair in pairs):
565
563
  as_one_line = context + this.separator.join(pair.left + this.serialize_kwargs(obj=pair.right, width=10000) for pair in pairs)
566
564
  # as_one_line = context + this.separator.join(pair.left + (this.serialize_kwargs(obj=pair.right, width=10000)) for pair in pairs)
567
565
  if len(as_one_line) <= this.line_length and "\n" not in as_one_line:
568
- out = as_one_line
566
+ out += as_one_line
569
567
  just_one_line = True
570
568
 
571
569
  if not just_one_line:
@@ -609,7 +607,7 @@ class _Peek:
609
607
  for s in line.splitlines():
610
608
  lines.append(indent2 + s)
611
609
 
612
- out = "\n".join(line.rstrip() for line in lines)
610
+ out += "\n".join(line.rstrip() for line in lines)
613
611
 
614
612
  else:
615
613
  if not this.show_line_number: # if "n" or "no parent", keep that info
@@ -619,18 +617,15 @@ class _Peek:
619
617
  if this.show_traceback:
620
618
  out += this.traceback()
621
619
 
620
+ if this.use_color and this.color not in ("", "-"):
621
+ out += _Peek._color_name_to_ANSI["-"]
622
+
622
623
  if as_str:
623
624
  if this.do_show():
624
625
  return out + this.end
625
626
  else:
626
627
  return ""
627
628
 
628
- if as_colored_str:
629
- if this.do_show():
630
- return this._color_name_to_ANSI[this.color.lower()] + out + this.end + this._color_name_to_ANSI["-"]
631
- else:
632
- return ""
633
-
634
629
  if this.to_clipboard:
635
630
  peek.copy_to_clipboard(pairs[-1].right if "pairs" in locals() else "", confirm=False)
636
631
  this.do_output(out)
@@ -678,12 +673,17 @@ class _Peek:
678
673
  return self.prefix + context
679
674
 
680
675
  def add_color_value(self, s):
681
- if (self.output != "stdout" or self._as_str) and not self._as_colored_str:
676
+ if not self.use_color:
682
677
  return s
683
- if self.color_value.lower() not in (self.color.lower(), ""):
684
- return self._color_name_to_ANSI[self.color_value.lower()] + s + self._color_name_to_ANSI[self.color.lower()]
678
+ if self.color_value == "":
679
+ ...
680
+ elif self.color_value == "-":
681
+ if self.color not in ("", "-"):
682
+ s = _Peek._color_name_to_ANSI["-"] + s + _Peek._color_name_to_ANSI[self.color]
685
683
  else:
686
- return s
684
+ if self.color_value != self.color:
685
+ s = _Peek._color_name_to_ANSI[self.color_value] + s + _Peek._color_name_to_ANSI[self.color]
686
+ return s
687
687
 
688
688
  def do_output(self, s):
689
689
  if self.do_show():
@@ -693,19 +693,12 @@ class _Peek:
693
693
  self.output(s, end=self.end)
694
694
  else:
695
695
  self.output(s + self.end)
696
- elif self.output == "stderr":
697
- print(s, file=sys.stderr, end=self.end)
698
- elif self.output == "stdout":
699
- if self.color not in ["", "-"]:
700
- s = self._color_name_to_ANSI[self.color.lower()] + s + self.end + self._color_name_to_ANSI["-"]
701
- if Pythonista:
702
- _Peek.print_pythonista_color(s, end="")
703
- else:
704
- print(s, end="")
696
+ elif self.output in ("stdout", "stderr"):
697
+ file = sys.stdout if self.output == "stdout" else sys.stderr
698
+ if Pythonista:
699
+ _Peek.print_pythonista_color(s, end=self.end, file=file)
705
700
  else:
706
- print(s, end=self.end)
707
- elif self.output == "stdout_nocolor":
708
- print(s, end=self.end)
701
+ print(s, end=self.end, file=file)
709
702
  elif self.output == "logging.debug":
710
703
  logging.debug(s)
711
704
  elif self.output == "logging.info":
@@ -779,7 +772,7 @@ class _Peek:
779
772
 
780
773
  _Peek.in_read_toml_message = ""
781
774
  _peek_no_toml = _Peek(**_Peek.name_default)
782
- _peek_toml = _Peek(**{**_Peek.name_default, **_Peek.read_toml()})
775
+ _peek_toml = _Peek(**(_Peek.name_default | _Peek.read_toml()))
783
776
  _Peek.in_read_toml_message = ""
784
777
  peek = _peek_toml.new()
785
778
  builtins.peek = peek
@@ -804,4 +797,3 @@ class _PeekModule(types.ModuleType):
804
797
 
805
798
  if __name__ != "__main__":
806
799
  sys.modules["peek"].__class__ = _PeekModule
807
-
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: peek-python
3
- Version: 25.0.6
3
+ Version: 25.0.8
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/peek
@@ -343,7 +343,7 @@ a number of configuration attributes:
343
343
  attribute alternative default
344
344
  ------------------------------------------------------
345
345
  color col "-"
346
- color_value col_val "-"
346
+ color_value col_val ""
347
347
  compact - False
348
348
  context_separator cs " ==> "
349
349
  depth - 1000000
@@ -372,7 +372,8 @@ show_time st False
372
372
  show_traceback - False
373
373
  sort_dicts - False
374
374
  to_clipboard clip False
375
- underscore_numbers un False
375
+ underscore_numbers un False
376
+ use_color - True
376
377
  values_only vo False
377
378
  value_only_for_fstrings voff False
378
379
  wrap_indent - " "
@@ -691,11 +692,6 @@ will result in:
691
692
 
692
693
  Of course, color and color_value may be specified in a peek.toml file, to make all peek output in a specified color.
693
694
 
694
- > [!NOTE]
695
- >
696
- > The color and color_value attributes are only applied when using stdout as output.
697
- >
698
- > Colors can be ignored completely by using `peek.output = "stdout_nocolor"`.
699
695
 
700
696
  ------
701
697
 
@@ -717,8 +713,24 @@ repr(peek.ANSI.red)='\x1b[1;31m'
717
713
 
718
714
  ------
719
715
 
716
+ ## use_color
717
+
718
+ Colors can be ignored completely by using `peek.use_color = False`.
719
+
720
+ So,
721
+
722
+ ```
723
+ peek(hello, color="red")
724
+ peek.use_color = False
725
+ peek(hello, color="red")
726
+ ```
727
+
728
+ will print `hello=world` once in red and once without color.
729
+
730
+ Of course, `use_color` can be specified in a peek.toml file.
720
731
 
721
732
  ## compact
733
+
722
734
  This attribute is used to specify the compact parameter for `pformat` (see the pprint documentation
723
735
  for details). `compact` is False by default.
724
736
 
@@ -1025,7 +1037,9 @@ for i in range(50):
1025
1037
  time.sleep(0.1)
1026
1038
  peek('')
1027
1039
  ```
1028
- The `end` parameter will be only applied when output is stdout, stdout_nocolor or stderr.
1040
+ > [!NOTE]
1041
+ >
1042
+ > The `end` parameter will not be only applied when output is "logging.debug", "logging.info", "logging.warning", "logging.error" or "logging.critical".
1029
1043
 
1030
1044
  > [!NOTE]
1031
1045
  >
@@ -1077,7 +1091,7 @@ You can also use peek.print (see below).
1077
1091
 
1078
1092
  > [!TIP]
1079
1093
  >
1080
- > Of course, print_only can be put in a **peek.toml** file.
1094
+ > Of course, `print_like` can be put in a **peek.toml** file.
1081
1095
 
1082
1096
  # Use peek.print to use peek like print with extras
1083
1097
  The method `peek.print` allows peek to be used as alternative to print. Note that `peek.print` applies the `color`, `context_separator`, `enabled`, `end`, `filter` and `output`, `separator_print`, `show_delta` and `show_time`. It is also possible to redirect the output to a string with `as_str`.
@@ -1173,23 +1187,22 @@ hello='world'
1173
1187
 
1174
1188
  Note that if enabled=False, the call will return the null string (`""`).
1175
1189
 
1176
- It is also possible to return a string with the embedded ANSI color escape strings, which can be useful to process the output in another program that supports ANSI colors, like salabim. This is done by setting the `as_colored_str` argument to True:
1190
+ By default, a string will contain embedded ANSI color escape strings if either `color` or `color_value` specifies a color. By setting `use_color` to False, these escape sequences will be suppressed.
1191
+
1177
1192
  ```
1178
1193
  hello = "world"
1179
- s = peek(hello, color="red", color_value="green", as_colored_str=True)
1180
- print(repr(s), end="")
1194
+ s = peek(hello, color="red", color_value="green", as_str=True)
1195
+ print(repr(s))
1196
+ peek.use_color = False
1197
+ s = peek(hello, color="red", color_value="green", as_str=True)
1198
+ print(repr(s))
1181
1199
  ```
1182
1200
  prints
1183
1201
  ```
1184
- "\x1b[1;31mhello=\x1b[1;32m'world'\x1b[1;31m\n\x1b[0m"
1202
+ '\x1b[1;31mhello=\x1b[1;32mworld\x1b[1;31m\x1b[0m\n'
1203
+ 'hello=world\n'
1185
1204
  ```
1186
1205
 
1187
- > [!NOTE]
1188
- >
1189
- > Specifying both `as_str` and `as_colored_str` is not allowed.
1190
-
1191
-
1192
-
1193
1206
  # Disabling peek's output
1194
1207
 
1195
1208
  ```
@@ -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 - debugging and benchmarking made easy"
13
- version = "25.0.6"
13
+ version = "25.0.8"
14
14
  readme = "README.md"
15
15
  requires-python = ">=3.7"
16
16
  dependencies = [
@@ -15,7 +15,9 @@ sys.path.insert(0, file_folder + "/../peek")
15
15
 
16
16
  import peek
17
17
 
18
- peek = peek.new(ignore_toml=True, output="stdout_nocolor")
18
+ peek = peek.new(ignore_toml=True)
19
+
20
+ globals().update(peek.ANSI.__dict__) # put white, red, ..., dark_yellow in globals
19
21
 
20
22
 
21
23
  class g:
@@ -121,7 +123,6 @@ def test_time_delta():
121
123
  time.sleep(0.1)
122
124
  assert 10.05 < peek.delta < 11
123
125
 
124
-
125
126
 
126
127
  def test_dynamic_prefix(capsys):
127
128
  g.i = 0
@@ -144,24 +145,26 @@ def test_values_only():
144
145
  s = peek(hello, as_str=True)
145
146
  assert s == "'world'\n"
146
147
 
148
+
147
149
  def test_locals(capsys):
148
150
  def square(x):
149
- result = x ** 2
151
+ result = x**2
150
152
  peek(locals)
151
-
153
+
152
154
  square(10)
153
155
  out, err = capsys.readouterr()
154
- assert out=='x=10, result=100\n'
155
-
156
+ assert out == "x=10, result=100\n"
157
+
156
158
 
157
159
  def test_globals(capsys):
158
160
  def square(x):
159
- result = x ** 2
161
+ result = x**2
160
162
  peek(globals)
161
-
163
+
162
164
  square(10)
163
165
  out, err = capsys.readouterr()
164
- assert 'pytest=<module' in out
166
+ assert "pytest=<module" in out
167
+
165
168
 
166
169
  def test_calls():
167
170
  with pytest.raises(AttributeError):
@@ -187,6 +190,7 @@ def test_repr_and_str(capsys):
187
190
  assert out.startswith("peek.new(")
188
191
  assert out.endswith(")\n")
189
192
 
193
+
190
194
  def test_output(capsys, tmpdir):
191
195
  g.result = ""
192
196
 
@@ -202,10 +206,6 @@ def test_output(capsys, tmpdir):
202
206
  out, err = capsys.readouterr()
203
207
  assert out == "hello='world'\n"
204
208
 
205
- peek(hello, output="stdout_nocolor")
206
- out, err = capsys.readouterr()
207
- assert out == "hello='world'\n"
208
-
209
209
  peek(hello, output="")
210
210
  out, err = capsys.readouterr()
211
211
  assert out == ""
@@ -282,12 +282,10 @@ def test_show_delta(capsys):
282
282
  assert "delta=" in out
283
283
 
284
284
 
285
- def test_as_str(capsys):
285
+ def test_as_str():
286
286
  hello = "world"
287
- s = peek(hello, as_str=True)
288
- peek(hello)
289
- out, err = capsys.readouterr()
290
- assert out == s
287
+ s = peek(hello, as_str=True, color="red")
288
+ assert s == f"{red}hello='world'{reset}\n"
291
289
 
292
290
  with pytest.raises(TypeError):
293
291
 
@@ -299,14 +297,12 @@ def test_as_str(capsys):
299
297
  with peek(as_str=True):
300
298
  pass
301
299
 
302
- def test_as_colored_str():
303
- hello = "world"
304
- s = peek(hello, as_colored_str=True)
305
- assert s == "\x1b[0mhello='world'\n\x1b[0m"
306
-
307
- with pytest.raises(ValueError):
308
- s=peek(hello, as_str=True, as_colored_str=True)
309
-
300
+ with peek.preserve():
301
+ peek.use_color = False
302
+ s = peek(hello, as_str=True, color="red")
303
+ assert s == "hello='world'\n"
304
+
305
+
310
306
  @pytest.mark.skipif(Pythonista, reason="Pythonista problem")
311
307
  def test_print(capsys):
312
308
  peek.print(*range(4))
@@ -327,16 +323,16 @@ def test_print(capsys):
327
323
  2
328
324
  """
329
325
  )
330
- result=peek.print(1,2,as_str=True)
331
- assert result=="1 2\n"
326
+ result = peek.print(1, 2, as_str=True)
327
+ assert result == "1 2\n"
332
328
 
333
- result=peek.print(1,2)
329
+ result = peek.print(1, 2)
334
330
  assert result is None
335
331
  out, err = capsys.readouterr()
336
332
  assert out == "1 2\n"
337
333
 
338
334
  with pytest.raises(AttributeError):
339
- peek.print(sep="|", sepp="/")
335
+ peek.print(sep="|", sepp="/")
340
336
 
341
337
 
342
338
  def test_clone():
@@ -411,6 +407,7 @@ lines=
411
407
  )
412
408
 
413
409
 
410
+ @pytest.mark.skipif(Pythonista, reason="Pythonista does not use escape sequences for color")
414
411
  def test_filter(capsys):
415
412
  def gen():
416
413
  for level, color in enumerate("- blue red green blue".split()):
@@ -430,34 +427,61 @@ def test_filter(capsys):
430
427
  out, err = capsys.readouterr()
431
428
  assert (
432
429
  out
433
- == """\
430
+ == f"""\
434
431
  level=0
435
- level=1
436
- level=2
437
- level=3
438
- level=4
439
- level=2
440
- level=3
441
- level=4
442
- level=2
443
- level=3
444
- level=4
445
- level=4
432
+ {blue}level=1{reset}
433
+ {red}level=2{reset}
434
+ {green}level=3{reset}
435
+ {blue}level=4{reset}
436
+ {red}level=2{reset}
437
+ {green}level=3{reset}
438
+ {blue}level=4{reset}
439
+ {red}level=2{reset}
440
+ {green}level=3{reset}
441
+ {blue}level=4{reset}
442
+ {blue}level=4{reset}
446
443
  level=0
447
- level=1
448
- level=2
449
- level=3
450
- level=4
444
+ {blue}level=1{reset}
445
+ {red}level=2{reset}
446
+ {green}level=3{reset}
447
+ {blue}level=4{reset}
451
448
  """
452
449
  )
453
450
 
454
451
 
455
- @pytest.mark.skipif(Pythonista, reason="Pythonista does not generate ansi codes")
456
452
  def test_color(capsys):
457
- peek(10 * 10, output="stdout")
458
- peek(10 * 10, color="red", output="stdout")
459
- out, err = capsys.readouterr()
460
- assert out == "10 * 10=100\n\x1b[1;31m10 * 10=100\n\x1b[0m"
453
+ with peek.preserve():
454
+ hello = "world"
455
+ s = peek(hello, as_str=True)
456
+ assert s == f"hello='world'\n"
457
+ s = peek(hello, as_str=True, color="")
458
+ assert s == f"hello='world'\n"
459
+ s = peek(hello, as_str=True, color="-")
460
+ assert s == f"hello='world'\n"
461
+ s = peek(hello, as_str=True, color="red")
462
+ assert s == f"{red}hello='world'{reset}\n"
463
+
464
+ peek.color_value = "-"
465
+ s = peek(hello, as_str=True)
466
+ assert s == f"hello='world'\n"
467
+ s = peek(hello, as_str=True, color="")
468
+ assert s == f"hello='world'\n"
469
+ s = peek(hello, as_str=True, color="-")
470
+ assert s == f"hello='world'\n"
471
+ s = peek(hello, as_str=True, color="red")
472
+ assert s == f"{red}hello={reset}'world'{red}{reset}\n"
473
+
474
+ peek.color_value = "blue"
475
+ s = peek(hello, as_str=True)
476
+ assert s == f"hello={blue}'world'{reset}\n"
477
+ s = peek(hello, as_str=True, color="")
478
+ assert s == f"hello={blue}'world'{reset}\n"
479
+ s = peek(hello, as_str=True, color="-")
480
+ assert s == f"hello={blue}'world'{reset}\n"
481
+ s = peek(hello, as_str=True, color="red")
482
+ assert s == f"{red}hello={blue}'world'{red}{reset}\n"
483
+ s = peek(hello, as_str=True, color="blue")
484
+ assert s == f"{blue}hello='world'{reset}\n"
461
485
 
462
486
 
463
487
  def test_incorrect_filter():
@@ -860,7 +884,7 @@ def test_check_output(capsys, tmpdir):
860
884
  def check_output():
861
885
  import x2
862
886
 
863
- peek.configure(show_line_number=True, show_exit= False,output="stdout_nocolor")
887
+ peek.configure(show_line_number=True, show_exit= False,use_color=False)
864
888
  x2.test()
865
889
  peek(1)
866
890
  peek(
@@ -1164,3 +1188,4 @@ hello='world'
1164
1188
 
1165
1189
  if __name__ == "__main__":
1166
1190
  pytest.main(["-vv", "-s", "-x", __file__])
1191
+
File without changes
File without changes