code: def encode(v: Any) -> Any:
      """
      Transforms a value `v` as a serializable intermediate representation (for
      example, named tuples are encoded as dictionaries). The intermediate
      representation is then recursively traversed and serialized either as
      Python code or as JSON string.
  
      This function is decorated with :func:`~functools.singledispatch` and can
      be specialized by clients for families of types that are not supported by
      the basic implementation (explained below).
  
      Examples
      --------
  
      The conversion logic implemented by the basic implementation is used
      as a fallback and is best explained by a series of examples.
  
      Lists (as lists).
  
      >>> encode([1, 2.0, '3'])
      [1, 2.0, '3']
  
      Dictionaries (as dictionaries).
  
      >>> encode({'a': 1, 'b': 2.0, 'c': '3'})
      {'a': 1, 'b': 2.0, 'c': '3'}
  
      Named tuples (as dictionaries with a
      ``'__kind__': <Kind.Instance: 'instance'>`` member).
  
      >>> from pprint import pprint
      >>> from typing import NamedTuple
      >>> class ComplexNumber(NamedTuple):
      ...     x: float = 0.0
      ...     y: float = 0.0
      >>> pprint(encode(ComplexNumber(4.0, 2.0)))
      {'__kind__': <Kind.Instance: 'instance'>,
       'class': 'gluonts.core.serde._base.ComplexNumber',
       'kwargs': {'x': 4.0, 'y': 2.0}}
  
      Classes with a :func:`~gluonts.core.component.validated` initializer (as
      dictionaries with a ``'__kind__': <Kind.Instance: 'instance'>`` member).
  
      >>> from gluonts.core.component import validated
      >>> class ComplexNumber:
      ...     @validated()
      ...     def __init__(self, x: float = 0.0, y: float = 0.0) -> None:
      ...         self.x = x
      ...         self.y = y
      >>> pprint(encode(ComplexNumber(4.0, 2.0)))
      {'__kind__': <Kind.Instance: 'instance'>,
       'args': [],
       'class': 'gluonts.core.serde._base.ComplexNumber',
       'kwargs': {'x': 4.0, 'y': 2.0}}
  
      Classes with a ``__getnewargs_ex__`` magic method (as dictionaries with a
      ``'__kind__': <Kind.Instance: 'instance'>`` member).
  
      >>> from gluonts.core.component import validated
      >>> class ComplexNumber:
      ...     def __init__(self, x: float = 0.0, y: float = 0.0) -> None:
      ...         self.x = x
      ...         self.y = y
      ...     def __getnewargs_ex__(self):
      ...         return [], {'x': self.x, 'y': self.y}
      >>> pprint(encode(ComplexNumber(4.0, 2.0)))
      {'__kind__': <Kind.Instance: 'instance'>,
       'args': [],
       'class': 'gluonts.core.serde._base.ComplexNumber',
       'kwargs': {'x': 4.0, 'y': 2.0}}
  
  
      Types (as dictionaries with a ``'__kind__': <Kind.Type: 'type'> member``).
  
      >>> encode(ComplexNumber)
      {'__kind__': <Kind.Type: 'type'>,
       'class': 'gluonts.core.serde._base.ComplexNumber'}
  
      Parameters
      ----------
      v
          The value to be encoded.
  
      Returns
      -------
      Any
          An encoding of ``v`` that can be serialized to Python code or
          JSON string.
  
      See Also
      --------
      decode
          Inverse function.
      dump_json
          Serializes an object to a JSON string.
      """
      if v is None:
          return None
  
      if isinstance(v, (float, int, str)):
          return v
  
      # check for namedtuples first, to encode them not as plain tuples
      if isinstance(v, tuple) and hasattr(v, "_asdict"):
          v = cast(NamedTuple, v)
          return {
              "__kind__": Kind.Instance,
              "class": fqname_for(v.__class__),
              "kwargs": encode(v._asdict()),
          }
  
      if isinstance(v, (tuple, set)):
          return {
              "__kind__": Kind.Instance,
              "class": fqname_for(type(v)),
              "args": [list(map(encode, v))],
          }
  
      if isinstance(v, list):
          return list(map(encode, v))
  
      if isinstance(v, dict):
          return valmap(encode, v)
  
      if isinstance(v, type):
          return {"__kind__": Kind.Type, "class": fqname_for(v)}
  
      if hasattr(v, "__init_passed_kwargs__"):
          return {
              "__kind__": Kind.Instance,
              "class": fqname_for(v.__class__),
              "kwargs": encode(
                  v.__init_passed_kwargs__
              ),  # mypy: ignore
          }
  
      if hasattr(v, "__getnewargs_ex__"):
          args, kwargs = v.__getnewargs_ex__()  # mypy: ignore
  
          return {
              "__kind__": Kind.Instance,
              "class": fqname_for(v.__class__),
              # args need to be a list, since we encode tuples explicitly
              "args": encode(list(args)),
              "kwargs": encode(kwargs),
          }
  
      try:
          # as fallback, we try to just take the path of the value
          fqname = fqname_for(v)
          assert (
              "<lambda>" not in fqname
          ), f"Can't serialize lambda function {fqname}"
  
          if hasattr(v, "__self__") and hasattr(v, "__func__"):
              # v is a method
              # to model`obj.method`, we encode `getattr(obj, "method")`
              return {
                  "__kind__": Kind.Instance,
                  "class": fqname_for(getattr),
                  "args": encode((v.__self__, v.__func__.__name__)),
              }
  
          return {"__kind__": Kind.Type, "class": fqname_for(v)}
      except AttributeError:
          pass
  
      raise RuntimeError(bad_type_msg.format(fqname_for(v.__class__)))
  
imports: import textwrap
  from enum import Enum
  from functools import singledispatch, partial
  from pathlib import PurePath
  from pydoc import locate
  from typing import Any, NamedTuple, cast
  from toolz.dicttoolz import valmap
  from gluonts.core import fqname_for
  from gluonts.pydantic import BaseModel
label_desc: Use image node_id 1 for calling a global function with example usage: encode(v) and returns: None, v, dict, dict, list, valmap, dict, dict, dict, dict, dict