For the full documentation, see www.salabim.org/peek .
- Overhaul of color enable/disable functionality:
as_colored_str
parameter does not exist anymore. Justas_str
, combined withuse_color = True
will do the job. "stdout_no_color" for output does not exist anymore. "stdout", combined withuse_color = False
will do the job. The new attributeuse_color
can be used to control whether ANSI escapes will be emitted. By defaultuse_color
is True, so given colors will be observed.
-
peek now offers direct access to ANSI color escape sequences with
peek.ANSI.black
,peek.ANSI.white
,peek.ANSI.red
,peek.ANSI.green
,peek.ANSI.blue
,peek.ANSI.cyan
,peek.ANSI.magenta
,peek.ANSI.yellow
,peek.ANSI.light_black
,peek.ANSI.light_white
,peek.ANSI.light_red
,peek.ANSI.light_green
,peek.ANSI.light_blue
,peek.ANSI.light_cyan
,peek.ANSI.light_magenta
,peek.ANSI.light_yellow
andpeek.reset
.E.g.
peek(repr(peek.ANSI.red))
will show
repr(peek.ANSI.red)='\x1b[1;31m'
- peek is not supported on Python < 3.9 anymore. Updated the pyproject.toml file accordingly.
- Bug when running under Pythonista fixed.
- Left over from an internal debug print removed.
-
peeking all local or all global variables with the functionality as introduced in 25.0.2 didn't work properly if peek was called directly (not via the PeekModule). Fixed by introducing an optional _via_module to peek.
-
peek no longer issues 'No source' warnings as it now automatically falls back to printing only the value in case the source can't be found or has changed.
-
peek now uses light colors for black, white, ..., yellow, to make the output easier to read. If you would like the 'normal' colors, use dark_black, dark_white, ..., dark_yellow. See the read.me for an overview of the colors.
-
It is now also possible to return peek's output as a string with the embedded ANSI color escape string(s). This is done by setting the
as_colored_str
argument to True:hello = "world" s = peek(hello, color="red", color_value="green", as_colored_str=True) print(repr(s), end="")
prints
"\x1b[1;31mhello=\x1b[1;32m'world'\x1b[1;31m\n\x1b[0m"
-
Introduced the possibility to print all local or all global variables.
To do that, just put
locals
orglobals
in the call to peek:peek(locals)
will print all local variables, apart from those starting with
__
Likewise,
peek(globals)
will print all global variables, apart from those starting with
__
[!IMPORTANT]
You should not add parentheses after
locals
orglobals
for peek to work properly!
-
Introduced the format attribute:
With the format attribute, it is possible to apply a format specifier to each of the values to be printed, like
test_float = 1.3 peek(test_float, format="06.3f")
This will print
test_float=01.300
The format should be like the Python format specifiers, with or without the
:
prefix, like"6.3f"
,">10"
,"06d"
,:6.3d
. It is also possible to use the!
format specifier:"!r"
,"!r:>10"
.If format is the null string (
""
) (the default), this functionality is skipped completely.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.
test_float = 1.3 test_integer=10 test_string = "test" test_dict=dict(one=1, two=2) peek(test_float, test_integer, test_string, test_dict, format=["04d", "06.3f", ">10"])
will result in
test_float=01.300, test_integer=0010, test_string= test, test_dict={'one': 1, 'two': 2}
Of course, format may be put in a peek.toml file.
- internal reorganization: all methods and constants are now in the _Peek class.
-
peek has a new attribute:
print_like
(aliasprint
), which is False by default. If true, peek behaves likepeek.print
.This makes it possible to use
peek
as were itprint
, but with colors, filtering, optional line numbers, timing info, and enabling/disabling.E.g. if we first set
peek.print_like = True
and then
peek(12, f"{min(1, 2)=}", list(range(4))
it will print
2 min(1, 2)=1 [0, 1, 2, 3]
sep
andend
are supported, so after setting print_like to True:peek(12, f"{min(1, 2)=}", list(range(4), sep="|",end="!\n"))
will print
12|min(1, 2)=1|[0, 1, 2, 3]!
It is possible to use
print_like
in a call to peek, likepeek(12, 13, 14, print_like=True)
, but it might be more convenient to use
peek.print(12, 13, 14)
[!TIP]
Of course,
print_like
can be set in a peek.toml file. -
Internal only:
peek.print
now just delegates topeek(print_like=True)
-
Introduced the
end
attribute, which works like the end parameter of print. By default,end
is "\n". This can be useful to have several peek outputs on one line, like:for i in range(5): peek(i*i, end=' ') peek('')
Maybe more useful is to show the output change on the same line, e.g. a status.
import time for i in range(50): peek(f"time {time.time()}",end="\r") time.sleep(0.1) peek('')
Note that
\r
does not work under Pythonista. -
Introduced
peek.print
which allows peek to be used as an alternative to print. Note thatpeek.print
obeys thecolor
,filter
,enabled
andas_str
andoutput
.So we can say
peek.color = "red" peek.filter = "level==1" peek.print(f"{max(1, 2)=}") peek.print(f"{min(1, 2)=}", level=1)
will print
min(1, 2)=1
in red.
In order to behave similar to print, peek has an extra attribute,
separator_print
orsepp
. This attribute (default " ") will be used whenpeek.printing
. When callingpeek.print
,sep
may be used instead. Sopeek.sepp = "|" peek.print("test")
Has the same effect as
peek.print("test", sep="|")
and
peek.print("test", sepp="|")
but not the same as
peek.sep = "|" # sets the 'normal' peek separator
-
enforce_line_length
attribute phased out because of limited use
- When both
peek.color
andpeek.color_value
were "-" or "", ansi escape sequences were still emitted. From now on, peek will suppress these. - From now on
peek.color
may also be "". It acts exactly as "-". Just for your information, ifpeek color_value
is "" it means use thepeek.color
, wheras ifpeek.color_value
is "-", it means switch back to no color.
- Bug when using peek with an istr (see www.salabim.org/istr) and
quote_string = False
. Fixed. - Some minor code cleanups.
-
Some more refactoring to avoid code duplication.
-
Changed several TypeError and ValueError exception to the more logical and consistent AttributeError
-
Implemented
repr
andstr
, where delta is the initial value (which required an alternative way to store delta) -
Added some more test in the PyTest script
-
Completely refactored the way peek defaults and arguments are handled, leading to much more compact and better maintainable code. Also errors are better captured.
-
This change makes it also possible to check for incorrect assignment of peek's attribute, like
peek.colour = 'red'
-
The show_level and show_color methods are replaced by the generic filter attribute, allowing more sophisticated filtering. E.g.
peek.filter('color not in blue' and level >= 2')
It is even possible to get access to all peek attributes in the filter condition, e.g.
peek.filter('delta > 2')
Note that peek checks the validity of a filter expression at definition time.
-
to_clipboard is not anymore just an argument of peek, but is now an attribute. As a consequence, the method
to_clipboard
has been renamed. It is nowcopy_to_clipboard
. -
to_clipboard will now put the last value on the clipboard (was the first)
-
Introduced the quote_string attribute. If this attribute is set to False, strings will be displayed without surrounding quotes (like str). When True (the default), strings will be displayed with surrounding quotes (like repr). E.g.
test='test' peek('==>', test) peek('==>', test, quote_string=False)
This will print:
'==>', test='test' ==>, test=test
-
provided phased out because of limited use
-
assert_ phased out because of limited use
-
peek.toml is now only read once upon importing peek, which is more efficient and stable
-
changed to calendar versioning, so this is version 24.0.0 .
- color_value may now be also the null string ("") to indicate to use the color for the values as well. This also the default now.
- introduced
peek.show_color()
, which makes it possible to show only output of given color(s). It is also possible to exclude given color(s). This works similar topeek.show_level()
, including as a context manager. - changed the no color attribute from
""
to"-"
(this change was necessary forpeek.show_color()
to also include no color). So, for instance,peek.show_color("not -")
will only show colored output. peek.output
may now be "stdout_nocolor", which makes that colors are ignored (this is primarily useful for tests).- micro optimization by not inserting any ansi escape sequences if neither color nor color_value is specified.
- the build process will now automatically insert the latest version for each of the requirements.
- all required modules in the pyproject.toml file now have a mininal version number (all the latest as of now). This is rather important as particularly older versions of executing may not compatible with peek. (inspired by an issue reported by Kirby James)
-
added an alternative way to copy peek output to the clipboard. From now on a call to peek has an optional keyword argument, to_clipboard:
- If to_clipboard==False (the default), nothing is copied to the clipboard.
- If to_clipboard==True, the value of the the first parameter will be copied to the clipboard. The output itself is as usual.
Examples:
part1 = 200 extra = "extra" peek(part1, extra, to_clipboard=True) # will print part1=200, extra='extra' and copy 200 to the clipboard peek(200, to_clipboard=True)\ # will print 200 and copy 200 to the clipboard peek(to_clipboard=True) # will print #5 (or similar) and empty the clipboard
Note that to_clipboard is not a peek attribute and can only be used when calling
peek
, If as_str==True, to_clipboard is ignored.
- updated pyproject.toml to correct the project.url information (inspired by a comment by Erik OShaughnessy)
-
introduced the possibility to copy peek output to the clipboard.
Therefore peek has now a method
to_clipboard
which accepts a value to be copied to the clipboard. So,part1 = 1234 peek.to_clipboard(part1)
will copy
1234
to the clipboard and writecopied to clipboard: 1234
to the console. If the confirmation message is not wanted, just add confirm=False, likepeek.to_clipboard(part1, confirm=False)
Implementation detail: this functionality uses pyperclip, apart from under Pythonista, whre the builtin clipboard module is used.
This functionality is particularly useful for entering an answer of an Advent of Code solution to the site.
(inspired by a comment by Geir Arne Hjelle)
-
show_level
is now a method -
show_level
is called slightly different as it is a method with a number (usually 1) of arguments.Each parameter may be:
- a float value or
- a string with the format from - to , where both from and to are optional. If from is omitted, -1E30 is assumed. If to is omitted, 1E30 is assumed. Negative values have to be parenthesized.
Examples:
peek.show_level (1)
==> show level 1peek.show_level (1, -3)
==> show level 1 and level -3peek.show_level ("1-2")
==> show level between 1 and 2peek.show_level("-")
==> show all levelspeek.show_level("")
==> show no levelspeek.show_level("1-")
==> show all levels >= 1peek.show_level("-10.2")
==> show all levels <=10.2peek.show_level(1, 2, "5-7", "10-")
==> show levels 1, 2, between 5 and 7 (inclusive) and >= 10peek.show_level((-3)-3")
==> show levels between -3 and 3 (inclusive)peek.show_level()
==> returns the current show_level
-
show_level can also be called with a minimum and/or a maximum value, e.g.
peek.show_level(min=1)
==> show all levels >= 1peek.show_level(max=10.2)
==> show all levels <= 10.2peek.show_level(min=1, max=10)
==> show all levels between 1 and 10 (inclusive)
Note that min or max cannot be combined with a specifier as above
-
show_level
can now be used as a context manager as well:with peek.show_level(1): peek(1, level=1) peek(2, level=2)
This will print one line with
1
only. -
color_value introduced. When specified, this is the color with which values are presented, e.g.
test="test" peek(test, color="red", color_value="blue")
-
colors on Pythonista are now handled via an ansi table lookup, thus being more reliable
-
performance of peek when level is not to be shown or enabled==False significantly improved.
show_level
element may be a range now, like "3-5', "-5", "3-".- if
level
is the null string now, output is always suppressed. - from this version on, it is required to have the following modules installed:
asttokens
,colorama
,executing
,six
,tomli
. These packages are in the pyproject.toml's dependencies, so normally these will be auto installed. The reason for this change is that some (potential) users didn't like the encrypted module contents.
- bug with Python < 3.13. Fixed.
-
peek now has an advanced level system, which can be very practical to enable and disable debug information.
See the readme file (e.g. on www.salabim.peek/changelog) for details.
-
peek now supports coloring of peek lines. Therefore, the attribute 'color' has been added.
The following colors are available:
- black
- red
- green
- blue
- yellow
- cyan
- magenta
- white
So, for instance,
peek(message, color="red")
orpeek.color = "green"
The attribute color has an abbreviated form:
col
, so, for instance,peek_green = peek.new(col = "green")
Resetting the color can be done with the null string:
peek.color = ""
.The color attribute can, of course, also be set in a peek.toml file.
-
peek now uses a peek.toml file for customization, instead of a peek.json file. Also, the directory hierarchy is now searched for rather than sys.path.
-
default line length is (again) 80. This change was made because it is now very easy to change the default line length with a toml file.
- peek is now also added to builtins, which means that you can just import it anywhere and it will become available in all modules.
-
default line length is now 160 (was 80)
-
removed the fast disable functionality as from now only peek() without positional arguments is allowed for decorator and context manager
-
phased out decorator/d and context_manager/cm parameters, because of limited use.
-
phased out fast disabling logic, as that is not relevant anymore
-
for decorator and context manager, positional arguments are not allowed. Not obeying this will result in a -possibly quite cryptic- error message
-
@peek without parentheses was always discouraged, and is not allowed anymore
-
use as decorator or context manager is not allowed in the REPL anymore
- finally managed to support the
import peek
functionality correctly.
- the new method of importing with
import peek
didn't work properly. So it is required to usefrom peek import peek
- Output of peek now goes to stdout by default.
- Source of pprint not included anymore.
The only consequence is that under Pyton<=3.7:
- dicts are always sorted under Python <=3.7 (regardless of the sort_dicts attribute)
- numbers cannot be underscored under Python <= 3.7 (regardless of the underscore_numbers attribute)
- The default prefix is now "" (the null string) (was: "peek| ")
- The equals_separator is now "=" (was ": ")
-
Initial release.
-
Rebrand of ycecream with several enhancement, particularly the
import peek
functionality.