Box icon indicating copy to clipboard operation
Box copied to clipboard

Box recast meaning

Open ipcoder opened this issue 5 years ago • 4 comments

Don't you think we can expand the meaning of the box_recast and consider it as a more generic mapping operation, that is

Box(..., box_recast={'item': callable, ...})

where the callable could be a type or any other callable object returning a value? It seems nothing prevents such interpretation already, right?

Thanks

ipcoder avatar May 18 '20 10:05 ipcoder

Can you give me an example of how that would be used so I understand what you want to accomplish with it?

cdgriffith avatar Jul 03 '20 15:07 cdgriffith

First I would like to stress that may remark/suggestion is about widening the meaning - not changing the code. The docs assume its only type casting can be used, but actually any function can be provided, "normalizing" the data in any required sense, for example:

Box(..., box_recast={'name': lambda x: x.title(), ...})

So, technically, one can use that already now, right? Its just that documentation seems to imply that only type casting can be used,

ipcoder avatar Jul 03 '20 15:07 ipcoder

I have updated to 5.0.1 and this extended behavior is not working anymore. It seems now there is an explicit assumption casting is to a class (if issubclass(... in box.py:407):

    def __recast(self, item, value):
        if self._box_config["box_recast"] and item in self._box_config["box_recast"]:
            try:
                if issubclass(self._box_config["box_recast"][item], (Box, box.BoxList)):

Is that your design decision to forbid extended interpretation of recasting as discussed above?

ipcoder avatar Jul 15 '20 10:07 ipcoder

That was actually added to fix another issue being experienced, and of course didn't have the above test case to see how that would affect that ability.

I will look into if it is possible to have it work as needed as it currently stands, as well as the above code, or if they would need to be separate things.

cdgriffith avatar Jul 15 '20 14:07 cdgriffith