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: Pickles a dictionary when it can not be directly pickled by `dill`.
  
  Generates a dictionary with two keys.
      - {`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.
      The keys are serialized with the suffix "__key__" and the values are
      serialized with the suffix "__value__".
      This is done because the keys of a dictionary can be complex classes that can
      not be directly serialized by `dill`.
  
      ```python
      {
          "{obj_name}.type": "serializable object type",
          "{obj_name}.dir": {
              "{key1}__key__": "serialized key1",
              "{key1}__value__": "serialized value corresponding to key1",
              "{key2}__key__": "serialized key2",
              "{key2}__value__": "serialized value corresponding to key2",
              ...
          }
      }
      ```
  
  Parameters
  ----------
  obj: `dict`
      The dictionary to be serialized.
  obj_name: `str`
      The name of the object to be serialized.
  
  Returns
  -------
  serialized: `dict`
      The serialized dictionary.
docstr_tok: ["Pickles","a","dictionary","when","it","can","not","be","directly","pickled","by","`","dill","`",".","Generates","a","dictionary","with","two","keys",".","-","{","`","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",".","The","keys","are","serialized","with","the","suffix","``","__key__","''","and","the","values","are","serialized","with","the","suffix","``","__value__","''",".","This","is","done","because","the","keys","of","a","dictionary","can","be","complex","classes","that","can","not","be","directly","serialized","by","`","dill","`",".","``","`","python","{","``","{","obj_name","}",".type","''",":","``","serializable","object","type","''",",","``","{","obj_name","}",".dir","''",":","{","``","{","key1","}","__key__","''",":","``","serialized","key1","''",",","``","{","key1","}","__value__","''",":","``","serialized","value","corresponding","to","key1","''",",","``","{","key2","}","__key__","''",":","``","serialized","key2","''",",","``","{","key2","}","__value__","''",":","``","serialized","value","corresponding","to","key2","''",",","...","}","}","``","`","Parameters","--","--","--","--","--","obj",":","`","dict","`","The","dictionary","to","be","serialized",".","obj_name",":","`","str","`","The","name","of","the","object","to","be","serialized",".","Returns","--","--","--","-","serialized",":","`","dict","`","The","serialized","dictionary","."]
code: def _serialize_dict(self, obj, obj_name):
      """Pickles a dictionary when it can not be directly pickled by `dill`.
  
      Generates a dictionary with two keys.
          - {`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.
          The keys are serialized with the suffix "__key__" and the values are
          serialized with the suffix "__value__".
          This is done because the keys of a dictionary can be complex classes that can
          not be directly serialized by `dill`.
  
          ```python
          {
              "{obj_name}.type": "serializable object type",
              "{obj_name}.dir": {
                  "{key1}__key__": "serialized key1",
                  "{key1}__value__": "serialized value corresponding to key1",
                  "{key2}__key__": "serialized key2",
                  "{key2}__value__": "serialized value corresponding to key2",
                  ...
              }
          }
          ```
  
      Parameters
      ----------
      obj: `dict`
          The dictionary to be serialized.
      obj_name: `str`
          The name of the object to be serialized.
  
      Returns
      -------
      serialized: `dict`
          The serialized dictionary.
      """
      # Dumps the type in .type dictionary
      serialized = {
          f"{obj_name}{TYPE_KEY_EXTENSION}": self.dumps_to_str(
              type(obj).__name__
          )
      }
  
      # Dumps the keys and values in .dir dictionary
      result = {}
      for key, value in obj.items():
          name = str(key)
          result.update(
              self.dumps(obj=key, obj_name=f"{name}{KEY_SUFFIX}")
          )
          result.update(
              self.dumps(obj=value, obj_name=f"{name}{VALUE_SUFFIX}")
          )
      serialized[f"{obj_name}{DIRECTORY_KEY_EXTENSION}"] = result
  
      return serialized
  
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 5 for calling the GreykitePickler obj's underlying member method code with example usage: obj._serialize_dict(obj, obj_name) and returns: serialized