Skip to main content

A utility python library for writing certain tasks in python easily & elegantly.

Project description

CAPSLOCK


A utility python library for writing certain tasks in python easily & elegantly.


[The library is still in development. The doc is not completed yet. You can contribute to improve the library.]

Background

capslock is a high level utility library written in python for writing certain frequently needed task in faster & efficient way. For example, if you want to keep track of the execution time of one of your method while optimizing it, witing code for tracking execution time can be done easily using capslock.


Installation

Install using the following command -

pip install capslock

Uninstall using the following command -

pip uninstall capslock

Getting Started

How to use decorators from capslock

Capslock defines different decorators that can be used out of the box for certain frequent tasks. E.g. getting the run time of certain function over the period of optimization in development phase.

Timing Decorator

To keep track of the execution time of a function in your project for optimizing it over the time, just put the "timing" decorator in your desired function. Capslock will keep track of different run of that function and will plot a well visualized graph for last five execution time of that function.

from capslock import timing

@timing(plot=True)
def say_hello():
    print("Hello World")

if __name__ == '__main__':
    say_hello()

This will generate output like bellow:

Output of Capslock Timing Decorator

And it will also keep track of runtime for different runs of the say_hello() function. and will plot a graph in the same directory of your python script if you set plot=True, otherwise the plot flag is by default False.

Runtime tracking using Capslock Timing Decorator

Debug Decorator

To get debug information of anyof your function, follow the bellow instruction-

from capslock import debug

@debug
def add(number1, number2):
    return number1 + number2

if __name__ == '__main__':
    print(add(20, 30))

will provide you the following output with some debug information-

Debug Information using Capslock Debug Decorator

Run Multiple Times Decorator

To run a function multiple times, use the run_multiple_times decorator from capslock package.

from datetime import datetime
from capslock import run_multiple_times

@run_multiple_times(times=10)
def current_time():
    now = datetime.now()
    return now.strftime("%H:%M:%S.%f")

if __name__ == '__main__':
    print(current_time())

will run the current time function 10 times.

Require Root previllege Decorator

If you want to prevent some accidental execution of a certain part of code that might cause system-wide changes and you wish the user to have root previllege before running that part of the python code, just use the require_root decorator from capslock.

from capslock import require_root

@require_root
def say_hello():
    for _ in range(10):
        print("Hello World")


if __name__ == '__main__':
    say_hello()

Now, say_hello() will only run, if the user is root.

Color your function output decorator

If you want to see your function output as colorful this method might help you to color your function output. At present this method support these colors BLUE, CYAN, GREEN, YELLOW, RED, BOLD, UNDERLINE

from capslock import color_output

@color_output("RED")
def hello_color():
    return "Hey! How are you!"

if __name__ == '__main__':
    hello_color()

Restrict Memory check before Memory intensive workload function execution

If you have a memory intensive workload function and you know the minimum memory requriement, you can restrict the fucntion execution by checking the available memory in that system beforehand by using this decorator.

from capslock import requires_ram

@requires_ram(8)
def memory_intensive_task():
    print("Running a memory-intensive task...")


if __name__ == '__main__':
    memory_intensive_task()

If the system has less memory than mentioned in the decorator, the function will not execute. E.g., in the above example, if the system has 8 GB or more RAM, then the function will execute.

How to Contribute

You can contribute in different ways. You can add more decorators for frequently used tasks in day to day development works.

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

capslock-2.0.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

capslock-2.0.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file capslock-2.0.1.tar.gz.

File metadata

  • Download URL: capslock-2.0.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for capslock-2.0.1.tar.gz
Algorithm Hash digest
SHA256 684168c46f3b036284fa194eaf8dfbbf40b9541bab612a283f285ffccfbbf533
MD5 dfc78c8e80432ae53eeabc75b4460f00
BLAKE2b-256 38349caa65eab5c6ba84e046f8e4078e49797b32fd6b35c0f689bdf7644fcd7d

See more details on using hashes here.

File details

Details for the file capslock-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: capslock-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for capslock-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 165164f3aa4f38d16e1f47c55176db5b5f3ecaa24615233b07f12d2ce2cd53ae
MD5 6d2014c80a2107ab24da674c1b95a79c
BLAKE2b-256 b848880e99d342eeef89e443420991b7dfb9b944394a4f9ee145a47d5241c662

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