Skip to content

Use indexed property to replace 1D numpy array #3

@Naikless

Description

@Naikless

Hi,

I stumbled upon your package and wondered whether it might serve my needs.

I would like to create a property that can be sliced and indexed like a 1D numpy array. This can be done using your IndexedProperty class.

import numpy as np
import indexedproperty as idx

class Foo:
    
    def __init__(self):
        self._bar = np.array(range(10))

    @idx.indexedproperty
    def bar(self,key):
        return self._bar[key]

foo = Foo()

foo.bar[2:7:3]
>>> array([2, 5])

However, when simply executing foo.bar, the actual indexedproperty object is returned instead of the full contents of foo._bar as would have been expected if foo.bar was actually a numpy array.

Is there any other solution to this than the obvious foo.bar[:]? I have looked into you source code quite a bit but couldn't come up with a solution, since IndexedProperty.__get__() is always executed before any slicing and thus can't know whether a slice is provided or not.

Background: In my use case, _bar is a very large array that is read only on demand. So I was hoping to be able to use indexed properties to reduce memory usage in cases where I only need slices of _bar. However, the above restriction would then force me to change all occurrences of foo.bar to foo.bar[:] in legacy code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions