|
5 | 5 | that can be mixed into or pinned onto other pandas classes.
|
6 | 6 |
|
7 | 7 | """
|
| 8 | +from pandas.core.common import AbstractMethodError |
8 | 9 |
|
9 | 10 |
|
10 | 11 | class DirNamesMixin(object):
|
@@ -33,3 +34,97 @@ def __dir__(self):
|
33 | 34 | rv = set(dir(type(self)))
|
34 | 35 | rv = (rv - self._dir_deletions()) | self._dir_additions()
|
35 | 36 | return sorted(rv)
|
| 37 | + |
| 38 | + |
| 39 | +class AccessorProperty(object): |
| 40 | + """Descriptor for implementing accessor properties like Series.str |
| 41 | + """ |
| 42 | + |
| 43 | + def __init__(self, accessor_cls, construct_accessor=None): |
| 44 | + self.accessor_cls = accessor_cls |
| 45 | + self.construct_accessor = (construct_accessor or |
| 46 | + accessor_cls._make_accessor) |
| 47 | + self.__doc__ = accessor_cls.__doc__ |
| 48 | + |
| 49 | + def __get__(self, instance, owner=None): |
| 50 | + if instance is None: |
| 51 | + # this ensures that Series.str.<method> is well defined |
| 52 | + return self.accessor_cls |
| 53 | + return self.construct_accessor(instance) |
| 54 | + |
| 55 | + def __set__(self, instance, value): |
| 56 | + raise AttributeError("can't set attribute") |
| 57 | + |
| 58 | + def __delete__(self, instance): |
| 59 | + raise AttributeError("can't delete attribute") |
| 60 | + |
| 61 | + |
| 62 | +class PandasDelegate(object): |
| 63 | + """ an abstract base class for delegating methods/properties """ |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def _make_accessor(cls, data): |
| 67 | + raise AbstractMethodError("_make_accessor should be implemented" |
| 68 | + "by subclass and return an instance" |
| 69 | + "of `cls`.") |
| 70 | + |
| 71 | + def _delegate_property_get(self, name, *args, **kwargs): |
| 72 | + raise TypeError("You cannot access the " |
| 73 | + "property {name}".format(name=name)) |
| 74 | + |
| 75 | + def _delegate_property_set(self, name, value, *args, **kwargs): |
| 76 | + raise TypeError("The property {name} cannot be set".format(name=name)) |
| 77 | + |
| 78 | + def _delegate_method(self, name, *args, **kwargs): |
| 79 | + raise TypeError("You cannot call method {name}".format(name=name)) |
| 80 | + |
| 81 | + @classmethod |
| 82 | + def _add_delegate_accessors(cls, delegate, accessors, typ, |
| 83 | + overwrite=False): |
| 84 | + """ |
| 85 | + add accessors to cls from the delegate class |
| 86 | +
|
| 87 | + Parameters |
| 88 | + ---------- |
| 89 | + cls : the class to add the methods/properties to |
| 90 | + delegate : the class to get methods/properties & doc-strings |
| 91 | + acccessors : string list of accessors to add |
| 92 | + typ : 'property' or 'method' |
| 93 | + overwrite : boolean, default False |
| 94 | + overwrite the method/property in the target class if it exists |
| 95 | + """ |
| 96 | + |
| 97 | + def _create_delegator_property(name): |
| 98 | + |
| 99 | + def _getter(self): |
| 100 | + return self._delegate_property_get(name) |
| 101 | + |
| 102 | + def _setter(self, new_values): |
| 103 | + return self._delegate_property_set(name, new_values) |
| 104 | + |
| 105 | + _getter.__name__ = name |
| 106 | + _setter.__name__ = name |
| 107 | + |
| 108 | + return property(fget=_getter, fset=_setter, |
| 109 | + doc=getattr(delegate, name).__doc__) |
| 110 | + |
| 111 | + def _create_delegator_method(name): |
| 112 | + |
| 113 | + def f(self, *args, **kwargs): |
| 114 | + return self._delegate_method(name, *args, **kwargs) |
| 115 | + |
| 116 | + f.__name__ = name |
| 117 | + f.__doc__ = getattr(delegate, name).__doc__ |
| 118 | + |
| 119 | + return f |
| 120 | + |
| 121 | + for name in accessors: |
| 122 | + |
| 123 | + if typ == 'property': |
| 124 | + f = _create_delegator_property(name) |
| 125 | + else: |
| 126 | + f = _create_delegator_method(name) |
| 127 | + |
| 128 | + # don't overwrite existing methods/properties |
| 129 | + if overwrite or not hasattr(cls, name): |
| 130 | + setattr(cls, name, f) |
0 commit comments