1

I am trying to serialize a class that has external dependency.

The way the class is created is that it receives a config in its init function, and creates an object that receives that config and assign it to self.

What I'm trying to accomplish, is that I want to serialize the class, and depending upon the context of creation, I want to be able to inject a different config.

class Foo:
  def __init__(self, some_value, config):
    self.some_value = some_value
    self.some_service = SomeService(config)

What I'd want in this scenario, is to serialzie self.some_value, but not self.some_service (and neither config, as this is changed).

So, what is the proper pattern? I've had a look at the getstate/setstate dunder, which is perfect for serializing only part of the class, but not injecting config when unpickling. I would have expected Unpickler to work perfectly in this instance, but it doesn't seem like it (and seems to work only with files for some reason? The data is serialized in a redis DB, so no file). I'd rather not have a service locator either, but have the config injected than fetched.

Clarifications : The issue is not how to use pickle or the pickler. The issue is more of a choice of pattern. I have external dependecies in the object (as denoted by self.some_service = SomeService(config)).

There are 2 ways to reconstruct that object at unpickling :

  • Use a custom Pickler/Unpickler to detect external dependency instance, serialize a hash of it, and when unpickling, give it whatever instance it requries at that moment
  • create the get/setstate dunder functions that detects the service, and does not serialize it.

Both have their pros and cons, but I'd like to know which one would be recommended. The unpickler can have the external dependecies when unpickling, and reassign it to the object when unpickling, but it seems like a 'heavy' solution. Using the get/setstate dunder requires to have the class the know how to fetch the external dependencies, and seems a bit 'magical' (the class fectching external services instead of them being given to the class).

Alex
  • 51
  • 3
  • Possible duplicate of [Pickle with custom classes](https://stackoverflow.com/questions/10842553/pickle-with-custom-classes) ([other](https://medium.com/the-python-corner/object-serialization-in-python-1d49c6ad071) good [information](https://stackabuse.com/introduction-to-the-python-pickle-module/) on the [topic](https://rushter.com/blog/pickle-serialization-internals/). And maybe [this](https://www.geeksforgeeks.org/understanding-python-pickling-example/) gives an idea of how to use this). You should also look into JSON-structures that you could dump/reload on creation to store states in vars. – Torxed Feb 25 '19 at 21:05
  • I'd argue that this question is not a duplicate (even after reading all of the information given). My problem is that I have an instance that has ref to unpickleable services. I don't/can't pickle those, but when I unpickle, the new instance needs its service ref set to those service. – Alex May 02 '19 at 14:44

1 Answers1

1

The solution I ended up with is essentially a bit like a service locator : I unpickle the instance, then I call a custom install_dependencies(injector)

Alex
  • 51
  • 3