Advertisement
Guest User

Untitled

a guest
Sep 13th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. 1)
  2. import pickle
  3.  
  4. class Parent:
  5. def __init__(self, serializable_data, third_party_object):
  6. self.serializable_data = serializable_data
  7. self.third_party_object = third_party_object # Object from third-party library
  8.  
  9. def __getstate__(self):
  10. state = self.__dict__.copy()
  11.  
  12. # Handle third-party object serialization
  13. if 'third_party_object' in state:
  14. try:
  15. # Attempt to pickle the third-party object (or specific parts of it)
  16. pickle.dumps(state['third_party_object'])
  17. except pickle.PicklingError:
  18. # If it fails, replace it with a placeholder or None
  19. state['third_party_object'] = None
  20. return state
  21.  
  22. def __setstate__(self, state):
  23. self.__dict__.update(state)
  24.  
  25. # Optionally reinitialize the third-party object if needed
  26. if self.third_party_object is None:
  27. self.third_party_object = "Reinitialize or set to a default value"
  28.  
  29. # Example usage:
  30.  
  31. class ThirdPartyObject:
  32. def __init__(self):
  33. self.serializable_part = "This is serializable"
  34. self.non_serializable_part = open("file.txt", "w") # This is not serializable
  35.  
  36. # Create an instance
  37. third_party_instance = ThirdPartyObject()
  38. parent_instance = Parent(serializable_data="Serializable data", third_party_object=third_party_instance)
  39.  
  40. # Pickle the parent object
  41. pickled_data = pickle.dumps(parent_instance)
  42.  
  43. # Unpickle the parent object
  44. unpickled_obj = pickle.loads(pickled_data)
  45.  
  46. print(unpickled_obj.serializable_data) # Will be restored
  47. print(unpickled_obj.third_party_object) # This will be None or reinitialized
  48.  
  49. 2)
  50. import pickle
  51. import weakref
  52.  
  53. class Parent:
  54. def __init__(self, serializable_data, third_party_object):
  55. self.serializable_data = serializable_data
  56. # Store a weak reference to the third-party object if it's not serializable
  57. self.third_party_object = weakref.ref(third_party_object)
  58.  
  59. def __getstate__(self):
  60. state = self.__dict__.copy()
  61. # We don't pickle weak references, so set it to None or leave it out
  62. state['third_party_object'] = None
  63. return state
  64.  
  65. def __setstate__(self, state):
  66. self.__dict__.update(state)
  67. # Reinitialize or re-fetch the third-party object as necessary
  68. self.third_party_object = None
  69.  
  70. # Example usage as above, but storing weak references
  71.  
  72. 3)
  73. import copyreg
  74.  
  75. # Define how to serialize/deserialize third-party objects
  76. def pickle_third_party_object(obj):
  77. # You can return just the serializable parts of the object
  78. return ThirdPartyObject, () # Returning constructor and arguments to re-create it
  79.  
  80. # Register the function with copyreg
  81. copyreg.pickle(ThirdPartyObject, pickle_third_party_object)
  82.  
  83. # Now when you pickle, the registered function will be used to handle ThirdPartyObject
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement