yikit.helpers package#
This package contains helper functions and classes that are used throughout the project.
The functions and classes in this package are designed to be reusable and modular, making it easy to incorporate them into different parts of the project. These helpers are intended to simplify common tasks, such as checking if a package is installed, iterating with a dummy progress bar, and verifying function arguments.
- class yikit.helpers.dummy_tqdm(_dummy_tqdm__iterable: Iterable[T] | None = None, *args, **kwargs)#
Bases:
Iterable,Generic[T]Stand-in for
tqdmthat shows no progress bar.It replaces
tqdmwhen tqdm is not installed or no progress bar is wanted. Iterating over it yields the items of the given iterable in order. Any other attribute, such asupdate,closeorset_postfix, is a function that accepts any arguments and does nothing.- Parameters:
__iterable (iterable or None, default=None) – The iterable to iterate over, passed positionally (an
iterable=keyword is ignored like the other keyword arguments). None, or any other false value, means that nothing is iterated.*args – Ignored; accepted for compatibility with
tqdm.**kwargs – Ignored; accepted for compatibility with
tqdm(such astotalanddesc).
Examples
>>> from yikit.helpers import dummy_tqdm >>> list(dummy_tqdm([3, 1, 2], desc="fit")) [3, 1, 2] >>> progress_bar = dummy_tqdm(total=3) >>> progress_bar.update(1) >>> progress_bar.close()
- yikit.helpers.is_argument(__callable: Callable[[...], Any], arg_name: str) bool#
Return whether a callable has a parameter of the given name.
The names of the parameters are read with
inspect.signature(). Variable parameters count by their own names ("kwargs"for**kwargs); they do not make other names parameters.- Parameters:
__callable (callable) – The function, method or class to inspect (passed positionally). The signature of a bound method does not include
self, and that of a class is the signature of its constructor.arg_name (str) – The name of the parameter.
- Returns:
True if
arg_nameis the name of a parameter of__callable, False otherwise.- Return type:
bool
- Raises:
TypeError – If
__callableis not callable.ValueError – If no signature can be read from
__callable(such as some built-in functions).
Examples
>>> from yikit.helpers import is_argument >>> def fit(X, y, sample_weight=None, **kwargs): ... pass >>> is_argument(fit, "sample_weight") True >>> is_argument(fit, "eval_set") False
- yikit.helpers.is_installed(package_name: str) bool#
Return whether a package can be imported.
The package is looked up with
importlib.util.find_spec(), so a top-level package is found without being imported.- Parameters:
package_name (str) – The name used to import the package, such as
"sklearn"(not the name of the distribution,"scikit-learn").- Returns:
True if the package is installed, False otherwise.
- Return type:
bool
- Raises:
ModuleNotFoundError – If
package_nameis a dotted name (such as"a.b") whose parent package is not installed. The parent packages of a dotted name are imported.
Examples
>>> from yikit.helpers import is_installed >>> is_installed("numpy") True >>> is_installed("yikit_package_that_does_not_exist") False
- class yikit.helpers.tqdm_joblib(total: int, silent: bool = False, **tqdm_kwargs: Any)#
Bases:
objectShow the progress of
joblib.Parallelwith a progress bar.Inside the context,
joblib.Parallelcalls move the progress bar forward by the number of tasks they have finished, whatevern_jobsis (including the sequentialn_jobs=1andNone). With several jobs the bar may move by batches of tasks. The bar is atqdm.auto.tqdm, or adummy_tqdmthat shows nothing when tqdm is not installed orsilentis True.A call is counted when it is started on the thread that entered the context while no other counted call of the context is running. So the work of the outermost
Parallelis counted, and theParallelcalls that its tasks start (on the same thread withn_jobs=1, or on worker threads) are not. Calls with areturn_asother than"list"(joblib >= 1.3), which return a generator before their tasks run, are not counted either; as such a call is not counted, theParallelcalls that its tasks start on the thread of the context (withn_jobs=1) are counted.Contexts can be nested, and separate threads can use their own contexts at the same time. With nested contexts on a thread, the innermost active one counts; a context entered inside a task of a counted call counts the calls of that task.
While any context is active,
joblib.parallel.Parallel.__call__andParallel.print_progressare replaced. They are restored when the last active context exits, also when an exception is raised.- Parameters:
total (int) – The expected number of tasks, passed to tqdm.
silent (bool, default=False) – If True, use
dummy_tqdmeven when tqdm is installed, so that no progress bar is shown.**tqdm_kwargs – Other keyword arguments passed to tqdm, such as
desc.
- total#
The expected number of tasks.
- Type:
int
- silent#
Whether no progress bar is shown.
- Type:
bool
- tqdm_kwargs#
The other keyword arguments passed to tqdm.
- Type:
dict
- tqdm#
The progress bar made on the latest entering (closed after the context exits), or None before the context is entered.
- Type:
tqdm, dummy_tqdm or None
Examples
>>> from joblib import Parallel, delayed >>> from yikit.helpers import tqdm_joblib >>> with tqdm_joblib(total=3, desc="squares") as progress_bar: ... squares = Parallel(n_jobs=1)(delayed(pow)(i, 2) for i in range(3)) >>> squares [0, 1, 4]
Use
silentto hide the progress bar:>>> with tqdm_joblib(total=3, silent=True): ... squares = Parallel(n_jobs=2)(delayed(pow)(i, 2) for i in range(3)) >>> squares [0, 1, 4]