def for_various_result_types_of__filter_by_which(test_meth): # Taking into consideration that the data specification's # overridable method `filter_by_which()` is required to return a # container that is either a *dict items view* or a *set-like* # object, we want to ensure that the methods which call # `filter_by_which()` handle the call results properly for various # types of these results (i.e., regardless of whether the result is # a *dict items view* or a *set-like* object; regardless of whether # the object is an instance of a builtin type or of a custom one; # and regardless of whether the object is mutable or immutable...). # # By wrapping with this decorator a test of a data specification's # method which uses `filter_by_which()` call results *and* by # decorating the test case class that contains such a test with # `@unittest_expander.expand` -- we ensure that the method will be # tested for various kinds of types of `filter_by_which()` call # results. @foreach([ None, lambda iterable: dict(iterable).viewitems(), lambda iterable: collections.ItemsView(dict(iterable)), frozenset, set, CustomImmutableSet, CustomMutableSet, ]) @functools.wraps(test_meth) def decorated_test_meth(self, result_type_adjuster): if result_type_adjuster is None: test_meth(self) return called = [] wrapped = _make_wrapped__filter_by_which( result_type_adjuster, called, orig=self.base_data_spec_class.filter_by_which) with patch.object(self.base_data_spec_class, 'filter_by_which', wrapped): assert not called, 'bug in the test' test_meth(self) assert called, 'bug in the test' def _make_wrapped__filter_by_which(result_type_adjuster, called, orig): @staticmethod def wrapped(*args, **kwargs): called.append(None) orig_result = orig(*args, **kwargs) return result_type_adjuster(orig_result) return wrapped return decorated_test_meth
def iteritems(self): return collections.ItemsView(self.mapping)
def viewitems(self): return collections.ItemsView(self)
import collections device = {"hostname":"falcon"} print(device) device['vendor'] = 'nokia' print(device) device['model'] = '7750' print(device) device.setdefault('type', 'SR12') print(device) print(collections.Counter(device)) print(collections.ItemsView(device)) print(collections.KeysView(device)) print(collections.MappingView(device)) for k,v in device.items(): print(k,v) for k,v in sorted(device.items()): print(k,v) for item in collections.ItemsView(device): print(item) device.pop('type') print(device)
def _viewratings(instance): "Returns a :py:class:`collections.ItemsView` of its own ratings." return collections.ItemsView(instance._ratings)
def _viewitems(instance): "Returns a :py:class:`collections.ItemsView` of its own items." return collections.ItemsView(instance._dict)