code: def eval_resource_expression(val, threads_arg=True):
      def generic_callable(val, threads_arg, **kwargs):
          args = {
              "input": kwargs["input"],
              "attempt": kwargs["attempt"],
              "system_tmpdir": tempfile.gettempdir(),
          }
          if threads_arg:
              args["threads"] = kwargs["threads"]
          try:
              value = eval(
                  val,
                  args,
              )
          # Triggers for string arguments like n1-standard-4
          except NameError:
              return val
          except Exception as e:
              if not (
                  isinstance(e, FileNotFoundError)
                  and e.filename in kwargs["input"]
              ):
                  # Missing input files are handled by the caller
                  raise WorkflowError(
                      "Failed to evaluate default resources value "
                      f"'{val}'.\n"
                      "    String arguments may need additional "
                      "quoting. Ex: --default-resources "
                      "\"tmpdir='/home/user/tmp'\".",
                      e,
                  )
              raise e
          return value
  
      if threads_arg:
  
          def callable(wildcards, input, attempt, threads, rulename):
              return generic_callable(
                  val,
                  threads_arg=threads_arg,
                  wildcards=wildcards,
                  input=input,
                  attempt=attempt,
                  threads=threads,
                  rulename=rulename,
              )
  
      else:
  
          def callable(wildcards, input, attempt, rulename):
              return generic_callable(
                  val,
                  threads_arg=threads_arg,
                  wildcards=wildcards,
                  input=input,
                  attempt=attempt,
                  rulename=rulename,
              )
  
      return callable
  
imports: from collections import UserDict, defaultdict
  import itertools
  import operator
  import re
  import tempfile
  from snakemake.exceptions import ResourceScopesException, WorkflowError
  from snakemake.common.tbdstring import TBDString
label_desc: Use image node_id 1 for calling a global function with example usage: eval_resource_expression(val, threads_arg) and returns: callable, value, generic_callable, generic_callable, val