Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

immutable state #20

Open
jamesdbrock opened this issue Feb 1, 2023 · 2 comments
Open

immutable state #20

jamesdbrock opened this issue Feb 1, 2023 · 2 comments

Comments

@jamesdbrock
Copy link
Collaborator

I want to drop some notes here in this issue about immutable state.

When we call set_state we want to pass in a new state, which means creating a new immutable state object which possibly re-uses some of the immutable state from the old state object. Here are some patterns I developed for doing that. I don't know if this is the most Pythonic way of doing immutable data structures but this is what I came up with. The tricky part is doing shallow copies of class object properties which are lists.

class StatePersist:
    """
    State which should persist after the app closes and opens.
    """
    def __init__(self):
        self.modelpath : str = ""
        self.imagepath : str = ""
        self.sensitivity : int = 50 
        self.pixelsmillimeter : int = 50.0
        self.filters: list[StateFilterPersist] = []

    def __copy__(self):
        cls = self.__class__
        result = cls.__new__(cls)
        result.__dict__.update(self.__dict__)
        result.filters = self.filters[:]
        return result
        """
        When we shallow copy, we also want to make a shallow copy of the member lists.

        https://docs.python.org/3/library/copy.html

        https://stackoverflow.com/questions/1500718/how-to-override-the-copy-deepcopy-operations-for-a-python-object
        """


def handle_filteradd(self, event=None):
        state = copy.copy(self.state)
        state.filters.append(StateFilter())
        state.persist = copy.copy(self.state.persist)
        state.persist.filters.append(StateFilterPersist())
        state.discover = discover_state(state.persist, self.state.discover)
        match self.state.infer:
            case StateInferred():
                state.infer = copy.copy(self.state.infer)
                state.infer.filters.append(None)
        self.persist_state_save(state.persist)
        self.set_state(state)
@jamesdbrock
Copy link
Collaborator Author

Maybe stuff in this post would be helpful for re-wx idioms https://medium.datadriveninvestor.com/exploring-the-best-functional-programming-modules-in-python-74489b67c4c4

@jamesdbrock
Copy link
Collaborator Author

FUNCTIONAL PYTHON, PART I: TYPOPÆDIA PYTHONICA

https://www.tweag.io/blog/2022-09-08-fp1-typopaedia-pythonica/

FUNCTIONAL PYTHON, PART II: DIAL M FOR MONOID

https://www.tweag.io/blog/2023-01-19-fp2-dial-m-for-monoid/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant