seequent / properties

An organizational aid and wrapper for validation and tab completion of class properties/traits.
http://propertiespy.rtfd.org
MIT License
18 stars 9 forks source link

Coerce on Instance Property? #285

Closed lheagy closed 5 years ago

lheagy commented 5 years ago

Would including a coerce on an Instance property be of interest? I have an example where I am creating a List property of Array values, and would like to allow the user to simply input a list of numpy arrays, and we cast them to properties.Array. Right now, doing so causes a validation error.

locations = properties.List(
    "list of locations of each electrode in a dipole receiver",
    properties.Instance("electrode locations", properties.Array),
    min_length=1, max_length=2
)

The only mechanism from the outside that I would see to remedy this is to subclass properties.Instance? (other ideas appreciated!), which seems like a bit of overkill.

Would something like the following seem suitable?

locations = properties.List(
    "list of locations of each electrode in a dipole receiver",
    properties.Instance("electrode locations", properties.Array, coerce=True),
    min_length=1, max_length=2
)
fwkoch commented 5 years ago

Hey @lheagy - I think this might be slightly off track. An Instance of properties.Array will expect it to be a Property instance, not actually an array. I think what you are looking for is something like:

class HasLocations(properties.HasProperties):

    locations = properties.List(
        'List of arrays',
        properties.Array(''),
        min_length=1,
        max_length=2,
    )

Then you can do this: image

properties.List has a coerce attribute that gives even more flexibility, like this: image

That totally bypasses Instance and sounds like it fits your use case? If this isn't quite what you are looking for, please follow up! There is a bit more you can do with Instance if necessary, but it would probably look more like properties.Instance('', NumpyArraySubclass) or something...

lheagy commented 5 years ago

Many thanks for the clarification @fwkoch!! This is what I needed :)