Skip to main content

clip_values

Clipping values in a human-readable way

Clipping numerical values to make sure they are within a lower and upper bound is a very common task.

For example, if you are dealing with RGB colours, you want each channel to be between 0 and 255, if you are dealing with shop sales, you need them to be between 0 and 1, or if you are writing a cool game, you want your character to stay inside the screen.

Now, if you have to do all this clipping, which alternative to clipping do you prefer?

Using clip_values.clip

clip offers human-readable syntax to your clipping operations:

from clip_values import clip
colour_channel = clip(colour_channel).between_(0).and_(255)
discount = clip(discount).between_(0).and_(1)
player_x_pos = clip(player_x_pos).between_(0).and_(SCREEN_WIDTH)

The clip alternative is the simplest and easiest to read! Compare it with two other common alternatives:

Using an if: ... elif: ... block is also easy to read, but takes up 4x more lines of code:

if colour_channel < 0:
    colour_channel = 0
elif colour_channel > 255:
    colour_channel = 255

if discount < 0:
    discount = 0
elif discount > 1:
    discount = 1

if player_x_pos < 0:
    player_x_pos = 0
elif player_x_pos > SCREEN_WIDTH:
    player_x_pos = SCREEN_WIDTH

Chaining min with max (or the other way around) is shorter, but this is much harder to read and you have to spend a couple of minutes figuring out the interaction between the two consecutive calls to min/max:

colour_channel = min(255, max(0, colour_channel))
discount = max(0, min(1, discount))
player_x_pos = min(SCREEN_WIDTH, max(0, player_x_pos))

Metadata

Release files for clip-values 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for clip-values 1.0
File Size Uploaded
clip_values-1.0.tar.gz 3.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for clip-values 1.0
File Interpreter ABI Platform
clip_values-1.0-py3-none-any.whl Python 3 none any Details

Total release size: 6.1 kB

Release files / clip_values-1.0.tar.gz

Download URL clip_values-1.0.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f210ffac019b8bfe3a01656dc00106d75086f546b6a3697f4627fe73f690db88
BLAKE2b-256 checksum
How to use checksums
daf25aa8463b297edf7eeeaadb27ca0e0881b37882506f36860570461223a214
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

Release files / clip_values-1.0-py3-none-any.whl

Download URL clip_values-1.0-py3-none-any.whl
Size 3.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2626f2f84bf3ff7d99515ba8f740ec0fe7208e071f790ef290581e2f6fb413cc
BLAKE2b-256 checksum
How to use checksums
1e7b48b72bff4e34db87140721933323a0bfd120d69fc833f7c089539a18e99d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page