Package: lyncs-utils
Collection of generic-purpose and stand-alone functionsThis package provides a collection of generic-purpose and stand-alone functions that are of common use.
A characteristic of the package is to be lightweight and dependencies-free.
Please, consider its installation if any of the following function can be of use in your project.
Any addition to the list is very welcome!InstallationThe package can be installed viapip:pip install [--user] lyncs_utilsDocumentationHere the list of functions implemented with a short description. Usehelp(lyncs_utils)for more details.Class UtilsFunctions and decorator for classes. Seelyncs_utils.class_utils.@add_to: Decorator for adding a function to a class@add_kwargs_of(fnc): Decorator for adding kwargs of a function to another@compute_property: Decorator for computing a property once and cache the result@static_property: Decorator for a static property (like staticmethod)@class_property: Decorator for a class property (like classmethod)call_method(obj, fnc, *args, **kwargs): Calls a method of the obj.default_repr_pretty: Default method to use forrepr_prettydefault(value, type=None, doc=None): Attribute with default value and optional type checkingmethodof(self, func): Returns the class where method has been defined@before_super: Decorator that call method from super before executing for self@after_super: Decorator that call method from super after executing for selfExtensionsExtensions of Python standard functions. Seelyncs_utils.extensions.count: See itertools.count. Addscallmethodredirect_stdout: See contextlib.redirect_stdout. Now, it redirects stdout also from Ckeydefaultdict: A defaultdict that passes the key to the factoryFreezableDict: Extension of dict. A dictionary that can be frozen at any moment.cache: Enables functools.cache for all versions of Pythonlazy_import(module): Lazy import for modulessetitems(arr, vals): Sets items of an iterable objectcommonsuffix(words): Finds common suffix in words@raiseif(fail, error): Decorator that raiseserroriffailisTrueRaiseOnUse(error): Class instance that raiseserrorwhen usedndict: A dictionally that supports numerical operationsMathMath utils. Seelyncs_utils.math.prod(arr):  Enables math.prod for all versions of Pythonsign(n):  Sign of a numberiscomplex(n): If n is complexisclose(a,b,warn_tol=None,**): math.isclose with support for complex and warning tolfactors(n): Returns the list of factors of nprime_factors(n): Returns the list of prime factors of nFunctoolsTools for functions. Seelyncs_utils.functools.is_keyword(key): Whether key can be used as a function keywordget_docstring(fnc): Returns the docstring of a function or classget_varnames(fnc): Returns the list of varnames of the functionhas_args(fnc): Whether the function uses *argshas_kwargs(fnc): Whether the function uses **kwargsget_defaults(fnc): Returns the dict of default values of the functionget_annotations(fnc): Returns the dict of annotations of the functionapply_annotations(fnc, *args, **kwargs): Applies the annotations of fnc to the respective *args, **kwargsselect_kwargs(fnc, *args, **kwargs): Calls fnc passing *args and ONLY the applicable **kwargs@spy: Decorator that will log debug information when the function is called@clickit: Decorator that adds click.option for any function argumentContext ManagersFunctionalities usingcontextmanagerbycontextlibmeant to be called within a with-statement.
Seelyncs_utils.contextlib.setting(obj, attr, value, default=None): Context manager that temporaly sets an attribute of an object.updating(obj, attr, value, default=None): Context manager that temporaly sets an item of an object.I/OTools for I/O. Seelyncs_utils.io.@open_file: Decorator that opens the file (if needed) before calling the functionread(fname/fp): Reads data from filewrite(fname/fp): Writes data into the fileread_struct(fname/fp, format): Reads a structure from filewrite_struct(fname/fp, format, *data): Writes a structure from filefile_size(fname/fp): Returns the file sizeto_path(fname/fp): Returns a Path object to the filedbdict: Dictionary-like class for storing dictionaries in a databaseLogicalFunctions returning or manipulating logical values (boolean). Seelyncs_utils.logical.single_true(iter): Whether one and only one element of the list is Trueisiterable(obj): Whether the object is iteragle or notinteractive(): Whether Python has been run in interactive modeversion(num, pkg): Compares the version number to the one of a packageNumpyFunctions returning or manipulating Numpy arrays (available withlyncs[numpy])outer(A,B): outer product, alias ofnumpy.kron.gamma_matrices(dim, euclidean=True): returns n-dimensional gamma matricessu_generators(N): returns NxN generators of su(N)ItertoolsFunctions for iterable objectsfirst(it): first element of an iterablelast(it): last element of an iterableindexes(it,val): indexes of occurances of a value in an iterablekeys(dict): calls keys, if available, or dict.keysvalues(dict): calls values, if available, or dict.valuesitems(dict): calls items, if available, or dict.itemsdictmap(fnc, dict): map for dictionariesdictzip(*dicts, fill=True, default=None): zip for dictionariesflat_dict(dict, sep=None, base=()): flat nested dictionaries into a single dictnest_dict(dict, sep=None): turns a flat dictionaries into a nested dictallclose(left, right, **): applies isclose recursively to iterable objectscompact_indexes(ids): compats list of integers into ranges where possiblePytestFunctions tu use withinpytest, to be impoted fromlyncs_utils.pytestDynParam(fnc): Dynamic params based on the test functionGetMark(map, def): Dynamic params based on the marks of the test function