<commit_before>###############################################################
#| CLASS Order Parameter
###############################################################

import mdtraj as md
import openpathsampling as paths
from openpathsampling.todict import restores_as_stub_object

class ObjectDict(dict):
    """
    Cache attached to Configuration indices stored in Configuration storage

    Parameters
    ----------
    name : string
        A short and unique name to be used in storage

    Attributes
    ----------
    name

    """

    def __init__(self, dimensions = 1, key_class = None):
        dict.__init__(self)
        self.dimensions = dimensions
        self.key_class = key_class

    def _get(self, obj):
        return dict.__getitem__(self, obj)

    def _set(self, obj, value):
        return dict.__setitem__(self, obj, value)

    def __getitem__(self, items):
        # Allow for numpy style of selecting several indices using a list as index parameter
        if type(items) is list:
            ret = [self._get(key) for key in items]
        else:
            ret = self._get(items)

        return ret

    def __setitem__(self, key, value):
        self._set(key, value)

    def existing(self, objs):
        """
        Find a subset of indices that are present in the cache

        Parameters
        ----------
        indices : list of int
            the initial list of indices to be tested

        Returns
        -------
        existing : list of int
            the subset of indices present in the cache
        """
        return [obj for obj in objs if obj in self]

    def missing(self, objs):
        """
        Find a subset of indices that are NOT present in the cache

        Parameters
        ----------
        indices : list of int
            the initial list of indices to be tested

        Returns
        -------
        existing : list of int
            the subset of indices NOT present in the cache
        """
        return [obj for obj in objs if obj not in self]


class StorableObjectDict(ObjectDict):
    """
    A cache that is attached to Configuration indices store in the Configuration storage

    Parameters
    ----------
    name : string
        A short and unique name to be used in storage

    Attributes
    ----------
    name
    """

    def __init__(self, name, dimensions = 1, key_class = None):
        super(StorableObjectDict, self).__init__(dimensions=dimensions, key_class=key_class)

        self.name = name
        self.storage_caches = dict() # caches the values stored in the data file
        self.var_name = key_class.__name__.lower() + '_' + 'op_' + self.name
        self.object_storages = [] # contains the list of associated ObjectStorages (that itself link to netCDF files)

        for s in self.object_storages:
            if s.content_class is not key_class:
                print 'One of the storages does not store objects of type :', key_class.__name__
                return

        for s in self.object_storages:
            self.storage_caches[s.storage] = dict()

    def _basetype(self, item):
        if type(item) is tuple:
            return item[0].content_class
        elif hasattr(item, 'base_cls'):
            return item.base_cls
        else:
            return type(item)

    def storable(self, item):
        """
        Return True if `item` has indices to be stored in an attached storage otherwise cache it in the dict itself
        """
        if type(item) is tuple:
            if item[0].content_class is self.key_class:
                if item[0].storage in self.object_storages and item[1] < len(item[0]):
                    return True
                else:
                    return False
            else:
                return False

        for s in self.storage_caches:
            if s in item.idx and item.idx[s] > 0:
                return True
        return False

    def in_store(self, item):
        """
        Returns True, if the item is already stored in an associated cache.
        """
        for s in self.storage_caches:
            if type(item) is tuple:
                if item[0].content_class is self.key_class:
                    if item[1] in self.storage_caches[item[0].storage]:
                        return True
                else:
                    # wrong type to load. Cannot ask for Configurations is
                    # values for Snapshots are stored
                    return False
            elif s in item.idx and item.idx[s] in self.storage_caches[s]:
                return True

        return False

    def __contains__(self, item):
        return dict.__contains__(self, item) or self.in_store(item)

    def _compatible(self, item):
        if type(item) is tuple:
            if item[0].content_class is self.key_class:
                return True
            else:
                return False
        else:
            if isinstance(item, self.key_class):
                return True
            else:
                return False

    def _get_from_stores(self, item):
        if self._compatible(item):
            for s in self.storage_caches:
                if type(item) is tuple:
                    if item[1] in self.storage_caches[item[0].storage]:
                        return self.storage_caches[item[0].storage][item[1]]
                elif s in item.idx and item.idx[s] in self.storage_caches[s]:
                    return self.storage_caches[s][item.idx[s]]

        return None

    def _set_to_stores(self, obj, value):
        for s in self.storage_caches:
            if s in obj.idx:
                self.storage_caches[s][obj.idx[s]] = value

    def _set(self, obj, value):
        if self.storable(obj):
            self._set_to_stores(obj, value)
        else:
            return dict.__setitem__(self, obj, value)

    def _get(self, item):<commit_msg>simplifies item_type handling in OP call<commit_after>###############################################################
