class_docstr: Extends the functionality of dill to serialize arbitrary objects.
  
  Originally intended to serialize the anomaly detector class
  `~greykite.detection.anomaly_detector.AnomalyDetector`, but
  can be potentially used to serialize other objects.
  
  Outputs a dictionary that can be serialized by `json` or `yaml` libraries.
  The dictionary has a tree structure, where each node is either a
  serialized object, or a pair of type and directory of serialized objects.
  The base of the directory is called "ROOT".
  
  Usage
  -----
      # To serialize
      pickler = GreykitePickler()
      pickled = pickler.dumps(obj)
      pickled_json = json.dumps(pickled)
  
      # To deserialize
      pickled = json.loads(pickled_json)
      obj = pickler.loads(pickled)
  
  Attributes
  ----------
  obj: any
      The object to be serialized.
class_docstr_tok: ["Extends","the","functionality","of","dill","to","serialize","arbitrary","objects",".","Originally","intended","to","serialize","the","anomaly","detector","class","`","~greykite.detection.anomaly_detector.AnomalyDetector","`",",","but","can","be","potentially","used","to","serialize","other","objects",".","Outputs","a","dictionary","that","can","be","serialized","by","`","json","`","or","`","yaml","`","libraries",".","The","dictionary","has","a","tree","structure",",","where","each","node","is","either","a","serialized","object",",","or","a","pair","of","type","and","directory","of","serialized","objects",".","The","base","of","the","directory","is","called","``","ROOT","''",".","Usage","--","--","-","#","To","serialize","pickler","=","GreykitePickler","(",")","pickled","=","pickler.dumps","(","obj",")","pickled_json","=","json.dumps","(","pickled",")","#","To","deserialize","pickled","=","json.loads","(","pickled_json",")","obj","=","pickler.loads","(","pickled",")","Attributes","--","--","--","--","--","obj",":","any","The","object","to","be","serialized","."]
docstr: Uses Depth First Search (DFS) to recursively serialize the input `obj`.
  
  For each object, the following happens:
  1. If the `obj` is serializable by `dill`, a dictionary key with {`obj_name`}.pkl will be generated.
  
      ```python
      {
          "{obj_name}.pkl": "serialized object",
      }
      ```
  
  2. Else a dictionary with two keys are generated.
      - {`obj_name`}.type stores the object type.
      - {`obj_name`}.dir stores the elements/attributes of the object.
      To build the {`obj_name`}.dir dictionary, `dumps` method is called recursively.
      ```python
      {
          "{obj_name}.type": "type of the object",
          "{obj_name}.dir": {
              "key1": "serialized value1",
              "key2": "serialized value2",
              ...
          }
      }
      ```
  
  The current supported recursion types are:
  
  - list/tuple: type name is "list" or "tuple", each element is attempted to
    be pickled independently if the entire list/tuple is not serializable.
    The order is preserved.
  - OrderedDict: type name is "ordered_dict", each key and value are attempted
    to be pickled independently if the entire dict is not serializable.
    The order is preserved.
  - dict: type name is "dict", each key and value are attempted to be pickled
    independently if the entire dict is not serializable.
    The order is not preserved.
  - class instance: type name is the class object, used to create new instance.
    Each attribute is attempted to be pickled independently if the entire
    instance is not serializable.
  
  Parameters
  ----------
  obj: any
      The object to be serialized.
  obj_name: `str`, default "ROOT"
      The name of the object to be serialized. Default is "ROOT".
  
  Returns
  -------
  serialized: `dict`
      The serialized object.
  
  Raises
  ------
  NotImplementedError: If the object cannot be serialized.
