def group(self, items, keep_empty=False):
        """
        Given an iterable of instances, groups them by state using :class:`ManagedState` instances
        as dictionary keys. Returns an `OrderedDict` that preserves the order of states from
        the source :class:`~coaster.utils.classes.LabeledEnum`.

        :param bool keep_empty: If ``True``, empty states are included in the result
        """
        cls = self.cls if self.cls is not None else type(self.obj)  # Class of the item being managed
        groups = OrderedDict()
        for mstate in self.statemanager.states_by_value.values():
            # Ensure we sort groups using the order of states in the source LabeledEnum.
            # We'll discard the unused states later.
            groups[mstate] = []
        # Now process the items by state
        for item in items:
            # Use isinstance instead of `type(item) != cls` to account for subclasses
            if not isinstance(item, cls):
                raise TypeError("Item %s is not an instance of type %s" % (repr(item), repr(self.cls)))
            statevalue = self.statemanager._value(item)
            mstate = self.statemanager.states_by_value[statevalue]
            groups[mstate].append(item)
        if not keep_empty:
            for key, value in list(groups.items()):
                if not value:
                    del groups[key]
        return groups
def group ( self , items , keep_empty = False ) : cls = self . cls if self . cls is not None else type ( self . obj ) groups = OrderedDict ( ) for mstate in self . statemanager . states_by_value . values ( ) : groups [ mstate ] = [ ] for item in items : if not isinstance ( item , cls ) : raise TypeError ( "Item %s is not an instance of type %s" % ( repr ( item ) , repr ( self . cls ) ) ) statevalue = self . statemanager . _value ( item ) mstate = self . statemanager . states_by_value [ statevalue ] groups [ mstate ] . append ( item ) if not keep_empty : for key , value in list ( groups . items ( ) ) : if not value : del groups [ key ] return groups
Given an iterable of instances, groups them by state using :class:`ManagedState` instances
        as dictionary keys. Returns an `OrderedDict` that preserves the order of states from
        the source :class:`~coaster.utils.classes.LabeledEnum`.

        :param bool keep_empty: If ``True``, empty states are included in the result