A clamped integer type with range-aware arithmetic, callbacks, and lerp utilities.
Project description
RangeInt
A clamped numeric type for Python. Values are automatically kept within a [min, max] range — all arithmetic, assignment, and mutations clamp silently. Built for game dev, UI sliders, stat systems, animation, or anywhere you want a number that stays in bounds.
from rangeint import RangeInt
hp = RangeInt(100, 0, 100)
hp -= 999
print(hp) # 0 — never goes below min
Install
pip install rangeint
Quick Start
from rangeint import RangeInt
volume = RangeInt(50, 0, 100)
volume += 80 # → 100 (clamped at max)
volume -= 200 # → 0 (clamped at min)
print(int(volume)) # 0
print(float(volume)) # 0.0
print(volume == 0) # True
API
Constructor
RangeInt(value, min_val, max_val)
Raises ValueError if min_val == max_val.
Properties
| Property | Description |
|---|---|
.value |
Current value (clamped on set) |
.min |
Lower bound |
.max |
Upper bound |
Methods
relative() -> float
Returns the value's position as a float between 0.0 and 1.0.
RangeInt(75, 0, 100).relative() # 0.75
lerp_to(percentage) -> float
Sets the value to a fractional position between min and max.
r = RangeInt(0, 0, 200)
r.lerp_to(0.5) # 100.0
nudge_percentage(amt) -> int
Shifts the value by a fraction of the total range.
r = RangeInt(50, 0, 100)
r.nudge_percentage(0.1) # 60
set_range(min_val, max_val) -> RangeInt
Updates the bounds and re-clamps the current value. Chainable.
r = RangeInt(80, 0, 100)
r.set_range(0, 50) # value clamped to 50
edge_check(val_check) -> bool
Returns True if the value is at "min" or "max".
RangeInt(0, 0, 100).edge_check("min") # True
RangeInt(0, 0, 100).edge_check("max") # False
is_between(low, high) -> bool
Returns True if the relative position falls within [low, high].
RangeInt(50, 0, 100).is_between(0.25, 0.75) # True
Callbacks
Register a function to fire whenever the value crosses a relative threshold.
r = RangeInt(0, 0, 100)
r._callbacks[0.5] = lambda: print("crossed 50%!")
r.value = 60 # prints "crossed 50%!"
r.value = 30 # prints "crossed 50%!" (crossed back down)
Operators
| Operator | Supported |
|---|---|
+, -, *, /, //, %, ** |
Yes |
+=, -=, *=, /= |
Yes |
Reflected +, -, * |
Yes |
-x, abs(x) |
Yes |
==, <, <=, >, >= |
Yes |
int(), float(), bool() |
Yes |
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rangeint-1.0.0.tar.gz.
File metadata
- Download URL: rangeint-1.0.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acb494fa968944992c4fa69de619b16f9dd47645cfab1dd070c8717a5b04c049
|
|
| MD5 |
60200d51c89b1504e159837465710322
|
|
| BLAKE2b-256 |
9821d5bce5edecb89b71d65ff7f56196f47648add40b40b30b17d46ae39072ba
|
File details
Details for the file rangeint-1.0.0-py3-none-any.whl.
File metadata
- Download URL: rangeint-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
756a93f782bbdd197d2fcdef7c2e762b5ae9ab99e2c77daca81a5ae0fc629c6f
|
|
| MD5 |
1150414fb352b7e960c912f80efaf053
|
|
| BLAKE2b-256 |
d43cf2768d5c1aa91f1dabfb26ef265a93e0a9037b7504c4513ab6b4e55d0b03
|