/usr/lib/python2.7/dist-packages/easydev/decorators.py is in python-easydev 0.9.35+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | # -*- python -*-
# -*- coding: utf-8 -*-
#
# This file is part of the easydev software
#
# Copyright (c) 2012-2014 -
#
# File author(s): Thomas Cokelaer (cokelaer, gmail.com)
#
# Distributed under the GPLv3 License.
# See accompanying file LICENSE.txt or copy at
# http://www.gnu.org/licenses/gpl-3.0.html
#
# Website: https://github.com/cokelaer/easydev
# website: http://github.com/cokelaer/easydev
#
##############################################################################
"""Handy decorators"""
from functools import wraps
import threading
__all__ = ['ifpylab', 'requires', 'ifpandas']
# decorator with arguments and optional arguments for a method
def _require(*args_deco, **kwds_deco):
"""Decorator for class method to check if an attribute exist
.. doctest::
from easydev.decorators import require
class Test(object):
def __init__(self):
self.m = 1
@require('m', "set the m attribute first")
def print(self):
print self.m
t = Test()
t.print()
.. todo:: first argument could be a list
"""
if len(args_deco) != 2:
raise ValueError("require decorator expects 2 parameter. First one is" +
"the required attribute. Second one is an error message.")
attribute = args_deco[0]
msg = args_deco[1]
if len(attribute.split('.')) > 2:
raise AttributeError('This version of require decorator introspect only 2 levels')
def decorator(func):
# func: function object of decorated method; has
# useful info like f.func_name for the name of
# the decorated method.
def newf(*args, **kwds):
# This code will be executed in lieu of the
# method you've decorated. You can call the
# decorated method via f(_args, _kwds).
names = attribute.split('.')
if len(names) == 1:
if hasattr(args[0], attribute):
return func(*args, **kwds)
else:
raise AttributeError('%s not found. %s' % (names, msg))
elif len(names) == 2:
if hasattr(getattr(args[0], names[0]), names[1]):
return func(*args, **kwds)
else:
raise AttributeError('%s not found. %s' % (names, msg))
newf.__name__ = func.__name__
newf.__doc__ = func.__doc__
return newf
return decorator
# for book keeping, could be useful:
"""
def _blocking(not_avail):
def blocking(f, *args, **kw):
if not hasattr(f, "thread"):
# no thread running
def set_result():
f.result = f(*args, **kw)
f.thread = threading.Thread(None, set_result)
f.thread.start()
return not_avail
elif f.thread.isAlive():
return not_avail
else:
# the thread is ended, return the stored result
del f.thread
return f.result
return blocking
"""
# same as require decorator but works with list of stirngs of
# single string and uses the functools utilities
def requires(requires, msg=""):
"""Decorator for class method to check if an attribute exist
.. doctest::
>>> from easydev.decorators import requires
>>> class Test(object):
... def __init__(self):
... self.m = 1
... self.x = 1
... @requires(['m','x'], "set the m attribute first")
... def printthis(self):
... print(self.m+self.x)
>>> t = Test()
>>> t.printthis()
2
"""
if isinstance(requires, str):
requires = [requires]
elif isinstance(requires, list) == False:
raise TypeError("First argument of the /requires/ decorator must be a" +
"string or list of string representing the required attributes" +
"to be found in your class. Second argument is a " +
"complementary message. ")
def actualDecorator(f):
@wraps(f)
def wrapper(*args, **kwds):
for require in requires:
if hasattr(args[0], require) == False:
raise AttributeError("{} not found in {}. ".format(require, args[0]) + msg)
return f(*args, **kwds)
return wrapper
return actualDecorator
# could be a macro maybe
def ifpandas(func):
"""check if pandas is available. If so, just return
the function, otherwise returns dumming function
that does nothing
"""
def wrapper(*args, **kwds):
return func(*args, **kwds)
try:
import pandas
return wrapper
except Exception:
def dummy():
pass
return dummy
def ifpylab(func):
"""check if pylab is available. If so, just return
the function, otherwise returns dumming function
that does nothing
"""
# for functions
def wrapper(*args, **kwds):
return func(*args, **kwds)
# for methods
try:
import pylab
return wrapper
except Exception:
def dummy():
pass
return dummy
|