I am looking to implement a custom method in my class which helps users slice based on index. The primary slicing will be based on dictionary key. I want to implement it similar to how Pandas does it, using df.iloc[n]
here's my code:
class Vector:
def __init__(self, map_object: dict):
self.dictionary = map_object
def __getitem__(self, key):
data = self.dictionary[key]
return data
def iloc(self, n):
key = list(self.dictionary)[n]
return self.dictionary[key]
However, if then write object.iloc[3]
after creating the object, I get an error saying 'method' object is not subscriptable
. So how can I implement this?
CodePudding user response:
The [
]
syntax requires a proper object with a __getitem__
method. In order to have a "slice method", use a property
that returns a helper which supports slicing.
The helper simply holds a reference to the actual parent
object, and defines a __getitem__
with the desired behaviour:
class VectorIloc:
def __init__(self, parent):
self.parent = parent
# custom logic for desired "iloc" behaviour
def __getitem__(self, item):
key = list(self.parent.dictionary)[item]
return self.parent[key]
On the actual class, merely define the desired "method" as a property
that returns the helper or as an attribute:
class Vector:
def __init__(self, map_object: dict):
self.dictionary = map_object
# if .iloc is used often
# self.iloc = VectorIloc(self)
def __getitem__(self, key):
return self.dictionary[key]
# if .iloc is used rarely
@property
def iloc(self):
return VectorIloc(self)
Whether to use a property
or an attribute is an optimisation that trades memory for performance: an attribute constructs and stores the helper always, while a property
constructs it only on-demand but on each access. A functools.cached_property
can be used as a middle-ground, creating the attribute on first access.
The property
is advantageous when the helper is used rarely per object, and especially if it often is not used at all.
Now, when calling vector.iloc[3]
, the vector.iloc
part provides the helper and the [3]
part invoces the helper's __getitem__
.
>>> vector = Vector({0:0, 1: 1, 2: 2, "three": 3})
>>> vector.iloc[3]
3
CodePudding user response:
I was looking for this implementation which I'm pretty used to in Pandas. However, after searching a lot, I could not find any suitable answer. So I went looking through the Pandas source code and found that the primary requirement for implementing this are as follows:
- Create the method with
@property
decorator, so that it accepts the slice object without throwing the above error - Create a second class to slice based on the index, pass self to this class, and return this class from the method
My final code ended up looking something like this:
class TimeSeries:
def __init__(self, data: dict):
self.data = data
def __getitem__(self, key):
data = self.data[key]
return data
@property
def iloc(self):
return Slicer(self)
class Slicer:
def __init__(self, obj):
self.time_series = obj
def __getitem__(self, n):
key = list(self.time_series.data)[n]
return self.time_series[key]
With the classes defined this way, I could write the following code:
>>> ts = TimeSeries({'a': 1, 'b': 2, 'c': 3, 'd': 4})
>>> print("value of a:", ts['a'])
value of a: 1
>>> print("value at position 0:", ts.iloc[0])
value at position 0: 1