Skip to content

Separate properties module #17590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import tslib
from tslib import NaT, Timestamp, Timedelta
import interval
from interval import Interval
from properties import AxisProperty, cache_readonly # noqa
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may be able to remove these imports entity and just change where they r called from

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. It looks like the only direct import of lib.cache_readonly is by util._decorators, and other imports get it from there. So that would only require changing it in one place. And lib.AxisProperty is only used in core.generic, so also an easy change. Pls confirm before I make this new change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep jose would be good


cdef int64_t NPY_NAT = util.get_nat()

Expand Down Expand Up @@ -1907,5 +1908,4 @@ cdef class BlockPlacement:


include "reduce.pyx"
include "properties.pyx"
include "inference.pyx"
69 changes: 69 additions & 0 deletions pandas/_libs/properties.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

from cython cimport Py_ssize_t

from cpython cimport (
PyDict_Contains, PyDict_GetItem, PyDict_SetItem)


cdef class cache_readonly(object):

cdef readonly:
object func, name, allow_setting

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):
self.func = func
self.name = func.__name__
return self

def __get__(self, obj, typ):
# 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

if PyDict_Contains(cache, self.name):
# not necessary to Py_INCREF
val = <object> PyDict_GetItem(cache, self.name)
else:
val = self.func(obj)
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

PyDict_SetItem(cache, self.name, value)

cdef class AxisProperty(object):
cdef:
Py_ssize_t axis

def __init__(self, axis=0):
self.axis = axis

def __get__(self, obj, type):
cdef list axes = obj._data.axes
return axes[self.axis]

def __set__(self, obj, value):
obj._set_axis(self.axis, value)
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def get_tag(self):
cmdclass['build_src'] = DummyBuildSrc
cmdclass['build_ext'] = CheckingBuildExt

lib_depends = ['reduce', 'inference', 'properties']
lib_depends = ['reduce', 'inference']


def srcpath(name=None, suffix='.pyx', subdir='src'):
Expand Down Expand Up @@ -478,6 +478,7 @@ def pxd(name):
ext_data = {
'_libs.lib': {'pyxfile': '_libs/lib',
'depends': lib_depends + tseries_depends},
'_libs.properties': {'pyxfile': '_libs/properties', 'include': []},
'_libs.hashtable': {'pyxfile': '_libs/hashtable',
'pxdfiles': ['_libs/hashtable'],
'depends': (['pandas/_libs/src/klib/khash_python.h']
Expand Down