lkj


Namelkj JSON
Version 0.1.29 PyPI version JSON
download
home_pagehttps://github.com/thorwhalen/lkj
SummaryA dump of homeless useful utils
upload_time2024-11-12 11:37:08
maintainerNone
docs_urlNone
authorThor Whalen
requires_pythonNone
licenseapache-2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # lkj

Lightweight Kit Jumpstart. A place for useful python utils built only with pure python.

To install:	```pip install lkj```

[Documentation](https://i2mint.github.io/lkj)

Note: None of the tools here require anything else but pure python.
Additionally, things are organized in such a way that these can be 
easily copy-pasted into other projects. 
That is, modules are all self contained (so can easily be copy-paste-vendored 
(do be nice and mention the source!))
Further, many functions will contain their own imports: Those functions can even be 
copy-paste-vendored by just copying the function body.


# Examples of utils

## loggers

### clog

Conditional log

```python
>>> clog(False, "logging this")
>>> clog(True, "logging this")
logging this
```

One common usage is when there's a verbose flag that allows the user to specify
whether they want to log or not. Instead of having to litter your code with
`if verbose:` statements you can just do this:

```python
>>> verbose = True  # say versbose is True
>>> _clog = clog(verbose)  # makes a clog with a fixed condition
>>> _clog("logging this")
logging this
```

You can also choose a different log function.
Usually you'd want to use a logger object from the logging module,
but for this example we'll just use `print` with some modification:

```python
>>> _clog = clog(verbose, log_func=lambda x: print(f"hello {x}"))
>>> _clog("logging this")
hello logging this
```

### print_with_timestamp

Prints with a timestamp and optional refresh.
- input: message, and possibly args (to be placed in the message string, sprintf-style
- output: Displays the time (HH:MM:SS), and the message
- use: To be able to track processes (and the time they take)

```python
>>> print_with_timestamp('processing element X')
(29)09:56:36 - processing element X
```

### return_error_info_on_error

Decorator that returns traceback and local variables on error.

This decorator is useful for debugging. It will catch any exceptions that occur
in the decorated function, and return an ErrorInfo object with the traceback and
local variables at the time of the error.
- `func`: The function to decorate.
- `caught_error_types`: The types of errors to catch.
- `error_info_processor`: A function that processes the ErrorInfo object.

Tip: To parametrize this decorator, you can use a functools.partial function.

Tip: You can have your error_info_processor persist the error info to a file or
database, or send it to a logging service.

```python
>>> from lkj import return_error_info_on_error, ErrorInfo
>>> @return_error_info_on_error
... def foo(x, y=2):
...     return x / y
...
>>> t = foo(1, 2)
>>> assert t == 0.5
>>> t = foo(1, y=0)
Exiting from foo with error: division by zero
>>> if isinstance(t, ErrorInfo):
...     assert isinstance(t.error, ZeroDivisionError)
...     hasattr(t, 'traceback')
...     assert t.locals['args'] == (1,)
...     assert t.locals['kwargs'] == {'y': 0}
```

## Miscellaneous

### chunker

Chunk an iterable into non-overlapping chunks of size chk_size.

```python
chunker(a, chk_size, *, include_tail=True)
```

```python
>>> from lkj import chunker
>>> list(chunker(range(8), 3))
[(0, 1, 2), (3, 4, 5), (6, 7)]
>>> list(chunker(range(8), 3, include_tail=False))
[(0, 1, 2), (3, 4, 5)]
```

### import_object

Import and return an object from a dot string path.

```python
import_object(dot_path: str)
```

```python 
>>> f = import_object('os.path.join')
>>> from os.path import join
>>> f is join
True
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/thorwhalen/lkj",
    "name": "lkj",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "Thor Whalen",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/1b/a2/b995b291d9bb5127e4ec042d78adfe7a6cf641e625d2cc4f42830f5e4a80/lkj-0.1.29.tar.gz",
    "platform": "any",
    "description": "# lkj\n\nLightweight Kit Jumpstart. A place for useful python utils built only with pure python.\n\nTo install:\t```pip install lkj```\n\n[Documentation](https://i2mint.github.io/lkj)\n\nNote: None of the tools here require anything else but pure python.\nAdditionally, things are organized in such a way that these can be \neasily copy-pasted into other projects. \nThat is, modules are all self contained (so can easily be copy-paste-vendored \n(do be nice and mention the source!))\nFurther, many functions will contain their own imports: Those functions can even be \ncopy-paste-vendored by just copying the function body.\n\n\n# Examples of utils\n\n## loggers\n\n### clog\n\nConditional log\n\n```python\n>>> clog(False, \"logging this\")\n>>> clog(True, \"logging this\")\nlogging this\n```\n\nOne common usage is when there's a verbose flag that allows the user to specify\nwhether they want to log or not. Instead of having to litter your code with\n`if verbose:` statements you can just do this:\n\n```python\n>>> verbose = True  # say versbose is True\n>>> _clog = clog(verbose)  # makes a clog with a fixed condition\n>>> _clog(\"logging this\")\nlogging this\n```\n\nYou can also choose a different log function.\nUsually you'd want to use a logger object from the logging module,\nbut for this example we'll just use `print` with some modification:\n\n```python\n>>> _clog = clog(verbose, log_func=lambda x: print(f\"hello {x}\"))\n>>> _clog(\"logging this\")\nhello logging this\n```\n\n### print_with_timestamp\n\nPrints with a timestamp and optional refresh.\n- input: message, and possibly args (to be placed in the message string, sprintf-style\n- output: Displays the time (HH:MM:SS), and the message\n- use: To be able to track processes (and the time they take)\n\n```python\n>>> print_with_timestamp('processing element X')\n(29)09:56:36 - processing element X\n```\n\n### return_error_info_on_error\n\nDecorator that returns traceback and local variables on error.\n\nThis decorator is useful for debugging. It will catch any exceptions that occur\nin the decorated function, and return an ErrorInfo object with the traceback and\nlocal variables at the time of the error.\n- `func`: The function to decorate.\n- `caught_error_types`: The types of errors to catch.\n- `error_info_processor`: A function that processes the ErrorInfo object.\n\nTip: To parametrize this decorator, you can use a functools.partial function.\n\nTip: You can have your error_info_processor persist the error info to a file or\ndatabase, or send it to a logging service.\n\n```python\n>>> from lkj import return_error_info_on_error, ErrorInfo\n>>> @return_error_info_on_error\n... def foo(x, y=2):\n...     return x / y\n...\n>>> t = foo(1, 2)\n>>> assert t == 0.5\n>>> t = foo(1, y=0)\nExiting from foo with error: division by zero\n>>> if isinstance(t, ErrorInfo):\n...     assert isinstance(t.error, ZeroDivisionError)\n...     hasattr(t, 'traceback')\n...     assert t.locals['args'] == (1,)\n...     assert t.locals['kwargs'] == {'y': 0}\n```\n\n## Miscellaneous\n\n### chunker\n\nChunk an iterable into non-overlapping chunks of size chk_size.\n\n```python\nchunker(a, chk_size, *, include_tail=True)\n```\n\n```python\n>>> from lkj import chunker\n>>> list(chunker(range(8), 3))\n[(0, 1, 2), (3, 4, 5), (6, 7)]\n>>> list(chunker(range(8), 3, include_tail=False))\n[(0, 1, 2), (3, 4, 5)]\n```\n\n### import_object\n\nImport and return an object from a dot string path.\n\n```python\nimport_object(dot_path: str)\n```\n\n```python \n>>> f = import_object('os.path.join')\n>>> from os.path import join\n>>> f is join\nTrue\n```\n",
    "bugtrack_url": null,
    "license": "apache-2.0",
    "summary": "A dump of homeless useful utils",
    "version": "0.1.29",
    "project_urls": {
        "Homepage": "https://github.com/thorwhalen/lkj"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "395d21e6d5c652ee6d98331b05fc030baacb7b72f9e70799cf02a69a4374a39d",
                "md5": "f07b75ddab336aa62c1b3e21bb4c6d76",
                "sha256": "248a2f27bafd275ef68c55c9d41715a27b844ee2a905fcbca7fd11fc9147c68b"
            },
            "downloads": -1,
            "filename": "lkj-0.1.29-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f07b75ddab336aa62c1b3e21bb4c6d76",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 21370,
            "upload_time": "2024-11-12T11:37:07",
            "upload_time_iso_8601": "2024-11-12T11:37:07.671598Z",
            "url": "https://files.pythonhosted.org/packages/39/5d/21e6d5c652ee6d98331b05fc030baacb7b72f9e70799cf02a69a4374a39d/lkj-0.1.29-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1ba2b995b291d9bb5127e4ec042d78adfe7a6cf641e625d2cc4f42830f5e4a80",
                "md5": "7d7376f092783b498903ffa34c9d362e",
                "sha256": "7e9012ad1bd9fb50dc34e7ac90642cba4ff6f1cd53b75452a63d25de5b3e3b80"
            },
            "downloads": -1,
            "filename": "lkj-0.1.29.tar.gz",
            "has_sig": false,
            "md5_digest": "7d7376f092783b498903ffa34c9d362e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 19733,
            "upload_time": "2024-11-12T11:37:08",
            "upload_time_iso_8601": "2024-11-12T11:37:08.545805Z",
            "url": "https://files.pythonhosted.org/packages/1b/a2/b995b291d9bb5127e4ec042d78adfe7a6cf641e625d2cc4f42830f5e4a80/lkj-0.1.29.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-12 11:37:08",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "thorwhalen",
    "github_project": "lkj",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "lkj"
}
        
Elapsed time: 1.29563s