lyncs-utils


Namelyncs-utils JSON
Version 0.5.1 PyPI version JSON
download
home_pagehttps://github.com/Lyncs-API/lyncs.utils
SummaryCollection of generic-purpose and stand-alone functions
upload_time2024-03-23 16:41:11
maintainerNone
docs_urlNone
authorSimone Bacchio
requires_pythonNone
licenseNone
keywords collection generic-purpose stand-alone functions
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Collection of generic-purpose and stand-alone functions

[![python](https://img.shields.io/pypi/pyversions/lyncs_utils.svg?logo=python&logoColor=white)](https://pypi.org/project/lyncs_utils/)
[![pypi](https://img.shields.io/pypi/v/lyncs_utils.svg?logo=python&logoColor=white)](https://pypi.org/project/lyncs_utils/)
[![license](https://img.shields.io/github/license/Lyncs-API/lyncs.utils?logo=github&logoColor=white)](https://github.com/Lyncs-API/lyncs.utils/blob/master/LICENSE)
[![build & test](https://img.shields.io/github/actions/workflow/status/Lyncs-API/lyncs.utils/ci_cd.yml?logo=github&logoColor=white&branch=master)](https://github.com/Lyncs-API/lyncs.utils/actions)
[![codecov](https://img.shields.io/codecov/c/github/Lyncs-API/lyncs.utils?logo=codecov&logoColor=white)](https://codecov.io/gh/Lyncs-API/lyncs.utils)
[![pylint](https://img.shields.io/badge/pylint%20score-9.4%2F10-green?logo=python&logoColor=white)](http://pylint.pycqa.org/)
[![black](https://img.shields.io/badge/code%20style-black-000000.svg?logo=codefactor&logoColor=white)](https://github.com/ambv/black)

This 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!


## Installation

The package can be installed via `pip`:

```
pip install [--user] lyncs_utils
```


## Documentation

Here the list of functions implemented with a short description. Use `help(lyncs_utils)` for more details.


### Class Utils

Functions and decorator for classes. See `lyncs_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 for _repr_pretty_
- `default(value, type=None, doc=None)`: Attribute with default value and optional type checking
- `methodof(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 self

### Extensions

Extensions of Python standard functions. See `lyncs_utils.extensions`.

- `count`: See itertools.count. Adds __call__ method
- `redirect_stdout`: See contextlib.redirect_stdout. Now, it redirects stdout also from C
- `keydefaultdict`: A defaultdict that passes the key to the factory
- `FreezableDict`: Extension of dict. A dictionary that can be frozen at any moment.
- `cache`: Enables functools.cache for all versions of Python
- `lazy_import(module)`: Lazy import for modules
- `setitems(arr, vals)`: Sets items of an iterable object
- `commonsuffix(words)`: Finds common suffix in words
- `@raiseif(fail, error)`: Decorator that raises `error` if `fail` is `True`
- `RaiseOnUse(error)`: Class instance that raises `error` when used
- `ndict`: A dictionally that supports numerical operations

### Math

Math utils. See `lyncs_utils.math`.

- `prod(arr)`:  Enables math.prod for all versions of Python
- `sign(n)`:  Sign of a number
- `iscomplex(n)`: If n is complex
- `isclose(a,b,warn_tol=None,**)`: math.isclose with support for complex and warning tol
- `factors(n)`: Returns the list of factors of n
- `prime_factors(n)`: Returns the list of prime factors of n

### Functools

Tools for functions. See `lyncs_utils.functools`.

- `is_keyword(key)`: Whether key can be used as a function keyword
- `get_docstring(fnc)`: Returns the docstring of a function or class
- `get_varnames(fnc)`: Returns the list of varnames of the function
- `has_args(fnc)`: Whether the function uses *args
- `has_kwargs(fnc)`: Whether the function uses **kwargs
- `get_defaults(fnc)`: Returns the dict of default values of the function
- `get_annotations(fnc)`: Returns the dict of annotations of the function
- `apply_annotations(fnc, *args, **kwargs)`: Applies the annotations of fnc to the respective *args, **kwargs
- `select_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 argument

### Context Managers

Functionalities using `contextmanager` by `contextlib` meant to be called within a with-statement.
See `lyncs_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/O

Tools for I/O. See `lyncs_utils.io`.

- `@open_file`: Decorator that opens the file (if needed) before calling the function
- `read(fname/fp)`: Reads data from file
- `write(fname/fp)`: Writes data into the file
- `read_struct(fname/fp, format)`: Reads a structure from file
- `write_struct(fname/fp, format, *data)`: Writes a structure from file
- `file_size(fname/fp)`: Returns the file size
- `to_path(fname/fp)`: Returns a Path object to the file
- `dbdict`: Dictionary-like class for storing dictionaries in a database

### Logical

Functions returning or manipulating logical values (boolean). See `lyncs_utils.logical`.

- `single_true(iter)`: Whether one and only one element of the list is True
- `isiterable(obj)`: Whether the object is iteragle or not
- `interactive()`: Whether Python has been run in interactive mode
- `version(num, pkg)`: Compares the version number to the one of a package

### Numpy

Functions returning or manipulating Numpy arrays (available with `lyncs[numpy]`)

- `outer(A,B)`: outer product, alias of `numpy.kron`.
- `gamma_matrices(dim, euclidean=True)`: returns n-dimensional gamma matrices
- `su_generators(N)`: returns NxN generators of su(N)

### Itertools

Functions for iterable objects

- `first(it)`: first element of an iterable
- `last(it)`: last element of an iterable
- `indexes(it,val)`: indexes of occurances of a value in an iterable
- `keys(dict)`: calls keys, if available, or dict.keys
- `values(dict)`: calls values, if available, or dict.values
- `items(dict)`: calls items, if available, or dict.items
- `dictmap(fnc, dict)`: map for dictionaries
- `dictzip(*dicts, fill=True, default=None)`: zip for dictionaries
- `flat_dict(dict, sep=None, base=())`: flat nested dictionaries into a single dict
- `nest_dict(dict, sep=None)`: turns a flat dictionaries into a nested dict
- `allclose(left, right, **)`: applies isclose recursively to iterable objects
- `compact_indexes(ids)`: compats list of integers into ranges where possible

### Pytest

Functions tu use within `pytest`, to be impoted from `lyncs_utils.pytest`

- `DynParam(fnc)`: Dynamic params based on the test function
- `GetMark(map, def)`: Dynamic params based on the marks of the test function
- `lazy_fixture(str)`: Use a fixture as one of the values passed in @pytest.mark.parametrize

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Lyncs-API/lyncs.utils",
    "name": "lyncs-utils",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "Collection, generic-purpose, stand-alone, functions",
    "author": "Simone Bacchio",
    "author_email": "s.bacchio@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/2c/5d/8345d695f1d8ada5ad740cee9694d9cbcc2eec2e69e4320fe93080d69b4f/lyncs_utils-0.5.1.tar.gz",
    "platform": null,
    "description": "# Collection of generic-purpose and stand-alone functions\n\n[![python](https://img.shields.io/pypi/pyversions/lyncs_utils.svg?logo=python&logoColor=white)](https://pypi.org/project/lyncs_utils/)\n[![pypi](https://img.shields.io/pypi/v/lyncs_utils.svg?logo=python&logoColor=white)](https://pypi.org/project/lyncs_utils/)\n[![license](https://img.shields.io/github/license/Lyncs-API/lyncs.utils?logo=github&logoColor=white)](https://github.com/Lyncs-API/lyncs.utils/blob/master/LICENSE)\n[![build & test](https://img.shields.io/github/actions/workflow/status/Lyncs-API/lyncs.utils/ci_cd.yml?logo=github&logoColor=white&branch=master)](https://github.com/Lyncs-API/lyncs.utils/actions)\n[![codecov](https://img.shields.io/codecov/c/github/Lyncs-API/lyncs.utils?logo=codecov&logoColor=white)](https://codecov.io/gh/Lyncs-API/lyncs.utils)\n[![pylint](https://img.shields.io/badge/pylint%20score-9.4%2F10-green?logo=python&logoColor=white)](http://pylint.pycqa.org/)\n[![black](https://img.shields.io/badge/code%20style-black-000000.svg?logo=codefactor&logoColor=white)](https://github.com/ambv/black)\n\nThis package provides a collection of generic-purpose and stand-alone functions that are of common use.\nA characteristic of the package is to be lightweight and dependencies-free.\nPlease, consider its installation if any of the following function can be of use in your project.\nAny addition to the list is very welcome!\n\n\n## Installation\n\nThe package can be installed via `pip`:\n\n```\npip install [--user] lyncs_utils\n```\n\n\n## Documentation\n\nHere the list of functions implemented with a short description. Use `help(lyncs_utils)` for more details.\n\n\n### Class Utils\n\nFunctions and decorator for classes. See `lyncs_utils.class_utils`.\n\n- `@add_to`: Decorator for adding a function to a class\n- `@add_kwargs_of(fnc)`: Decorator for adding kwargs of a function to another\n- `@compute_property`: Decorator for computing a property once and cache the result\n- `@static_property`: Decorator for a static property (like staticmethod)\n- `@class_property`: Decorator for a class property (like classmethod)\n- `call_method(obj, fnc, *args, **kwargs)`: Calls a method of the obj.\n- `default_repr_pretty`: Default method to use for _repr_pretty_\n- `default(value, type=None, doc=None)`: Attribute with default value and optional type checking\n- `methodof(self, func)`: Returns the class where method has been defined\n- `@before_super`: Decorator that call method from super before executing for self\n- `@after_super`: Decorator that call method from super after executing for self\n\n### Extensions\n\nExtensions of Python standard functions. See `lyncs_utils.extensions`.\n\n- `count`: See itertools.count. Adds __call__ method\n- `redirect_stdout`: See contextlib.redirect_stdout. Now, it redirects stdout also from C\n- `keydefaultdict`: A defaultdict that passes the key to the factory\n- `FreezableDict`: Extension of dict. A dictionary that can be frozen at any moment.\n- `cache`: Enables functools.cache for all versions of Python\n- `lazy_import(module)`: Lazy import for modules\n- `setitems(arr, vals)`: Sets items of an iterable object\n- `commonsuffix(words)`: Finds common suffix in words\n- `@raiseif(fail, error)`: Decorator that raises `error` if `fail` is `True`\n- `RaiseOnUse(error)`: Class instance that raises `error` when used\n- `ndict`: A dictionally that supports numerical operations\n\n### Math\n\nMath utils. See `lyncs_utils.math`.\n\n- `prod(arr)`:  Enables math.prod for all versions of Python\n- `sign(n)`:  Sign of a number\n- `iscomplex(n)`: If n is complex\n- `isclose(a,b,warn_tol=None,**)`: math.isclose with support for complex and warning tol\n- `factors(n)`: Returns the list of factors of n\n- `prime_factors(n)`: Returns the list of prime factors of n\n\n### Functools\n\nTools for functions. See `lyncs_utils.functools`.\n\n- `is_keyword(key)`: Whether key can be used as a function keyword\n- `get_docstring(fnc)`: Returns the docstring of a function or class\n- `get_varnames(fnc)`: Returns the list of varnames of the function\n- `has_args(fnc)`: Whether the function uses *args\n- `has_kwargs(fnc)`: Whether the function uses **kwargs\n- `get_defaults(fnc)`: Returns the dict of default values of the function\n- `get_annotations(fnc)`: Returns the dict of annotations of the function\n- `apply_annotations(fnc, *args, **kwargs)`: Applies the annotations of fnc to the respective *args, **kwargs\n- `select_kwargs(fnc, *args, **kwargs)`: Calls fnc passing *args and ONLY the applicable **kwargs\n- `@spy`: Decorator that will log debug information when the function is called\n- `@clickit`: Decorator that adds click.option for any function argument\n\n### Context Managers\n\nFunctionalities using `contextmanager` by `contextlib` meant to be called within a with-statement.\nSee `lyncs_utils.contextlib`.\n\n- `setting(obj, attr, value, default=None)`: Context manager that temporaly sets an attribute of an object.\n- `updating(obj, attr, value, default=None)`: Context manager that temporaly sets an item of an object.\n\n### I/O\n\nTools for I/O. See `lyncs_utils.io`.\n\n- `@open_file`: Decorator that opens the file (if needed) before calling the function\n- `read(fname/fp)`: Reads data from file\n- `write(fname/fp)`: Writes data into the file\n- `read_struct(fname/fp, format)`: Reads a structure from file\n- `write_struct(fname/fp, format, *data)`: Writes a structure from file\n- `file_size(fname/fp)`: Returns the file size\n- `to_path(fname/fp)`: Returns a Path object to the file\n- `dbdict`: Dictionary-like class for storing dictionaries in a database\n\n### Logical\n\nFunctions returning or manipulating logical values (boolean). See `lyncs_utils.logical`.\n\n- `single_true(iter)`: Whether one and only one element of the list is True\n- `isiterable(obj)`: Whether the object is iteragle or not\n- `interactive()`: Whether Python has been run in interactive mode\n- `version(num, pkg)`: Compares the version number to the one of a package\n\n### Numpy\n\nFunctions returning or manipulating Numpy arrays (available with `lyncs[numpy]`)\n\n- `outer(A,B)`: outer product, alias of `numpy.kron`.\n- `gamma_matrices(dim, euclidean=True)`: returns n-dimensional gamma matrices\n- `su_generators(N)`: returns NxN generators of su(N)\n\n### Itertools\n\nFunctions for iterable objects\n\n- `first(it)`: first element of an iterable\n- `last(it)`: last element of an iterable\n- `indexes(it,val)`: indexes of occurances of a value in an iterable\n- `keys(dict)`: calls keys, if available, or dict.keys\n- `values(dict)`: calls values, if available, or dict.values\n- `items(dict)`: calls items, if available, or dict.items\n- `dictmap(fnc, dict)`: map for dictionaries\n- `dictzip(*dicts, fill=True, default=None)`: zip for dictionaries\n- `flat_dict(dict, sep=None, base=())`: flat nested dictionaries into a single dict\n- `nest_dict(dict, sep=None)`: turns a flat dictionaries into a nested dict\n- `allclose(left, right, **)`: applies isclose recursively to iterable objects\n- `compact_indexes(ids)`: compats list of integers into ranges where possible\n\n### Pytest\n\nFunctions tu use within `pytest`, to be impoted from `lyncs_utils.pytest`\n\n- `DynParam(fnc)`: Dynamic params based on the test function\n- `GetMark(map, def)`: Dynamic params based on the marks of the test function\n- `lazy_fixture(str)`: Use a fixture as one of the values passed in @pytest.mark.parametrize\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Collection of generic-purpose and stand-alone functions",
    "version": "0.5.1",
    "project_urls": {
        "Documentation": "https://github.com/Lyncs-API/lyncs.utils/README.md",
        "Download": "https://github.com/Lyncs-API/lyncs.utils/archive/master.zip",
        "Homepage": "https://github.com/Lyncs-API/lyncs.utils",
        "Source": "https://github.com/Lyncs-API/lyncs.utils",
        "Tracker": "https://github.com/Lyncs-API/lyncs.utils/issues"
    },
    "split_keywords": [
        "collection",
        " generic-purpose",
        " stand-alone",
        " functions"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2c5d8345d695f1d8ada5ad740cee9694d9cbcc2eec2e69e4320fe93080d69b4f",
                "md5": "2758432c08c5990df2689e241fd95df4",
                "sha256": "ec6694fd862388959a61808a81acc8e2b1a511706b9863c19fa5dff2b94c22f0"
            },
            "downloads": -1,
            "filename": "lyncs_utils-0.5.1.tar.gz",
            "has_sig": false,
            "md5_digest": "2758432c08c5990df2689e241fd95df4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 27659,
            "upload_time": "2024-03-23T16:41:11",
            "upload_time_iso_8601": "2024-03-23T16:41:11.036245Z",
            "url": "https://files.pythonhosted.org/packages/2c/5d/8345d695f1d8ada5ad740cee9694d9cbcc2eec2e69e4320fe93080d69b4f/lyncs_utils-0.5.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-23 16:41:11",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Lyncs-API",
    "github_project": "lyncs.utils",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "lyncs-utils"
}
        
Elapsed time: 0.51386s