code: def build_ipy_lexer(python3):
      """Builds IPython lexers depending on the value of `python3`.
  
      The lexer inherits from an appropriate Python lexer and then adds
      information about IPython specific keywords (i.e. magic commands,
      shell commands, etc.)
  
      Parameters
      ----------
      python3 : bool
          If `True`, then build an IPython lexer from a Python 3 lexer.
  
      """
      # It would be nice to have a single IPython lexer class which takes
      # a boolean `python3`.  But since there are two Python lexer classes,
      # we will also have two IPython lexer classes.
      if python3:
          PyLexer = Python3Lexer
          name = "IPython3"
          aliases = ["ipython3"]
          doc = """IPython3 Lexer"""
      else:
          PyLexer = PythonLexer
          name = "IPython"
          aliases = ["ipython2", "ipython"]
          doc = """IPython Lexer"""
  
      ipython_tokens = [
          (
              r"(?s)(\s*)(%%capture)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(PyLexer)),
          ),
          (
              r"(?s)(\s*)(%%debug)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(PyLexer)),
          ),
          (
              r"(?is)(\s*)(%%html)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(HtmlLexer)),
          ),
          (
              r"(?s)(\s*)(%%javascript)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(JavascriptLexer)),
          ),
          (
              r"(?s)(\s*)(%%js)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(JavascriptLexer)),
          ),
          (
              r"(?s)(\s*)(%%latex)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(TexLexer)),
          ),
          (
              r"(?s)(\s*)(%%perl)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(PerlLexer)),
          ),
          (
              r"(?s)(\s*)(%%prun)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(PyLexer)),
          ),
          (
              r"(?s)(\s*)(%%pypy)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(PyLexer)),
          ),
          (
              r"(?s)(\s*)(%%python)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(PyLexer)),
          ),
          (
              r"(?s)(\s*)(%%python2)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(PythonLexer)),
          ),
          (
              r"(?s)(\s*)(%%python3)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(Python3Lexer)),
          ),
          (
              r"(?s)(\s*)(%%ruby)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(RubyLexer)),
          ),
          (
              r"(?s)(\s*)(%%time)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(PyLexer)),
          ),
          (
              r"(?s)(\s*)(%%timeit)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(PyLexer)),
          ),
          (
              r"(?s)(\s*)(%%writefile)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(PyLexer)),
          ),
          (
              r"(?s)(\s*)(%%file)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(PyLexer)),
          ),
          (
              r"(?s)(\s*)(%%)(\w+)(.*)",
              bygroups(Text, Operator, Keyword, Text),
          ),
          (
              r"(?s)(^\s*)(%%!)([^\n]*\n)(.*)",
              bygroups(Text, Operator, Text, using(BashLexer)),
          ),
          (
              r"(%%?)(\w+)(\?\??)$",
              bygroups(Operator, Keyword, Operator),
          ),
          (r"\b(\?\??)(\s*)$", bygroups(Operator, Text)),
          (
              r"(%)(sx|sc|system)(.*)(\n)",
              bygroups(Operator, Keyword, using(BashLexer), Text),
          ),
          (r"(%)(\w+)(.*\n)", bygroups(Operator, Keyword, Text)),
          (
              r"^(!!)(.+)(\n)",
              bygroups(Operator, using(BashLexer), Text),
          ),
          (
              r"(!)(?!=)(.+)(\n)",
              bygroups(Operator, using(BashLexer), Text),
          ),
          (
              r"^(\s*)(\?\??)(\s*%{0,2}[\w\.\*]*)",
              bygroups(Text, Operator, Text),
          ),
          (
              r"(\s*%{0,2}[\w\.\*]*)(\?\??)(\s*)$",
              bygroups(Text, Operator, Text),
          ),
      ]
  
      tokens = PyLexer.tokens.copy()
      tokens["root"] = ipython_tokens + tokens["root"]
  
      attrs = {
          "name": name,
          "aliases": aliases,
          "filenames": [],
          "__doc__": doc,
          "tokens": tokens,
      }
  
      return type(name, (PyLexer,), attrs)
  
imports: import re
  from pygments.lexers import BashLexer, HtmlLexer, JavascriptLexer, RubyLexer, PerlLexer, PythonLexer, Python3Lexer, TexLexer
  from pygments.lexer import Lexer, DelegatingLexer, RegexLexer, do_insertions, bygroups, using
  from pygments.token import Generic, Keyword, Literal, Name, Operator, Other, Text, Error
  from pygments.util import get_bool_opt
label_desc: Use image node_id 1 for calling a global function with example usage: build_ipy_lexer(python3) and returns: type