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

Support for pickle and copy #94

Closed
NeilGirdhar opened this issue Sep 26, 2015 · 5 comments
Closed

Support for pickle and copy #94

NeilGirdhar opened this issue Sep 26, 2015 · 5 comments

Comments

@NeilGirdhar
Copy link

Has there been any thought about supporting Python pickling and copying? In other words, implementing __setstate__ and __getstate__ on the Instance type?

I did something like this:

class HasTraits:

    def __getstate__(self, **kwargs):
        state = super().__getstate__(**kwargs)

        # Create entries for traits that want to be pickled.
        for name, trait in self.class_traits(pickled=True).items():
            state[name] = trait.getstate(self, **kwargs)

        return state

    def __setstate__(self, state):
        # Set traits that were pickled.
        for name, trait in self.class_traits(pickled=True).items():
            value = state[name]
            trait.setstate(self, value)

        super().__setstate__(state)


class Instance(ClassBasedTraitType):

    def getstate(self, **kwargs):
        return trait.get(self, None)

    def setstate(self, obj, value):
        trait.set(self, value)

after which of course pickle and copy operations save and reload the state of traits automatically.

@rmorshea
Copy link
Contributor

I don't have my computer available so I can't test it, but it seems like pickling for this is already supported since the trait values are stored in _trait_values.

@NeilGirdhar
Copy link
Author

@rmorshea Oh of course. If a subclass overrides setstate and getstate, they will have to pickle _trait_values themselves. Is that the approach you prefer?

@rmorshea
Copy link
Contributor

@NeilGirdhar deleted old comment (couldn't check to see if __dict__ overrided descriptors since my computer's still out of commission). In any case though, going down the route of your example, you'd have to manage pickleability both on state[name] and state['_trait_values'][name] which seems redundant.

However, checking for getstate and setstate methods that control the values in state['_trait_values'] might be something to consider.

@rmorshea
Copy link
Contributor

@SylvainCorlay @jasongrout
Would it be worth including getstate(self, obj) and setstate(self, obj, value) methods for TraitTypes whose values are potentially unpicklable that get called in __getstate__ and __setstate__?

@rmorshea
Copy link
Contributor

rmorshea commented May 8, 2021

I think we can close this until someone else requests TraitType specific get/setstate methods

@rmorshea rmorshea closed this as completed May 8, 2021
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

2 participants