You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

serialize.py 960 B

3 years ago
1234567891011121314151617181920212223242526272829
  1. # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
  2. import cloudpickle
  3. class PicklableWrapper(object):
  4. """
  5. Wrap an object to make it more picklable, note that it uses
  6. heavy weight serialization libraries that are slower than pickle.
  7. It's best to use it only on closures (which are usually not picklable).
  8. This is a simplified version of
  9. https://github.com/joblib/joblib/blob/master/joblib/externals/loky/cloudpickle_wrapper.py
  10. """
  11. def __init__(self, obj):
  12. self._obj = obj
  13. def __reduce__(self):
  14. s = cloudpickle.dumps(self._obj)
  15. return cloudpickle.loads, (s,)
  16. def __call__(self, *args, **kwargs):
  17. return self._obj(*args, **kwargs)
  18. def __getattr__(self, attr):
  19. # Ensure that the wrapped object can be used seamlessly as the previous object.
  20. if attr not in ["_obj"]:
  21. return getattr(self._obj, attr)
  22. return getattr(self, attr)

No Description