dalejung / pandas-composition

Pandas Composition/Inheritance
MIT License
9 stars 0 forks source link

Infinite loop with subclasses overridding __getitem__ #5

Closed dalejung closed 11 years ago

dalejung commented 11 years ago
class InfiniteFrame(UserFrame):
    counts = {} 
    log = []
    def __getitem__(self, name):
        InfiniteFrame.log.append(name)
        count = InfiniteFrame.counts.setdefault(name, 0)
        count += 1 
        InfiniteFrame.counts[name] = count
        # if not for this count check, we'd go into an infinite loop
        if count > 5:
            raise AttributeError('error through exaustion')
        return self.ix[:2] 

idf = InfiniteFrame(np.random.randn(10, 10))
cols = idf.columns
counts = InfiniteFrame.counts

print counts
# {'columns': 1, 'ix': 6}
print InfiniteFrame.log
# ['columns', 'ix', 'ix', 'ix', 'ix', 'ix', 'ix']

Due to the DataSet changes that were merged in __tr_getattr__ calls __getitem__ which can run into an infinite loop with subclasses that override __getitem__.

If it weren't for the explicit loop check, this would run infinitely. If the overridding __getitem__ doesn't properly raise AttributeError for things like ix then this will run infinitely. Even though it the __getitem__ would have worked if the getattr -> getitem logic wasn't there.

dalejung commented 11 years ago

bc2b10c2ea6c87d15f5bc81e0ba0c7132c13e492