code: def extract_hist_ranges(ranges_str):
      """Turn a string of history ranges into 3-tuples of (session, start, stop).
  
      Empty string results in a `[(0, 1, None)]`, i.e. "everything from current
      session".
  
      Examples
      --------
      >>> list(extract_hist_ranges("~8/5-~7/4 2"))
      [(-8, 5, None), (-7, 1, 5), (0, 2, 3)]
      """
      if ranges_str == "":
          yield (0, 1, None)  # Everything from current session
          return
  
      for range_str in ranges_str.split():
          rmatch = range_re.match(range_str)
          if not rmatch:
              continue
          start = rmatch.group("start")
          if start:
              start = int(start)
              end = rmatch.group("end")
              # If no end specified, get (a, a + 1)
              end = int(end) if end else start + 1
          else:  # start not specified
              if not rmatch.group("startsess"):  # no startsess
                  continue
              start = 1
              end = None  # provide the entire session hist
  
          if rmatch.group("sep") == "-":  # 1-3 == 1:4 --> [1, 2, 3]
              end += 1
          startsess = rmatch.group("startsess") or "0"
          endsess = rmatch.group("endsess") or startsess
          startsess = int(startsess.replace("~", "-"))
          endsess = int(endsess.replace("~", "-"))
          assert (
              endsess >= startsess
          ), "start session must be earlier than end session"
  
          if endsess == startsess:
              yield (startsess, start, end)
              continue
          # Multiple sessions in one range:
          yield (startsess, start, None)
          for sess in range(startsess + 1, endsess):
              yield (sess, 1, None)
          yield (endsess, 1, end)
  
imports: import atexit
  import datetime
  from pathlib import Path
  import re
  import sqlite3
  import threading
  from traitlets.config.configurable import LoggingConfigurable
  from decorator import decorator
  from IPython.utils.decorators import undoc
  from IPython.paths import locate_profile
  from traitlets import Any, Bool, Dict, Instance, Integer, List, Unicode, Union, TraitError, default, observe
label_desc: Use image node_id 3 for calling a global function with example usage: extract_hist_ranges(ranges_str) without return types