Skip to main content

A collection of helper functions and decorators that I occasionally use to get my life in order.

Project description

A collection of helper functions and decorators that I occasionally use to get my life in order.


pip install mj_helpers


A decorator to profile function calls.


from mj_helpers.decorators import profileit

def foo():
    return do_stuff()

In a Python shell:

>>> from .foo import foo
>>> foo()
>>> from pstats import Stats
>>> stats = Stats('/tmp/foo.profile')
>>> stats.sort_stats('cumulative').print_stats(50)

Fri Nov 27 08:34:14 2015    /tmp/foo.profile
2 function calls in 0.000 seconds

Ordered by: cumulative time
ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    0.000    0.000 <ipython-input-8-8f35865ca12d>:1(foo)
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

If installed via pip or python

mj_stats /tmp/foo.profile /tmp/bar.profile --sorting=cumulative --limit=50


Logging decorator that logs function name, args and kwargs.

In Python:

from mj_helpers.log_decorator import log_function_io

def foo(bar, spam=None, *args, **kwargs):
    return do_stuff()

class Thing(object):

class Bar(object):

    def static_method(spam, eggs='eggs'):
        return True

    def class_method(cls, foo='foo', bar=None):
        return False

    def function(self, thing):
        return Thing()


>>> foo('a', *['first star', 'second star'], **{'something': 'boo'})
2016-02-02 05:04:50,963 - __main__ - DEBUG - [FUN] foo [ARG] bar: 'a', spam: 'first star', something: 'boo' *('second star',)
2016-02-02 05:04:50,963 - __main__ - DEBUG - [FUN] foo [RET] None

>>> Bar.static_method('spam')
2016-02-02 05:09:30,426 - __main__ - DEBUG - [FUN] static_method [ARG] spam: 'spam'
2016-02-02 05:09:30,426 - __main__ - DEBUG - [FUN] static_method [RET] True

>>> Bar.class_method(bar='spam')
2016-02-02 05:11:39,753 - __main__ - DEBUG - [FUN] class_method [ARG] bar: 'spam'
2016-02-02 05:11:39,753 - __main__ - DEBUG - [FUN] class_method [RET] False

>>> Bar().function(thing='spam')
2016-02-02 05:13:01,679 - __main__ - DEBUG - [FUN] function [ARG] thing: 'spam'
2016-02-02 05:13:01,679 - __main__ - DEBUG - [FUN] function [RET] <__main__.Thing object at 0x7f33d8627f90>


Caching decorator to assist in caching function calls / returns.

In Python:

from mj_helpers.decorators import cache_it

def foo(bar, spam=None, *args, **kwargs):
    return do_stuff()

# If using memoize instead of django's cache you can see the cache by:
>>> cache_it._cache
    <function foo at 0x7f4419daa848>: {
        "(('a', 'first star', 'second star'), (('something', 'boo'),))":
        (None, 1454391947.614329)

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

mj_helpers-0.0.1.tar.gz (7.0 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page