Completed
Push — dev-4.1 ( c2f7a9...db8c57 )
by Felipe A.
01:12
created

wrapped()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
1
2
import functools
3
4
5
NOT_DEFINED = type('NotDefinedType', (object,), {})
6
7
8
def psetattr(obj, name, value=NOT_DEFINED):
9
    '''
10
    Sets given value to given object, in a deferred way. Thought as a setattr
11
    partial which returns value instead of None.
12
13
    This function also works as decorator.
14
15
    :param obj: object which attr will be set
16
    :type obj: object
17
    :param name: attribute name will be set
18
    :type name: str
19
    :param value: attribute value will be set on `obj` with name `name`
20
    :type value: any
21
22
    :return: value given as `value` param
23
    :rtype: any
24
    '''
25
    def wrapped(value):
26
        setattr(obj, name, value)
27
        return value
28
    return wrapped if value is NOT_DEFINED else wrapped(value)
29