Skip to main content

Utils for data python project

Project description

pytils

Utils for data python projects. Simple, straightforward, but time saving.

  • pickledays - decorator of class. Save the object state to the filesystem after it's initialization for some defined time. Load object after another call.
  • singleton - decorator of class. Share object to all calls. There will be only one instance.
  • logger - log the info messages to file, stream and discord.
  • log - decorator of function. Log the function call into the logger.
  • configurator - additional method for Dynaconf, which create variable in settings file.
  • pandas_table - additional method for pandas, which save the DataFrame to specific sheet. Differs from standard df.to_excel by saving other sheets.

Installation

From the PiPy:

pip install pytils-functions

Directly from github:

pip install git+https://github.com/Whisperes/pytils.git

How to log

Discord logs

Straight logs

Log to the handlers:

  • discord webhook,
  • streamhandler
  • timerotation file.

For each handler you can set your own log level in the settings.toml file in your project (see Dynaconf package)

from pytils.logger import logger

logger.debug("this is a debugging message")
logger.success("this is a success message")
logger.info("this is an informational message")
logger.warning("this is a warning message")
logger.notice("this is a notice message")
logger.error("this is an error message")
logger.critical("this is a critical message")
logger.log(89, "this is a number message")

How to decorate functions for logs

from pytils.logger import log

@log()
def my_function(a=[1,3], b=2, c=3):
    //do something

answer = my_function([11, 'beta'], 2, c=3)

It will be loged like this:

2022-09-02 18:13:25 my-pc |[3812] DEBUG Processing my_function: ([11, 'beta'], 2), {'c': 3}

2022-09-02 18:13:25 my-pc |[3812] SUCCESS my_function: ([11, 'beta'], 2), {'c': 3}

Add log level

Additional levels added to logging module:

  • SUCCESS (15)

  • NOTICE (25)

    from pytils.logger import addLoggingLevel

    addLoggingLevel('MY_LOG_LEVEL', 45)

Cashe

Cashe object to the disk

from pytils.pickler import pickledays

@pickledays(period=2)
class A:
    def __init__(self, var):
        self.var = var

a = A(2)

Object A will be saved for 2 days. New call A(2) will take the state of object from pickle file. Req: object have to be immutable.

No duplicates

from pytils.singleton import Singleton_args


@Singleton_args
class A:
    def __init__(self, var):
        self.var = var

a = A(2)

Just singleton this. Your object with sspecific set of args will be be the only one through the whole code.

Retry errors

@retry(retries=5, delay=1)
def example_function():
    import random
    if random.random() < 0.8:
        raise ValueError("Random error")
    else:
        return('True')



example_function()

Decorate function with failing possibility. Delay in seconds.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytils-functions-0.1.8.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

pytils_functions-0.1.8-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file pytils-functions-0.1.8.tar.gz.

File metadata

  • Download URL: pytils-functions-0.1.8.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.13

File hashes

Hashes for pytils-functions-0.1.8.tar.gz
Algorithm Hash digest
SHA256 61a7a03f0e5bb44d113a1bd7c0b9186ff6cca7a498ac18d3787fb0a657dd2c83
MD5 febf6bb923746994a05f98c7f19cec0d
BLAKE2b-256 7b0d7ae856e88fd366e8ed0db1d1197e7e8eceb16ba0ca0f148e462e03d89747

See more details on using hashes here.

File details

Details for the file pytils_functions-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for pytils_functions-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 35e5c9e361e520a12cedef4e3ef139d0152b6f1594bcd75701c5aacb2ed7aeeb
MD5 083d49261c43b78520a0044225cac8e5
BLAKE2b-256 612973f8a87e27883f6e42b70f1a027de7bb80dcafc9c76a8353733036536541

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page