code: def test_result_filter():
      # ensure baseline without filtering
      assert_equal(
          [r["somekey"] for r in TestUtils().__call__(4)], [0, 1, 2, 3]
      )
      # test two functionally equivalent ways to filter results
      # 1. Constraint-based -- filter by exception
      #    we have a full set of AND and OR operators for this
      # 2. custom filer function -- filter by boolean return value
      for filt in (
          EnsureKeyChoice("somekey", (0, 2)),
          lambda x: x["somekey"] in (0, 2),
      ):
          assert_equal(
              [
                  r["somekey"]
                  for r in TestUtils().__call__(4, result_filter=filt)
              ],
              [0, 2],
          )
          # constraint returns full dict
          assert_dict_equal(
              TestUtils().__call__(4, result_filter=filt)[-1],
              {
                  "action": "off",
                  "path": "some",
                  "status": "ok",
                  "somekey": 2,
              },
          )
  
      # test more sophisticated filters that actually get to see the
      # API call's kwargs
      def greatfilter(res, **kwargs):
          assert_equal(kwargs.get("dataset", "bob"), "awesome")
          return True
  
      TestUtils().__call__(
          4, dataset="awesome", result_filter=greatfilter
      )
  
      def sadfilter(res, **kwargs):
          assert_equal(kwargs.get("dataset", "bob"), None)
          return True
  
      TestUtils().__call__(4, result_filter=sadfilter)
  
imports: import logging
  import re
  from contextlib import contextmanager
  from os.path import exists
  from os.path import join
  from time import sleep
  from datalad.distribution.dataset import Dataset, EnsureDataset, datasetmethod
  from datalad.interface.base import build_doc, eval_results
  from datalad.support.constraints import EnsureKeyChoice, EnsureNone, EnsureStr
  from datalad.support.param import Parameter
  from datalad.tests.utils_pytest import assert_dict_equal, assert_equal, assert_in, assert_not_equal, assert_not_in, assert_raises, assert_re_in, assert_repo_status, assert_result_count, assert_true, ok_, slow, with_tempfile, with_tree
  from datalad.utils import swallow_logs, swallow_outputs
  from ..base import Interface
  from ..results import get_status_dict
  from ..utils import discover_dataset_trace_to_targets, handle_dirty_dataset
label_desc: Use image node_id 7 for calling a global function with example usage: test_result_filter() and returns: True, True