Skip to content

Commit

Permalink
CLN/DOC: cache_readonly: remove allow_setting + preserve docstring (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Mar 6, 2018
1 parent e02f737 commit db82165
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
34 changes: 10 additions & 24 deletions pandas/_libs/properties.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,28 @@ from cpython cimport (
PyDict_Contains, PyDict_GetItem, PyDict_SetItem)


cdef class cache_readonly(object):
cdef class CachedProperty(object):

cdef readonly:
object func, name, allow_setting
object func, name, __doc__

def __init__(self, func=None, allow_setting=False):
if func is not None:
self.func = func
self.name = func.__name__
self.allow_setting = allow_setting

def __call__(self, func, doc=None):
def __init__(self, func):
self.func = func
self.name = func.__name__
return self
self.__doc__ = getattr(func, '__doc__', None)

def __get__(self, obj, typ):
# Get the cache or set a default one if needed
if obj is None:
# accessed on the class, not the instance
return self

# Get the cache or set a default one if needed
cache = getattr(obj, '_cache', None)
if cache is None:
try:
cache = obj._cache = {}
except (AttributeError):
return
return self

if PyDict_Contains(cache, self.name):
# not necessary to Py_INCREF
Expand All @@ -40,20 +37,9 @@ cdef class cache_readonly(object):
PyDict_SetItem(cache, self.name, val)
return val

def __set__(self, obj, value):

if not self.allow_setting:
raise Exception("cannot set values for [%s]" % self.name)

# Get the cache or set a default one if needed
cache = getattr(obj, '_cache', None)
if cache is None:
try:
cache = obj._cache = {}
except (AttributeError):
return
cache_readonly = CachedProperty

PyDict_SetItem(cache, self.name, value)

cdef class AxisProperty(object):
cdef:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ def _is_strictly_monotonic_decreasing(self):
def is_lexsorted_for_tuple(self, tup):
return True

@cache_readonly(allow_setting=True)
@cache_readonly
def is_unique(self):
""" return if the index has unique values """
return self._engine.is_unique
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@ def test_is_(self):
assert not ind.is_(ind.copy())
assert not ind.is_(ind.copy(deep=False))
assert not ind.is_(ind[:])
assert not ind.is_(ind.view(np.ndarray).view(Index))
assert not ind.is_(np.array(range(10)))

# quasi-implementation dependent
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

import numpy as np
from pandas import Index
from pandas._libs import lib, writers as libwriters
import pandas.util.testing as tm

Expand Down Expand Up @@ -198,3 +199,8 @@ def test_get_reverse_indexer(self):
result = lib.get_reverse_indexer(indexer, 5)
expected = np.array([4, 2, 3, 6, 7], dtype=np.int64)
tm.assert_numpy_array_equal(result, expected)


def test_cache_readonly_preserve_docstrings():
# GH18197
assert Index.hasnans.__doc__ is not None

0 comments on commit db82165

Please sign in to comment.