code: def convert_to_dgl_func(pyfunc):
      """Convert a python function to DGL function
  
      Parameters
      ----------
      pyfunc : python function
          The python function to be converted.
  
      Returns
      -------
      dglfunc: dgl.nd.Function
          The converted dgl function.
      """
      local_pyfunc = pyfunc
  
      def cfun(args, type_codes, num_args, ret, _):
          """ctypes function"""
          num_args = (
              num_args.value
              if isinstance(num_args, ctypes.c_int)
              else num_args
          )
          pyargs = (
              C_TO_PY_ARG_SWITCH[type_codes[i]](args[i])
              for i in range(num_args)
          )
          # pylint: disable=broad-except
          try:
              rv = local_pyfunc(*pyargs)
          except Exception:
              msg = traceback.format_exc()
              _LIB.DGLAPISetLastError(c_str(msg))
              return -1
  
          if rv is not None:
              if isinstance(rv, tuple):
                  raise ValueError(
                      "PackedFunction can only support one return value"
                  )
              temp_args = []
              values, tcodes, _ = _make_dgl_args((rv,), temp_args)
              if not isinstance(ret, DGLRetValueHandle):
                  ret = DGLRetValueHandle(ret)
              check_call(
                  _LIB.DGLCFuncSetReturn(
                      ret, values, tcodes, ctypes.c_int(1)
                  )
              )
              _ = temp_args
              _ = rv
          return 0
  
      handle = FunctionHandle()
      f = DGLPackedCFunc(cfun)
      # NOTE: We will need to use python-api to increase ref count of the f
      # DGL_FREE_PYOBJ will be called after it is no longer needed.
      pyobj = ctypes.py_object(f)
      ctypes.pythonapi.Py_IncRef(pyobj)
      check_call(
          _LIB.DGLFuncCreateFromCFunc(
              f, pyobj, DGL_FREE_PYOBJ, ctypes.byref(handle)
          )
      )
      return _CLASS_FUNCTION(handle, False)
  
imports: from __future__ import absolute_import
  import ctypes
  import traceback
  from numbers import Integral, Number
  from ..base import _LIB, c_str, check_call, string_types
  from ..object_generic import convert_to_object, ObjectGeneric
  from ..runtime_ctypes import DGLByteArray, DGLContext, DGLDataType
  from .None import ndarray, object
  from .ndarray import _make_array, NDArrayBase
  from .object import ObjectBase
  from .types import _wrap_arg_func, C_TO_PY_ARG_SWITCH, DGLCFuncFinalizer, DGLPackedCFunc, DGLValue, RETURN_SWITCH, TypeCode
label_desc: Use image node_id 2 for calling a global function with example usage: convert_to_dgl_func(pyfunc) and returns: _CLASS_FUNCTION, int, unknown