Allows you to print certain line only once in the loops.
Project description
print-once
print-once is a lightweight Python package designed to help you control the output of specific messages and function calls in your code. It ensures that messages are printed only a limited number of times, which can be useful for reducing console clutter and ensuring unique notifications.
It's well-suited for certain debugging scenarios, such as printing tensor or hyperparameter information only on the first step or the first call during deep learning training.
Installation
Install print-once via pip:
pip install print-once
Usage
print_once
Use print_once as an alternative to print. It will display a message only once, or a specified number of times.
from print_once import print_once
for i in range(100):
print_once(f"called {i}") # This line will be printed only once
for i in range(10, 20):
print_once(f"called {i}", max_times=3) # This line is printed up to 3 times
In the example above, the output will be:
called 0
called 10
called 11
called 12
Note: print_once identifies unique calls based on file path and line number, so calls on different lines will be treated separately. You can understand it as that each line using print_once will execute only once.
call_once
It also provides a call_once function to ensure a custom function is executed only once.
from print_once import call_once
def my_func(x, y):
print(f'{x} + {y} = {x + y}')
for i in range(100):
# `my_func` will be called only once
call_once(my_func, i, i + 1)
The result will be:
0 + 1 = 1
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 print_once-0.0.1.tar.gz.
File metadata
- Download URL: print_once-0.0.1.tar.gz
- Upload date:
- Size: 2.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27117a078e2617a677539b17de973556e245e6f050473a393e0ceb8a6eacd294
|
|
| MD5 |
04d04f2043cb04c6d94b55fba657e6f1
|
|
| BLAKE2b-256 |
497d34924501d872c85e4f7a09a19b3d97fc655c31d8640a3fa1d06131be1c51
|
File details
Details for the file print_once-0.0.1-py3-none-any.whl.
File metadata
- Download URL: print_once-0.0.1-py3-none-any.whl
- Upload date:
- Size: 2.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3a09987fee55cd947f329fc09d0e2b91ad26d29776b7065e8c8df2960812033
|
|
| MD5 |
789d7555aa2b25bed5ced229353d9bd9
|
|
| BLAKE2b-256 |
f7f1174c852817f3eb823e6c0ae4f5a3b5f29ce9c79506616e1f7d1a082fb6d0
|