Skip to main content

Extend the capabilities of the standard displayhook in Python

Project description

Downloads Downloads Coverage Status Lines of code Hits-of-Code Test-Package Python versions PyPI version Checked with mypy Ruff DeepWiki

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 it:

import sys
from displayhooks import not_display

not_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.

import sys
from displayhooks import converted_displayhook

@converted_displayhook
def new_displayhook(value):
    return value.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:

import sys
from types import FunctionType
from displayhooks import not_display

not_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(lambda x: 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:

import sys
from types import FunctionType
from displayhooks import not_display, autorestore_displayhook

@autorestore_displayhook
def do_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>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

displayhooks-0.0.6.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

displayhooks-0.0.6-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file displayhooks-0.0.6.tar.gz.

File metadata

  • Download URL: displayhooks-0.0.6.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for displayhooks-0.0.6.tar.gz
Algorithm Hash digest
SHA256 38e77bc26fc425dd10a949017b23aebc0629d60d7b1c309bc160619cf7afa775
MD5 4e648a10bb7d36adf0e100fb2ce8fadf
BLAKE2b-256 fdb9024d6ea8b6b79a176415f368f0ecb16e150c42d9c8c7d243c43eb56fdb26

See more details on using hashes here.

Provenance

The following attestation bundles were made for displayhooks-0.0.6.tar.gz:

Publisher: release.yml on mutating/displayhooks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file displayhooks-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: displayhooks-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for displayhooks-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 9fa2fe0a2477dfe8d01beb25f1dd6ab1f3bf333e6865692d288ab6cf9c7ec646
MD5 61704e1fc8b4db7c439ba14ab16e9ccd
BLAKE2b-256 eee13edaeb96044045cfe50997f930b7a7ae4473418bbe074461942cb8f02ae4

See more details on using hashes here.

Provenance

The following attestation bundles were made for displayhooks-0.0.6-py3-none-any.whl:

Publisher: release.yml on mutating/displayhooks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page