You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
classStatePersist:
""" State which should persist after the app closes and opens. """def__init__(self):
self.modelpath : str=""self.imagepath : str=""self.sensitivity : int=50self.pixelsmillimeter : int=50.0self.filters: list[StateFilterPersist] = []
def__copy__(self):
cls=self.__class__result=cls.__new__(cls)
result.__dict__.update(self.__dict__)
result.filters=self.filters[:]
returnresult""" 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 """defhandle_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)
matchself.state.infer:
caseStateInferred():
state.infer=copy.copy(self.state.infer)
state.infer.filters.append(None)
self.persist_state_save(state.persist)
self.set_state(state)
The text was updated successfully, but these errors were encountered:
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.The text was updated successfully, but these errors were encountered: