code: def set_env_var(var, val):
      "Set an environment variable & restore it."
      is_set = var in os.environ
      old_val = None
      if is_set:
          old_val = os.environ[var]
      try:
          if val is None:
              if is_set:
                  del os.environ[var]
          else:
              os.environ[var] = val
          yield
      finally:
          if is_set:
              os.environ[var] = old_val
          elif val is not None:
              del os.environ[var]
  
file_path: lkpy/lenskit/util/test.py
imports: import os
  import os.path
  from contextlib import contextmanager
  import pytest
  from lenskit.datasets import MovieLens, ML100K
  from lenskit.crossfold import simple_test_pair
  from lenskit.algorithms.basic import PopScore
  from lenskit.algorithms.ranking import PlackettLuce
  from lenskit.batch import recommend
label_desc: Use image node_id 2 for calling a global function with example usage: set_env_var(var, val) without return types