Point72 / csp

csp is a high performance reactive stream processing library, written in C++ and Python
https://github.com/Point72/csp/wiki
Apache License 2.0
191 stars 33 forks source link

Add postprocess_to_dict hook for to_dict method in structs #280

Closed arhamchopra closed 2 months ago

arhamchopra commented 3 months ago

This PR adds postprocess_to_dict hook to the structs. This hook is invoked by the to_dict method on the dict obtained from parsing a struct. It will allows users to customize the final dict generated by the to_dict method for each struct.

class A(Struct):
    replace_me: str
    data: int = 0
    def postprocess_to_dict(self, obj_dict):
        obj_dict['replace_me'] = 'replace_me'
        obj_dict['postprocess_invoked'] = True
        return obj_dict

a = A(replace_me='Dummy', data=1)
a.to_dict() == {'replace_me': 'replace_me', 'data': 1, 'postprocess_invoked': True}
AdamGlustein commented 3 months ago

Note that the function was removed in https://github.com/Point72/csp/pull/258 as no internal csp code called it and it was meant to be an internal function.

However, an external user reported that they were using the feature and this change broke their pipeline, so we're adding it back in.

arhamchopra commented 3 months ago

Note that the function was removed in #258 as no internal csp code called it and it was meant to be an internal function.

However, an external user reported that they were using the feature and this change broke their pipeline, so we're adding it back in.

Note the difference in the name postprocess_to_dict versus _postprocess_dict_to_python (in #258). This method is now being exposed as a public hook so that users can override this method if needed.