Skip to content

Repository files navigation

DownloadsDownloadsCoverage StatusLines of codeHits-of-CodeTest-PackagePython versionsPyPI versionChecked with mypyRuffDeepWiki

logo

It's a micro-library for customizing sys.displayhook.

If you need to change the default behavior of sys.displayhook, this library lets you do it:

  • 💎 declaratively
  • 🫥 compactly
  • 🌞 beautifully

Table of contents

Quick start

Install it:

pip install displayhooks

Then use:

importsysfromdisplayhooksimportnot_displaynot_display(int)
sys.displayhook('Most of the adventures recorded in this book really occurred; one or two were experiences of my own, the rest those of boys who were schoolmates of mine.')
#> 'Most of the adventures recorded in this book really occurred; one or two were experiences of my own, the rest those of boys who were schoolmates of mine.'sys.displayhook(666)
# [nothing!]

Transform displayed values

You can declaratively define a converter function for displayed values. Its return value will be passed to the original displayhook function.

importsysfromdisplayhooksimportconverted_displayhook@converted_displayhookdefnew_displayhook(value):
returnvalue.lower()
sys.displayhook("What’s gone with that boy, I wonder? You TOM!")
#> 'what’s gone with that boy, i wonder? you tom!'

If your function returns None, nothing is displayed.

Prohibiting the display of certain types of values

You can disable the display of certain data types, similar to how NoneType values are ignored by default.

You could already see a similar example above, let's look at it again:

importsysfromtypesimportFunctionTypefromdisplayhooksimportnot_displaynot_display(FunctionType)
sys.displayhook('Nothing! Look at your hands. And look at your mouth. What is that truck?')
#> 'Nothing! Look at your hands. And look at your mouth. What is that truck?'sys.displayhook(lambdax: x)
# [nothing!]

In this example, you can see that we have disabled the display for functions, but all other data types are displayed unchanged.

Automatic recovery of the default hook

You can limit the impact on sys.displayhook only to the code inside a function. If you hang the autorestore_displayhook decorator on it, after exiting the function, the displayhook that was installed before it was called will be automatically restored:

importsysfromtypesimportFunctionTypefromdisplayhooksimportnot_display, autorestore_displayhook@autorestore_displayhookdefdo_something_dangerous():
not_display(FunctionType)
sys.displayhook(do_something_dangerous)
# [nothing!]do_something_dangerous()
sys.displayhook(do_something_dangerous)
#> <function do_something_dangerous at 0x104c980e0>

About

Extend the capabilities of the standard displayhook in Python

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages