deep.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. '''stuf deep objectry.'''
  3. from functools import partial
  4. from operator import attrgetter, getitem
  5. clsdict = attrgetter('__dict__')
  6. selfname = attrgetter('__name__')
  7. # get class of instance.
  8. getcls = attrgetter('__class__')
  9. clsname = lambda this: selfname(getcls(this))
  10. # lightweight object manipulation
  11. hasit = lambda x, y: y in clsdict(x)
  12. getit = lambda x, y: clsdict(x)[y]
  13. setit = lambda x, y, z: clsdict(x).__setitem__(y, z)
  14. delit = lambda x, y: clsdict(x).__delitem__(y)
  15. def attr_or_item(this, key):
  16. '''Get attribute or item `key` from `this`.'''
  17. try:
  18. return getitem(this, key)
  19. except (KeyError, TypeError):
  20. return getter(this, key)
  21. def deepget(this, key, default=None):
  22. '''Get deep attribut `key` on `this`, setting it to `default` if unset.'''
  23. try:
  24. return attrgetter(key)(this)
  25. except AttributeError:
  26. return default
  27. def deleter(this, key):
  28. '''Delete attribute `key` from `this`.'''
  29. try:
  30. object.__delattr__(this, key)
  31. except (AttributeError, TypeError):
  32. delattr(this, key)
  33. def getter(this, key):
  34. '''Get attribute `key` from `this`.'''
  35. try:
  36. return object.__getattribute__(this, key)
  37. except (AttributeError, TypeError):
  38. return getattr(this, key)
  39. def members(this):
  40. '''Iterator version of ``inspect.getmembers``.'''
  41. getr = partial(getattr, this)
  42. for key in dir(this):
  43. try:
  44. value = getr(key)
  45. except AttributeError:
  46. pass
  47. else:
  48. yield key, value
  49. def setter(this, key, value):
  50. '''Set attribute `key` on `this` to value and return `value`.'''
  51. # it's an instance
  52. try:
  53. this.__dict__[key] = value
  54. # it's a class
  55. except TypeError:
  56. setattr(this, key, value)
  57. return value
  58. else:
  59. return value
  60. def setthis(this, key, value):
  61. '''Set attribute `key` on `this` to value and return `this`.'''
  62. # it's an instance
  63. try:
  64. this.__dict__[key] = value
  65. # it's a class
  66. except TypeError:
  67. setattr(this, key, value)
  68. return this
  69. else:
  70. return this
  71. def setdefault(this, key, default=None):
  72. '''Get attribute `key` on `this`, setting it with `default` if unset.'''
  73. try:
  74. return getter(this, key)
  75. except AttributeError:
  76. return setter(this, key, default)
  77. def setpart(this, key, method, *args):
  78. '''
  79. Set attribute `key` on `this` to partial method and return partial method.
  80. '''
  81. part = partial(method, *args)
  82. try:
  83. this.__dict__[key] = part
  84. # it's a class
  85. except TypeError:
  86. setattr(this, key, part)
  87. else:
  88. return part