Skip to main content

Handy decorators for day-to-day use!

Project description

Build Status

Handy Decorators


This is a set of handy decorators which one can use for their day-to-day life coding.

Installation Method

Install it via pip

pip install handy-decorators

Description

The set of decorators contain some daily needed decorators for being used in our day to day coding life. This has following set of decorators.

trycatch

This decorator surounds your function with a try-except block and if your code/function raises an exception, it's caught by this decorator and reported by logging.

>>> from decorators import trycatch
>>> @trycatch
... def func():
...     print(0/0) # Division by 0 must raise exception
...
>>> func()
Exception occurred: [integer division or modulo by zero]
>>>

timer

This decorator will calculate a time required in seconds by your function for execution.

>>> from decorators import timer
>>> @timer
... def a():
...     import time
...     print('Hi')
...     time.sleep(1)
...
>>> a()
Hi
Time taken by the function is [1.00103902817] sec
>>>

singleton

This decorator is for making your class singleton.
The features given by this decorator are:

  • If instances of same class are created with same args and kwargs, decorator will return previously existing instance
  • If instances of same class are created with different args and kwargs, decorator will create a different one for you and store the newly created instance
>>> from decorators import singleton
>>>
>>> @singleton
... class A:
...     def __init__(self, *args, **kwargs):
...         pass
...
>>>
>>> a = A(name='Siddhesh')
>>> b = A(name='Siddhesh', lname='Sathe')
>>> c = A(name='Siddhesh', lname='Sathe')
>>> a is b  # has to be different
False
>>> b is c  # has to be same
True
>>>

Please create an issue if more decorators are needed.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

handy_decorators-0.0.7-py2-none-any.whl (15.4 kB view hashes)

Uploaded Python 2

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