def _hashable(item): """ Returns the item if it is naturally hashable, otherwise it tries to use ub.hash_data to make it hashable. Errors if it cannot. """ try: hash(item) except TypeError: return util_hash.hash_data(item) else: return item
def _condense_cfgstr(self, cfgstr=None): cfgstr = self._rectify_cfgstr(cfgstr) # The 49 char maxlen is just long enough for an 8 char name, an 1 char # underscore, and a 40 char sha1 hash. max_len = 49 if len(cfgstr) > max_len: condensed = util_hash.hash_data(cfgstr, hasher=self.hasher, base='hex') condensed = condensed[0:max_len] else: condensed = cfgstr return condensed
def _rectify_cfgstr(self, cfgstr=None): cfgstr = self.cfgstr if cfgstr is None else cfgstr if cfgstr is None and self.depends is not None: from ubelt import util_hash # lazy hashing of depends data into cfgstr if isinstance(self.depends, str): self.cfgstr = self.depends else: self.cfgstr = util_hash.hash_data(self.depends) cfgstr = self.cfgstr if cfgstr is None and self.enabled: warnings.warn( 'No cfgstr given in Cacher constructor or call for {}'.format( self.fname), UserWarning) cfgstr = '' if self.fname is None: raise AssertionError('no fname specified in Cacher') if self.dpath is None: raise AssertionError('no dpath specified in Cacher') return cfgstr