cdgriffith / Box

Python dictionaries with advanced dot notation access
https://github.com/cdgriffith/Box/wiki
MIT License
2.61k stars 106 forks source link

Box recast meaning #153

Open ipcoder opened 4 years ago

ipcoder commented 4 years ago

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

cdgriffith commented 4 years ago

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

ipcoder commented 4 years ago

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 commented 4 years ago

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?

cdgriffith commented 4 years ago

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.