#| CLASS Order Parameter
###############################################################

import mdtraj as md
import openpathsampling as paths
from openpathsampling.todict import restores_as_stub_object

class ObjectDict(dict):
    """
    Cache attached to Configuration indices stored in Configuration storage

    Parameters
    ----------
    name : string
        A short and unique name to be used in storage

    Attributes
    ----------
    name

    """

    def __init__(self, dimensions = 1, key_class = None):
        dict.__init__(self)
        self.dimensions = dimensions
        self.key_class = key_class

    def _get(self, obj):
        return dict.__getitem__(self, obj)

    def _set(self, obj, value):
        return dict.__setitem__(self, obj, value)

    def __getitem__(self, items):
        # Allow for numpy style of selecting several indices using a list as index parameter
        if type(items) is list:
            ret = [self._get(key) for key in items]
        else:
            ret = self._get(items)

        return ret

    def __setitem__(self, key, value):
        self._set(key, value)

    def existing(self, objs):
        """
        Find a subset of indices that are present in the cache

        Parameters
        ----------
        indices : list of int
            the initial list of indices to be tested

        Returns
        -------
        existing : list of int
            the subset of indices present in the cache
        """
        return [obj for obj in objs if obj in self]

    def missing(self, objs):
        """
        Find a subset of indices that are NOT present in the cache

        Parameters
        ----------
        indices : list of int
            the initial list of indices to be tested

        Returns
        -------
        existing : list of int
            the subset of indices NOT present in the cache
        """
        return [obj for obj in objs if obj not in self]


class StorableObjectDict(ObjectDict):
    """
    A cache that is attached to Configuration indices store in the Configuration storage

    Parameters
    ----------
    name : string
        A short and unique name to be used in storage

    Attributes
    ----------
    name
    """

    def __init__(self, name, dimensions = 1, key_class = None):
        super(StorableObjectDict, self).__init__(dimensions=dimensions, key_class=key_class)

        self.name = name
        self.storage_caches = dict() # caches the values stored in the data file
        self.var_name = key_class.__name__.lower() + '_' + 'op_' + self.name
        self.object_storages = [] # contains the list of associated ObjectStorages (that itself link to netCDF files)

        for s in self.object_storages:
            if s.content_class is not key_class:
                print 'One of the storages does not store objects of type :', key_class.__name__
                return

        for s in self.object_storages:
            self.storage_caches[s.storage] = dict()

    def _basetype(self, item):
        if type(item) is tuple:
            return item[0].content_class
        elif hasattr(item, 'base_cls'):
            return item.base_cls
        else:
            return type(item)

    def storable(self, item):
        """
        Return True if `item` has indices to be stored in an attached storage otherwise cache it in the dict itself
        """
        if type(item) is tuple:
            if item[0].content_class is self.key_class:
                if item[0].storage in self.object_storages and item[1] < len(item[0]):
                    return True
                else:
                    return False
            else:
                return False

        for s in self.storage_caches:
            if s in item.idx and item.idx[s] > 0:
                return True
        return False

    def in_store(self, item):
        """
        Returns True, if the item is already stored in an associated cache.
        """
        for s in self.storage_caches:
            if type(item) is tuple:
                if item[0].content_class is self.key_class:
                    if item[1] in self.storage_caches[item[0].storage]:
                        return True
                else:
                    # wrong type to load. Cannot ask for Configurations is
                    # values for Snapshots are stored
                    return False
            elif s in item.idx and item.idx[s] in self.storage_caches[s]:
                return True

        return False

    def __contains__(self, item):
        return dict.__contains__(self, item) or self.in_store(item)

    def _compatible(self, item):
        return self._basetype(item) is self.key_class

    def _get_from_stores(self, item):
        if self._compatible(item):
            for s in self.storage_caches:
                if type(item) is tuple:
                    if item[1] in self.storage_caches[item[0].storage]:
                        return self.storage_caches[item[0].storage][item[1]]
                elif s in item.idx and item.idx[s] in self.storage_caches[s]:
                    return self.storage_caches[s][item.idx[s]]

        return None

    def _set_to_stores(self, obj, value):
        for s in self.storage_caches:
            if s in obj.idx:
                self.storage_caches[s][obj.idx[s]] = value

    def _set(self, obj, value):
        if self.storable(obj):
            self._set_to_stores(obj, value)
        else:
            return dict.__setitem__(self, obj, value)

    def _get(self, item):
{'fraction_non_alphanumeric': 0.10300725890079503, 'fraction_numerical': 0.0026788800553059107, 'mean_word_length': 1.7786314525810325, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7467480', 'n_tokens_mistral': 3119, 'n_tokens_neox': 2974, 'n_words': 1066}