class_docstr: Defines `custom data type validation.
  
  <https://pydantic-docs.helpmanual.io/#custom-data-types>`_ for
  the :class:`~mxnet.context.Context` data type.
class_docstr_tok: ["Defines","`","custom","data","type","validation",".","<","https",":","\/\/pydantic-docs.helpmanual.io\/","#","custom-data-types",">","`","_","for","the",":","class",":","`","~mxnet.context.Context","`","data","type","."]
code: def validate(cls, v: Union[str, mx.Context]) -> mx.Context:
      if isinstance(v, mx.Context):
          return v
  
      m = re.search(r"^(?P<dev_type>cpu|gpu)(\((?P<dev_id>\d+)\))?$", v)
  
      if m:
          return mx.Context(m["dev_type"], int(m["dev_id"] or 0))
      else:
          raise ValueError(
              f"bad MXNet context {v}, expected either an "
              "mx.context.Context or its string representation"
          )
  
file_path: gluonts/src/gluonts/mx/context.py
imports: import functools
  import logging
  import re
  from typing import Union
  import mxnet
label_desc: Use image node_id 1 for calling the MXContext obj's underlying member method code with example usage: obj.validate(cls, v) and returns: v, mx