/usr/lib/python2.7/dist-packages/stuf/deep.py is in python-stuf 0.9.16-0ubuntu1.
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 | # -*- coding: utf-8 -*-
'''stuf deep objectry.'''
from functools import partial
from operator import attrgetter, getitem
clsdict = attrgetter('__dict__')
selfname = attrgetter('__name__')
# get class of instance.
getcls = attrgetter('__class__')
clsname = lambda this: selfname(getcls(this))
# lightweight object manipulation
hasit = lambda x, y: y in clsdict(x)
getit = lambda x, y: clsdict(x)[y]
setit = lambda x, y, z: clsdict(x).__setitem__(y, z)
delit = lambda x, y: clsdict(x).__delitem__(y)
def attr_or_item(this, key):
'''Get attribute or item `key` from `this`.'''
try:
return getitem(this, key)
except (KeyError, TypeError):
return getter(this, key)
def deepget(this, key, default=None):
'''Get deep attribut `key` on `this`, setting it to `default` if unset.'''
try:
return attrgetter(key)(this)
except AttributeError:
return default
def deleter(this, key):
'''Delete attribute `key` from `this`.'''
try:
object.__delattr__(this, key)
except (AttributeError, TypeError):
delattr(this, key)
def getter(this, key):
'''Get attribute `key` from `this`.'''
try:
return object.__getattribute__(this, key)
except (AttributeError, TypeError):
return getattr(this, key)
def members(this):
'''Iterator version of ``inspect.getmembers``.'''
getr = partial(getattr, this)
for key in dir(this):
try:
value = getr(key)
except AttributeError:
pass
else:
yield key, value
def setter(this, key, value):
'''Set attribute `key` on `this` to value and return `value`.'''
# it's an instance
try:
this.__dict__[key] = value
# it's a class
except TypeError:
setattr(this, key, value)
return value
else:
return value
def setthis(this, key, value):
'''Set attribute `key` on `this` to value and return `this`.'''
# it's an instance
try:
this.__dict__[key] = value
# it's a class
except TypeError:
setattr(this, key, value)
return this
else:
return this
def setdefault(this, key, default=None):
'''Get attribute `key` on `this`, setting it with `default` if unset.'''
try:
return getter(this, key)
except AttributeError:
return setter(this, key, default)
def setpart(this, key, method, *args):
'''
Set attribute `key` on `this` to partial method and return partial method.
'''
part = partial(method, *args)
try:
this.__dict__[key] = part
# it's a class
except TypeError:
setattr(this, key, part)
else:
return part
|