peek-python 25.0.0__tar.gz → 25.0.2__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
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: peek-python
3
- Version: 25.0.0
3
+ Version: 25.0.2
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
@@ -46,15 +46,15 @@ And on top of that, you get some basic benchmarking functionality.
46
46
 
47
47
  * [Use peek.print to use peek like print with extras](#use-peekprint-to-use-peek-like-print-with-extras)
48
48
 
49
+ * [Peeking locals and globals](#peeking-locals-and-globals)
50
+
49
51
  * [Return a string instead of sending to output](#return-a-string-instead-of-sending-to-output)
50
52
 
51
53
  * [Disabling peek's output](#disabling-peeks-output)
52
54
 
53
- * [Using level to control peek output](#Using-level-to-control-peek-output)
54
-
55
- * [Using color to control peek output](#Using-color-to-control-peek-output)
55
+ * [Using filter to control peek output](#using-filter-to-control-peek-output)
56
56
 
57
- * [Copying to the clipboard](#Copying-to-the-clipboard)
57
+ * [Copying to the clipboard](#copying-to-the-clipboard)
58
58
 
59
59
  * [Interpreting the line number information](#interpreting-the-line-number-information)
60
60
 
@@ -346,6 +346,7 @@ enabled - True
346
346
  end - "\n"
347
347
  equals_separator - "="
348
348
  filter f ""
349
+ format fmt ""
349
350
  indent - 1
350
351
  level lvl 0
351
352
  line_length ll 80
@@ -905,7 +906,42 @@ This will print:
905
906
  >
906
907
  > This setting does not influence how strings are displayed within other data structures, like dicts and lists.
907
908
 
909
+ ## format / fmt
910
+ With the format attribute, it is possible to apply a format specifier to each of the values to be printed, like
911
+ ```
912
+ test_float = 1.3
913
+ peek(test_float, format="06.3f")
914
+ ```
915
+ This will print
916
+ ```
917
+ test_float=01.300
918
+ ```
919
+
920
+ The format should be like the Python format specifiers, with or without the `:` prefix, like `"6.3f"`, `">10"`, `"06d"`, `:6.3d`.
921
+ It is also possible to use the `!` format specifier: `"!r"`, `"!r:>10"`.
922
+
923
+ If format is the null string (`""`) (the default), this functionality is skipped completely.
924
+
925
+ It is also possible to use a list (or tuple) of format specifiers, which are tried in succession. If they all fail, the 'normal' serializer will be used.
926
+
927
+ ```
928
+ test_float = 1.3
929
+ test_integer=10
930
+ test_string = "test"
931
+ test_dict=dict(one=1, two=2)
932
+ peek(test_float, test_integer, test_string, test_dict, format=["04d", "06.3f", ">10"])
933
+ ```
934
+
935
+ will result in
936
+
937
+ ```
938
+ test_float=01.300, test_integer=0010, test_string= test, test_dict={'one': 1, 'two': 2}
939
+ ```
940
+
941
+ Of course, format may be put in a peek.toml file.
942
+
908
943
  ## values_only / vo
944
+
909
945
  If False (the default), both the left-hand side (if possible) and the
910
946
  value will be printed. If True, the left hand side will be suppressed:
911
947
 
@@ -1062,6 +1098,35 @@ peek.sep = "|" # sets the 'normal' peek separator
1062
1098
  >
1063
1099
  > `peek.print` does not obey the line length and will always return None (unless as_str is True).
1064
1100
 
1101
+
1102
+ # Peeking locals and globals
1103
+ It is possible to get the name and values of all local or global variables.
1104
+
1105
+ To do that, just put `locals` or `globals` in the call to peek, e.g.:
1106
+
1107
+ ```
1108
+ def my_func():
1109
+ a = 10
1110
+ b = a * a
1111
+ peek(locals)
1112
+ my_func()
1113
+ ```
1114
+
1115
+ will print all local variables, apart from those starting with `__`, so:
1116
+ ```
1117
+ a=10, b=100
1118
+ ```
1119
+
1120
+ Likewise,
1121
+ ```
1122
+ peek(globals)
1123
+ ```
1124
+ will print all global variables, apart from those starting with `__`
1125
+
1126
+ > [!IMPORTANT]
1127
+ >
1128
+ > You should not add parentheses after `locals` or `globals` for peek to work properly!
1129
+
1065
1130
  # Return a string instead of sending to output
1066
1131
 
1067
1132
  `peek(*args, as_str=True)` is like `peek(*args)` but the output is returned as a string instead
@@ -1387,6 +1452,7 @@ can show traceback yes no
1387
1452
  can be used like print w/extras yes (with peek.print) no
1388
1453
  allows non linefeed printing yes (via end parameter) requires patching
1389
1454
  PEP8 (Pythonic) API yes no
1455
+ format specification optional no
1390
1456
  sorts dicts no by default, optional *) yes
1391
1457
  supports compact, indent,
1392
1458
  and underscore_numbers
@@ -26,15 +26,15 @@ And on top of that, you get some basic benchmarking functionality.
26
26
 
27
27
  * [Use peek.print to use peek like print with extras](#use-peekprint-to-use-peek-like-print-with-extras)
28
28
 
29
+ * [Peeking locals and globals](#peeking-locals-and-globals)
30
+
29
31
  * [Return a string instead of sending to output](#return-a-string-instead-of-sending-to-output)
30
32
 
31
33
  * [Disabling peek's output](#disabling-peeks-output)
32
34
 
33
- * [Using level to control peek output](#Using-level-to-control-peek-output)
34
-
35
- * [Using color to control peek output](#Using-color-to-control-peek-output)
35
+ * [Using filter to control peek output](#using-filter-to-control-peek-output)
36
36
 
37
- * [Copying to the clipboard](#Copying-to-the-clipboard)
37
+ * [Copying to the clipboard](#copying-to-the-clipboard)
38
38
 
39
39
  * [Interpreting the line number information](#interpreting-the-line-number-information)
40
40
 
@@ -326,6 +326,7 @@ enabled - True
326
326
  end - "\n"
327
327
  equals_separator - "="
328
328
  filter f ""
329
+ format fmt ""
329
330
  indent - 1
330
331
  level lvl 0
331
332
  line_length ll 80
@@ -885,7 +886,42 @@ This will print:
885
886
  >
886
887
  > This setting does not influence how strings are displayed within other data structures, like dicts and lists.
887
888
 
889
+ ## format / fmt
890
+ With the format attribute, it is possible to apply a format specifier to each of the values to be printed, like
891
+ ```
892
+ test_float = 1.3
893
+ peek(test_float, format="06.3f")
894
+ ```
895
+ This will print
896
+ ```
897
+ test_float=01.300
898
+ ```
899
+
900
+ The format should be like the Python format specifiers, with or without the `:` prefix, like `"6.3f"`, `">10"`, `"06d"`, `:6.3d`.
901
+ It is also possible to use the `!` format specifier: `"!r"`, `"!r:>10"`.
902
+
903
+ If format is the null string (`""`) (the default), this functionality is skipped completely.
904
+
905
+ It is also possible to use a list (or tuple) of format specifiers, which are tried in succession. If they all fail, the 'normal' serializer will be used.
906
+
907
+ ```
908
+ test_float = 1.3
909
+ test_integer=10
910
+ test_string = "test"
911
+ test_dict=dict(one=1, two=2)
912
+ peek(test_float, test_integer, test_string, test_dict, format=["04d", "06.3f", ">10"])
913
+ ```
914
+
915
+ will result in
916
+
917
+ ```
918
+ test_float=01.300, test_integer=0010, test_string= test, test_dict={'one': 1, 'two': 2}
919
+ ```
920
+
921
+ Of course, format may be put in a peek.toml file.
922
+
888
923
  ## values_only / vo
924
+
889
925
  If False (the default), both the left-hand side (if possible) and the
890
926
  value will be printed. If True, the left hand side will be suppressed:
891
927
 
@@ -1042,6 +1078,35 @@ peek.sep = "|" # sets the 'normal' peek separator
1042
1078
  >
1043
1079
  > `peek.print` does not obey the line length and will always return None (unless as_str is True).
1044
1080
 
1081
+
1082
+ # Peeking locals and globals
1083
+ It is possible to get the name and values of all local or global variables.
1084
+
1085
+ To do that, just put `locals` or `globals` in the call to peek, e.g.:
1086
+
1087
+ ```
1088
+ def my_func():
1089
+ a = 10
1090
+ b = a * a
1091
+ peek(locals)
1092
+ my_func()
1093
+ ```
1094
+
1095
+ will print all local variables, apart from those starting with `__`, so:
1096
+ ```
1097
+ a=10, b=100
1098
+ ```
1099
+
1100
+ Likewise,
1101
+ ```
1102
+ peek(globals)
1103
+ ```
1104
+ will print all global variables, apart from those starting with `__`
1105
+
1106
+ > [!IMPORTANT]
1107
+ >
1108
+ > You should not add parentheses after `locals` or `globals` for peek to work properly!
1109
+
1045
1110
  # Return a string instead of sending to output
1046
1111
 
1047
1112
  `peek(*args, as_str=True)` is like `peek(*args)` but the output is returned as a string instead
@@ -1367,6 +1432,7 @@ can show traceback yes no
1367
1432
  can be used like print w/extras yes (with peek.print) no
1368
1433
  allows non linefeed printing yes (via end parameter) requires patching
1369
1434
  PEP8 (Pythonic) API yes no
1435
+ format specification optional no
1370
1436
  sorts dicts no by default, optional *) yes
1371
1437
  supports compact, indent,
1372
1438
  and underscore_numbers
@@ -4,7 +4,7 @@
4
4
  # | .__/ \___| \___||_|\_\
5
5
  # |_| like print, but easy.
6
6
 
7
- __version__ = "25.0.0"
7
+ __version__ = "25.0.2"
8
8
 
9
9
  """
10
10
  See https://github.com/salabim/peek for details
@@ -65,6 +65,7 @@ class _Peek:
65
65
  ("end", "", "\n"),
66
66
  ("equals_separator", "", "="),
67
67
  ("filter", "f", ""),
68
+ ("format", "fmt", ""),
68
69
  ("indent", "", 1),
69
70
  ("level", "lvl", 0),
70
71
  ("line_length", "ll", 80),
@@ -110,6 +111,9 @@ class _Peek:
110
111
  colors["-"] = "\033[0m"
111
112
  colors[""] = "\033[0m"
112
113
 
114
+ LOCALS=object()
115
+ GLOBALS=object()
116
+
113
117
  codes = {}
114
118
 
115
119
  ansi_to_rgb = {
@@ -180,6 +184,15 @@ class _Peek:
180
184
  if value > 0:
181
185
  return
182
186
 
187
+ elif name == "format":
188
+ if isinstance(value, str):
189
+ return
190
+ try:
191
+ if all(isinstance(sub_format, str) for sub_format in value):
192
+ return
193
+ except TypeError:
194
+ ...
195
+
183
196
  elif name == "filter":
184
197
  if value.strip() == "":
185
198
  return
@@ -513,7 +526,13 @@ class _Peek:
513
526
  pass
514
527
  if left:
515
528
  left += this.equals_separator
516
- pairs.append(Pair(left=left, right=right))
529
+ if right in (locals, globals,vars):
530
+ frame = inspect.currentframe().f_back.f_back
531
+ for name, value in {locals: frame.f_locals, globals: frame.f_globals, vars:frame.f_locals}[right].items():
532
+ if not (isinstance(value,PeekModule) or name.startswith("__")):
533
+ pairs.append(Pair(left=name+this.equals_separator,right=value))
534
+ else:
535
+ pairs.append(Pair(left=left, right=right))
517
536
 
518
537
  just_one_line = False
519
538
  if not (len(pairs) > 1 and this.separator == ""):
@@ -701,6 +720,17 @@ class _Peek:
701
720
  return ""
702
721
 
703
722
  def serialize_kwargs(self, obj, width):
723
+ if self.format:
724
+ if isinstance(self.format, str):
725
+ iterator = iter([self.format])
726
+ else:
727
+ iterator = iter(self.format)
728
+ for sub_format in iterator:
729
+ format_string = "{" + sub_format + "}" if sub_format.startswith(":") or sub_format.startswith("!") else "{:" + sub_format + "}"
730
+ try:
731
+ return format_string.format(obj)
732
+ except Exception:
733
+ ...
704
734
  if isinstance(obj, str):
705
735
  if not self.quote_string:
706
736
  return str(self.add_color_value(obj))
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: peek-python
3
- Version: 25.0.0
3
+ Version: 25.0.2
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
@@ -46,15 +46,15 @@ And on top of that, you get some basic benchmarking functionality.
46
46
 
47
47
  * [Use peek.print to use peek like print with extras](#use-peekprint-to-use-peek-like-print-with-extras)
48
48
 
49
+ * [Peeking locals and globals](#peeking-locals-and-globals)
50
+
49
51
  * [Return a string instead of sending to output](#return-a-string-instead-of-sending-to-output)
50
52
 
51
53
  * [Disabling peek's output](#disabling-peeks-output)
52
54
 
53
- * [Using level to control peek output](#Using-level-to-control-peek-output)
54
-
55
- * [Using color to control peek output](#Using-color-to-control-peek-output)
55
+ * [Using filter to control peek output](#using-filter-to-control-peek-output)
56
56
 
57
- * [Copying to the clipboard](#Copying-to-the-clipboard)
57
+ * [Copying to the clipboard](#copying-to-the-clipboard)
58
58
 
59
59
  * [Interpreting the line number information](#interpreting-the-line-number-information)
60
60
 
@@ -346,6 +346,7 @@ enabled - True
346
346
  end - "\n"
347
347
  equals_separator - "="
348
348
  filter f ""
349
+ format fmt ""
349
350
  indent - 1
350
351
  level lvl 0
351
352
  line_length ll 80
@@ -905,7 +906,42 @@ This will print:
905
906
  >
906
907
  > This setting does not influence how strings are displayed within other data structures, like dicts and lists.
907
908
 
909
+ ## format / fmt
910
+ With the format attribute, it is possible to apply a format specifier to each of the values to be printed, like
911
+ ```
912
+ test_float = 1.3
913
+ peek(test_float, format="06.3f")
914
+ ```
915
+ This will print
916
+ ```
917
+ test_float=01.300
918
+ ```
919
+
920
+ The format should be like the Python format specifiers, with or without the `:` prefix, like `"6.3f"`, `">10"`, `"06d"`, `:6.3d`.
921
+ It is also possible to use the `!` format specifier: `"!r"`, `"!r:>10"`.
922
+
923
+ If format is the null string (`""`) (the default), this functionality is skipped completely.
924
+
925
+ It is also possible to use a list (or tuple) of format specifiers, which are tried in succession. If they all fail, the 'normal' serializer will be used.
926
+
927
+ ```
928
+ test_float = 1.3
929
+ test_integer=10
930
+ test_string = "test"
931
+ test_dict=dict(one=1, two=2)
932
+ peek(test_float, test_integer, test_string, test_dict, format=["04d", "06.3f", ">10"])
933
+ ```
934
+
935
+ will result in
936
+
937
+ ```
938
+ test_float=01.300, test_integer=0010, test_string= test, test_dict={'one': 1, 'two': 2}
939
+ ```
940
+
941
+ Of course, format may be put in a peek.toml file.
942
+
908
943
  ## values_only / vo
944
+
909
945
  If False (the default), both the left-hand side (if possible) and the
910
946
  value will be printed. If True, the left hand side will be suppressed:
911
947
 
@@ -1062,6 +1098,35 @@ peek.sep = "|" # sets the 'normal' peek separator
1062
1098
  >
1063
1099
  > `peek.print` does not obey the line length and will always return None (unless as_str is True).
1064
1100
 
1101
+
1102
+ # Peeking locals and globals
1103
+ It is possible to get the name and values of all local or global variables.
1104
+
1105
+ To do that, just put `locals` or `globals` in the call to peek, e.g.:
1106
+
1107
+ ```
1108
+ def my_func():
1109
+ a = 10
1110
+ b = a * a
1111
+ peek(locals)
1112
+ my_func()
1113
+ ```
1114
+
1115
+ will print all local variables, apart from those starting with `__`, so:
1116
+ ```
1117
+ a=10, b=100
1118
+ ```
1119
+
1120
+ Likewise,
1121
+ ```
1122
+ peek(globals)
1123
+ ```
1124
+ will print all global variables, apart from those starting with `__`
1125
+
1126
+ > [!IMPORTANT]
1127
+ >
1128
+ > You should not add parentheses after `locals` or `globals` for peek to work properly!
1129
+
1065
1130
  # Return a string instead of sending to output
1066
1131
 
1067
1132
  `peek(*args, as_str=True)` is like `peek(*args)` but the output is returned as a string instead
@@ -1387,6 +1452,7 @@ can show traceback yes no
1387
1452
  can be used like print w/extras yes (with peek.print) no
1388
1453
  allows non linefeed printing yes (via end parameter) requires patching
1389
1454
  PEP8 (Pythonic) API yes no
1455
+ format specification optional no
1390
1456
  sorts dicts no by default, optional *) yes
1391
1457
  supports compact, indent,
1392
1458
  and underscore_numbers
@@ -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.0"
13
+ version = "25.0.2"
14
14
  readme = "README.md"
15
15
  requires-python = ">=3.6"
16
16
  dependencies = [
@@ -122,6 +122,7 @@ def test_time_delta():
122
122
  time.sleep(0.1)
123
123
  assert 10.05 < peek.delta < 11
124
124
 
125
+
125
126
 
126
127
  def test_dynamic_prefix(capsys):
127
128
  g.i = 0
@@ -307,7 +308,8 @@ def test_print(capsys):
307
308
 
308
309
  result=peek.print(1,2)
309
310
  assert result is None
310
-
311
+ out, err = capsys.readouterr()
312
+ assert out == "1 2\n"
311
313
 
312
314
  with pytest.raises(AttributeError):
313
315
  peek.print(sep="|", sepp="/")
File without changes
File without changes