<commit_before>"""
Application registry
====================
"""
import collections
import inspect
from typing import Dict, Union

ResourceID = collections.namedtuple('ResourceID', ['type', 'id'])
ResourceIdentifier = Union[ResourceID, Dict[str, str]]


class Registry(collections.UserDict):
    __slots__ = ('data',)

    def __getitem__(self, key):
        return super(Registry, self).__getitem__(
            key if isinstance(key, str) or inspect.isclass(key) else type(key)
        )

    def ensure_identifier(self, obj, asdict=False) -> ResourceIdentifier:
        """
        Returns the identifier object (:class:`ResourceID`) for the *resource*:

        .. code-block:: python3

            >>> registry.ensure_identifier({'type': 'something', 'id': 123})
            ResourceID(type='something', id='123')

        :arg obj:
            A two tuple ``(typename, id)``, a resource object or a resource
            document, which contains the *id* and *type* key
            ``{"type": ..., "id": ...}``.
        :arg bool asdict:
            Return ResourceID as dictionary if true
        """
        if isinstance(obj, collections.Sequence):
            assert len(obj) == 2
            result = ResourceID(str(obj[0]), str(obj[1]))
        elif isinstance(obj, collections.Mapping):
            result = ResourceID(str(obj['type']), str(obj['id']))
        else:
            schema = self[type(obj)]
            result = ResourceID(schema.type, str(schema._get_id(obj)))

        return result._asdict() if asdict and result else result<commit_msg>Avoid assertion in Registry ensure identifier method<commit_after>"""
Application registry
====================
"""
import collections
import inspect
from typing import Dict, Union

ResourceID = collections.namedtuple('ResourceID', ['type', 'id'])
ResourceIdentifier = Union[ResourceID, Dict[str, str]]


class Registry(collections.UserDict):
    __slots__ = ('data',)

    def __getitem__(self, key):
        return super(Registry, self).__getitem__(
            key if isinstance(key, str) or inspect.isclass(key) else type(key)
        )

    def ensure_identifier(self, obj, asdict=False) -> ResourceIdentifier:
        """
        Returns the identifier object (:class:`ResourceID`) for the *resource*:

        .. code-block:: python3

            >>> registry.ensure_identifier({'type': 'something', 'id': 123})
            ResourceID(type='something', id='123')

        :arg obj:
            A two tuple ``(typename, id)``, a resource object or a resource
            document, which contains the *id* and *type* key
            ``{"type": ..., "id": ...}``.
        :arg bool asdict:
            Return ResourceID as dictionary if true
        """
        if isinstance(obj, collections.Sequence) and len(obj) == 2:
            result = ResourceID(str(obj[0]), str(obj[1]))
        elif isinstance(obj, collections.Mapping):
            result = ResourceID(str(obj['type']), str(obj['id']))
        else:
            schema = self[type(obj)]
            result = ResourceID(schema.type, str(schema._get_id(obj)))

        return result._asdict() if asdict and result else result
{'fraction_non_alphanumeric': 0.14657360406091371, 'fraction_numerical': 0.006345177664974619, 'mean_word_length': 2.7851140456182475, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2617160', 'n_tokens_mistral': 867, 'n_tokens_neox': 832, 'n_words': 245}