docstr_tok: ["Uses","Depth","First","Search","(","DFS",")","to","recursively","serialize","the","input","`","obj","`",".","For","each","object",",","the","following","happens",":","1",".","If","the","`","obj","`","is","serializable","by","`","dill","`",",","a","dictionary","key","with","{","`","obj_name","`","}",".pkl","will","be","generated",".","``","`","python","{","``","{","obj_name","}",".pkl","''",":","``","serialized","object","''",",","}","``","`","2",".","Else","a","dictionary","with","two","keys","are","generated",".","-","{","`","obj_name","`","}",".type","stores","the","object","type",".","-","{","`","obj_name","`","}",".dir","stores","the","elements\/attributes","of","the","object",".","To","build","the","{","`","obj_name","`","}",".dir","dictionary",",","`","dumps","`","method","is","called","recursively",".","``","`","python","{","``","{","obj_name","}",".type","''",":","``","type","of","the","object","''",",","``","{","obj_name","}",".dir","''",":","{","``","key1","''",":","``","serialized","value1","''",",","``","key2","''",":","``","serialized","value2","''",",","...","}","}","``","`","The","current","supported","recursion","types","are",":","-","list\/tuple",":","type","name","is","``","list","''","or","``","tuple","''",",","each","element","is","attempted","to","be","pickled","independently","if","the","entire","list\/tuple","is","not","serializable",".","The","order","is","preserved",".","-","OrderedDict",":","type","name","is","``","ordered_dict","''",",","each","key","and","value","are","attempted","to","be","pickled","independently","if","the","entire","dict","is","not","serializable",".","The","order","is","preserved",".","-","dict",":","type","name","is","``","dict","''",",","each","key","and","value","are","attempted","to","be","pickled","independently","if","the","entire","dict","is","not","serializable",".","The","order","is","not","preserved",".","-","class","instance",":","type","name","is","the","class","object",",","used","to","create","new","instance",".","Each","attribute","is","attempted","to","be","pickled","independently","if","the","entire","instance","is","not","serializable",".","Parameters","--","--","--","--","--","obj",":","any","The","object","to","be","serialized",".","obj_name",":","`","str","`",",","default","``","ROOT","''","The","name","of","the","object","to","be","serialized",".","Default","is","``","ROOT","''",".","Returns","--","--","--","-","serialized",":","`","dict","`","The","serialized","object",".","Raises","--","--","--","NotImplementedError",":","If","the","object","can","not","be","serialized","."]
code: def dumps(self, obj, obj_name=OBJECT_TYPE_ROOT):
      """Uses Depth First Search (DFS) to recursively serialize the input `obj`.
  
          For each object, the following happens:
          1. If the `obj` is serializable by `dill`, a dictionary key with {`obj_name`}.pkl will be generated.
  
              ```python
              {
                  "{obj_name}.pkl": "serialized object",
              }
              ```
  
          2. Else a dictionary with two keys are generated.
              - {`obj_name`}.type stores the object type.
              - {`obj_name`}.dir stores the elements/attributes of the object.
              To build the {`obj_name`}.dir dictionary, `dumps` method is called recursively.\
  
              ```python
              {
                  "{obj_name}.type": "type of the object",
                  "{obj_name}.dir": {
                      "key1": "serialized value1",
                      "key2": "serialized value2",
                      ...
                  }
              }
              ```
  
          The current supported recursion types are:
  
          - list/tuple: type name is "list" or "tuple", each element is attempted to
            be pickled independently if the entire list/tuple is not serializable.
            The order is preserved.
          - OrderedDict: type name is "ordered_dict", each key and value are attempted
            to be pickled independently if the entire dict is not serializable.
            The order is preserved.
          - dict: type name is "dict", each key and value are attempted to be pickled
            independently if the entire dict is not serializable.
            The order is not preserved.
          - class instance: type name is the class object, used to create new instance.
            Each attribute is attempted to be pickled independently if the entire
            instance is not serializable.
  
          Parameters
          ----------
          obj: any
              The object to be serialized.
          obj_name: `str`, default "ROOT"
              The name of the object to be serialized. Default is "ROOT".
  
          Returns
          -------
          serialized: `dict`
              The serialized object.
  
          Raises
          ------
          NotImplementedError: If the object cannot be serialized.
          """
      self.obj = obj
      try:
          serialized = self.dumps_to_str(obj)
          return {f"{obj_name}{PICKLE_KEY_EXTENSION}": serialized}
      except NotImplementedError:
          if isinstance(obj, OrderedDict):
              return self._serialize_ordered_dict(obj, obj_name)
          if isinstance(obj, dict):
              return self._serialize_dict(obj, obj_name)
          if isinstance(obj, (list, tuple)):
              return self._serialize_list_tuple(obj, obj_name)
          if hasattr(obj, "__class__") and not isinstance(obj, type):
              return self._serialize_class(obj, obj_name)
          else:
              raise NotImplementedError(
                  f"Cannot pickle object of type {type(obj)}."
              )
  
file_path: greykite/greykite/detection/common/pickler.py
imports: import codecs
  import inspect
  from collections import OrderedDict
  import dill
label_desc: Use image node_id 2 for calling the GreykitePickler obj's underlying member method code with example usage: obj.dumps(obj, obj_name) and returns: dict, self, self, self, self