def __init__(self, items=None, item_class=None): from abjad.tools import datastructuretools from abjad.tools import pitchtools prototype_1 = (collections.Iterator, types.GeneratorType) prototype_2 = (TypedCounter, collections.Counter) if isinstance(items, str): items = items.split() elif isinstance(items, prototype_1): items = [item for item in items] elif isinstance(items, dict): items = self._dictionary_to_items(items, item_class) if isinstance(items, prototype_2): new_tokens = [] for item, count in items.items(): new_tokens.extend(count * [item]) items = new_tokens if item_class is None: item_class = self._named_item_class if items is not None: if (isinstance(items, datastructuretools.TypedCollection) and issubclass(items.item_class, self._parent_item_class)): item_class = items.item_class elif len(items): if isinstance(items, collections.Set): items = tuple(items) if isinstance(items, dict): item_class = self._dictionary_to_item_class(items) elif isinstance(items[0], str): item_class = self._named_item_class elif isinstance(items[0], (int, float)): item_class = self._numbered_item_class elif isinstance(items[0], self._parent_item_class): item_class = type(items[0]) assert issubclass(item_class, self._parent_item_class) TypedCounter.__init__( self, items=items, item_class=item_class, )
def __init__(self, tokens=None, item_class=None, custom_identifier=None): from abjad.tools import datastructuretools from abjad.tools import pitchtools if isinstance(tokens, str): tokens = tokens.split() elif isinstance(tokens, ( collections.Iterator, types.GeneratorType, )): tokens = [token for token in tokens] if isinstance(tokens, (TypedCounter, collections.Counter)): new_tokens = [] for token, count in tokens.iteritems(): new_tokens.extend([token] * count) tokens = new_tokens if item_class is None: item_class = self._named_item_class if tokens is not None: if isinstance(tokens, datastructuretools.TypedCollection) and \ issubclass(tokens.item_class, self._parent_item_class): item_class = tokens.item_class elif len(tokens): if isinstance(tokens, collections.Set): tokens = tuple(tokens) if isinstance(tokens[0], str): item_class = self._named_item_class elif isinstance(tokens[0], (int, float)): item_class = self._numbered_item_class elif isinstance(tokens[0], self._parent_item_class): item_class = type(tokens[0]) assert issubclass(item_class, self._parent_item_class) TypedCounter.__init__( self, tokens=tokens, item_class=item_class, custom_identifier=custom_identifier, )