Python collections.Hashable() Examples

The following are code examples for showing how to use collections.Hashable(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: matchminer-engine   Author: dfci   File: cerberus.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def _validate_excludes(self, excludes, field, value):
        """ {'type': ['hashable', 'hashables']} """
        if isinstance(excludes, Hashable):
            excludes = [excludes]

        # Save required field to be checked latter
        if 'required' in self.schema[field] and self.schema[field]['required']:
            self._unrequired_by_excludes.add(field)
        for exclude in excludes:
            if (exclude in self.schema and
               'required' in self.schema[exclude] and
                    self.schema[exclude]['required']):

                self._unrequired_by_excludes.add(exclude)

        if [True for key in excludes if key in self.document]:
            # Wrap each field in `excludes` list between quotes
            exclusion_str = ', '.join("'{0}'"
                                      .format(word) for word in excludes)
            self._error(field, errors.EXCLUDES_FIELD, exclusion_str) 
Example 2
Project: forge   Author: datawire   File: match.py    Apache License 2.0 6 votes vote down vote up
def projections(value, match_value=True):
    if match_value and isinstance(value, collections.Hashable):
        yield value
    traits = getattr(value, "MATCH_TRAITS", None)
    if traits is not None:
        if isinstance(traits, tuple):
            for t in traits:
                yield t
        else:
            yield traits
    if not isinstance(value, Marker):
        if isinstance(value, super):
            for cls in value.__self_class__.__mro__[1:]:
                yield cls
        else:
            for cls in value.__class__.__mro__:
                yield cls 
Example 3
Project: BAG_framework   Author: ucb-art   File: immutable.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def to_immutable(obj: Any) -> ImmutableType:
    """Convert the given Python object into an immutable type."""
    if obj is None:
        return obj
    if isinstance(obj, Hashable):
        # gets around cases of tuple of un-hashable types.
        try:
            hash(obj)
            return obj
        except TypeError:
            pass
    if isinstance(obj, tuple):
        return tuple((to_immutable(v) for v in obj))
    if isinstance(obj, list):
        return ImmutableList([to_immutable(v) for v in obj])
    if isinstance(obj, set):
        return ImmutableList([to_immutable(v) for v in sorted(obj)])
    if isinstance(obj, dict):
        return ImmutableSortedDict(obj)

    raise ValueError('Cannot convert the following object to immutable type: {}'.format(obj)) 
Example 4
Project: Redy   Author: thautwarm   File: bytecode_api.py    MIT License 6 votes vote down vote up
def __getitem__(self, k):
        if isinstance(k, Hashable):
            try:
                return self._dct[k]
            except TypeError:
                # Actually we should ensure all the nested structure in the object `k` should be hashable,
                # but it does cost.
                # `dict.__getitem__` invokes the builtin hashing method, which
                # failed if and only if this object isn't really hashable.
                # In the definition of `_GeneralMapping.__setitem__` there is a similar case.
                pass
        undef = _undef

        looked_up = next((snd for fst, snd in self._asoc_lst if fst == k),
                         undef)

        if looked_up is undef:
            raise KeyError(k)
        return looked_up 
Example 5
Project: pyblish-win   Author: pyblish   File: test_hash.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_hashable(self):
        objects = (self.default_expected +
                   self.fixed_expected)
        for obj in objects:
            self.assertIsInstance(obj, Hashable) 
Example 6
Project: pyblish-win   Author: pyblish   File: test_hash.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_not_hashable(self):
        for obj in self.error_expected:
            self.assertNotIsInstance(obj, Hashable)


# Issue #4701: Check that some builtin types are correctly hashable
#  (This test only used to fail in Python 3.0, but has been included
#   in 2.x along with the lazy call to PyType_Ready in PyObject_Hash) 
Example 7
Project: neural-fingerprinting   Author: StephanZheng   File: attacks.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def construct_variables(self, kwargs):
        """
        Construct the inputs to the attack graph to be used by generate_np.

        :param kwargs: Keyword arguments to generate_np.
        :return: Structural and feedable arguments as well as a unique key
                 for the graph given these inputs.
        """
        # the set of arguments that are structural properties of the attack
        # if these arguments are different, we must construct a new graph
        fixed = dict(
            (k, v) for k, v in kwargs.items() if k in self.structural_kwargs)

        # the set of arguments that are passed as placeholders to the graph
        # on each call, and can change without constructing a new graph
        feedable = dict(
            (k, v) for k, v in kwargs.items() if k in self.feedable_kwargs)

        if len(fixed) + len(feedable) < len(kwargs):
            warnings.warn("Supplied extra keyword arguments that are not "
                          "used in the graph computation. They have been "
                          "ignored.")

        if not all(
                isinstance(value, collections.Hashable)
                for value in fixed.values()):
            # we have received a fixed value that isn't hashable
            # this means we can't cache this graph for later use,
            # and it will have to be discarded later
            hash_key = None
        else:
            # create a unique key for this set of fixed paramaters
            hash_key = tuple(sorted(fixed.items()))

        return fixed, feedable, hash_key 
Example 8
Project: neural-fingerprinting   Author: StephanZheng   File: attacks.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def construct_variables(self, kwargs):
        """
        Construct the inputs to the attack graph to be used by generate_np.

        :param kwargs: Keyword arguments to generate_np.
        :return: Structural and feedable arguments as well as a unique key
                 for the graph given these inputs.
        """
        # the set of arguments that are structural properties of the attack
        # if these arguments are different, we must construct a new graph
        fixed = dict(
            (k, v) for k, v in kwargs.items() if k in self.structural_kwargs)

        # the set of arguments that are passed as placeholders to the graph
        # on each call, and can change without constructing a new graph
        feedable = dict(
            (k, v) for k, v in kwargs.items() if k in self.feedable_kwargs)

        if len(fixed) + len(feedable) < len(kwargs):
            warnings.warn("Supplied extra keyword arguments that are not "
                          "used in the graph computation. They have been "
                          "ignored.")

        if not all(
                isinstance(value, collections.Hashable)
                for value in fixed.values()):
            # we have received a fixed value that isn't hashable
            # this means we can't cache this graph for later use,
            # and it will have to be discarded later
            hash_key = None
        else:
            # create a unique key for this set of fixed paramaters
            hash_key = tuple(sorted(fixed.items()))

        return fixed, feedable, hash_key 
Example 9
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: constructor.py    MIT License 5 votes vote down vote up
def construct_mapping(self, node, deep=False):
        if not isinstance(node, MappingNode):
            raise ConstructorError(None, None,
                    "expected a mapping node, but found %s" % node.id,
                    node.start_mark)
        mapping = {}
        for key_node, value_node in node.value:
            key = self.construct_object(key_node, deep=deep)
            if not isinstance(key, collections.Hashable):
                raise ConstructorError("while constructing a mapping", node.start_mark,
                        "found unhashable key", key_node.start_mark)
            value = self.construct_object(value_node, deep=deep)
            mapping[key] = value
        return mapping 
Example 10
Project: matchminer-engine   Author: dfci   File: schema.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def _validate_type_hashable(self, value):
        if isinstance(value, Hashable):
            return True 
Example 11
Project: thewarden   Author: pxsocs   File: decorators.py    MIT License 5 votes vote down vote up
def __call__(self, *args):
        if not isinstance(args, collections.Hashable):
            # uncacheable. a list, for instance.
            # better to not cache than blow up.
            return self.func(*args)
        if args in self.cache:
            return self.cache[args]
        else:
            value = self.func(*args)
            self.cache[args] = value
            return value 
Example 12
Project: recordlinkage   Author: J535D165   File: types.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def is_hashable(arg):
    """Return True if hash(arg) will succeed, False otherwise.

    Some types will pass a test against collections.Hashable but fail when they
    are actually hashed with hash().

    Distinguish between these and other types by trying the call to hash() and
    seeing if they raise TypeError.

    Examples
    --------
    >>> a = ([],)
    >>> isinstance(a, collections.Hashable)
    True
    >>> is_hashable(a)
    False
    """

    # unfortunately, we can't use isinstance(arg, collections.Hashable), which
    # can be faster than calling hash, because numpy scalars on Python 3 fail
    # this test

    # reconsider this decision once this numpy bug is fixed:
    # https://github.com/numpy/numpy/issues/5562

    try:
        hash(arg)
    except TypeError:
        return False
    else:
        return True 
Example 13
Project: jsview   Author: fab13n   File: __init__.py    MIT License 5 votes vote down vote up
def get(self, x):
        if isinstance(x, Hashable): return self.hashable_cache.get(x, None)
        else: return self.non_hashable_cache.get(id(x), None) 
Example 14
Project: jsview   Author: fab13n   File: __init__.py    MIT License 5 votes vote down vote up
def set(self, x, y):
        if isinstance(x, Hashable): self.hashable_cache[x] = y
        else: self.non_hashable_cache[id(x)] = y 
Example 15
Project: proteusisc   Author: diamondman   File: utils.py    GNU Lesser General Public License v2.1 5 votes vote down vote up
def __call__(self, *args):
      for arg in args:
         if not isinstance(arg, collections.Hashable):
            # uncacheable. a list, for instance.
            # better to not cache than blow up.
            return self.func(*args)
      if args in self.cache:
         return self.cache[args]
      else:
         value = self.func(*args)
         self.cache[args] = value
         return value 
Example 16
Project: LaserTOF   Author: kyleuckert   File: test_multiarray.py    MIT License 5 votes vote down vote up
def test_collections_hashable(self):
        x = np.array([])
        self.assertFalse(isinstance(x, collections.Hashable)) 
Example 17
Project: FX-RER-Value-Extraction   Author: tsKenneth   File: test_multiarray.py    MIT License 5 votes vote down vote up
def test_collections_hashable(self):
        x = np.array([])
        assert_(not isinstance(x, collections_abc.Hashable)) 
Example 18
Project: flask-login   Author: PowerMobileWeb   File: test_login.py    MIT License 5 votes vote down vote up
def test_hashable(self):
        self.assertTrue(isinstance(UserMixin(), collections.Hashable)) 
Example 19
Project: recruit   Author: Frank-qlu   File: test_multiarray.py    Apache License 2.0 5 votes vote down vote up
def test_collections_hashable(self):
        x = np.array([])
        assert_(not isinstance(x, collections_abc.Hashable)) 
Example 20
Project: holepunch   Author: CypherpunkArmory   File: general.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def __call__(self, *args):
        if not isinstance(args, collections.Hashable):
            # uncacheable. a list, for instance.
            # better to not cache than blow up.
            return self.func(*args)
        if args in self.cache:
            return self.cache[args]
        else:
            value = self.func(*args)
            self.cache[args] = value
            return value 
Example 21
Project: apk_api_key_extractor   Author: alessandrodd   File: memoized.py    Apache License 2.0 5 votes vote down vote up
def __call__(self, *args):
        if not isinstance(args, collections.Hashable):
            # uncacheable. a list, for instance.
            # better to not cache than blow up.
            return self.func(*args)
        if args in self.cache:
            return self.cache[args]
        else:
            value = self.func(*args)
            self.cache[args] = value
            return value 
Example 22
Project: basenji   Author: calico   File: basenji_fetch_app2.py    Apache License 2.0 5 votes vote down vote up
def __call__(self, *args):
        if not isinstance(args, collections.Hashable):
            # uncacheable. a list, for instance.
            # better to not cache than blow up.
            return self.func(*args)
        if args in self.cache:
            return self.cache[args]
        else:
            value = self.func(*args)
            self.cache[args] = value
            return value 
Example 23
Project: basenji   Author: calico   File: basenji_fetch_app1.py    Apache License 2.0 5 votes vote down vote up
def __call__(self, *args):
        if not isinstance(args, collections.Hashable):
            # uncacheable. a list, for instance.
            # better to not cache than blow up.
            return self.func(*args)
        if args in self.cache:
            return self.cache[args]
        else:
            value = self.func(*args)
            self.cache[args] = value
            return value 
Example 24
Project: basenji   Author: calico   File: basenji_fetch_app.py    Apache License 2.0 5 votes vote down vote up
def __call__(self, *args):
        if not isinstance(args, collections.Hashable):
            # uncacheable. a list, for instance.
            # better to not cache than blow up.
            return self.func(*args)
        if args in self.cache:
            return self.cache[args]
        else:
            value = self.func(*args)
            self.cache[args] = value
            return value 
Example 25
Project: picoCTF   Author: picoCTF   File: reqparse.py    MIT License 5 votes vote down vote up
def _handle_arg_type(arg, param):
    if isinstance(arg.type, Hashable) and arg.type in PY_TYPES:
        param["type"] = PY_TYPES[arg.type]
    elif hasattr(arg.type, "__apidoc__"):
        param["type"] = arg.type.__apidoc__["name"]
        param["in"] = "body"
    elif hasattr(arg.type, "__schema__"):
        param.update(arg.type.__schema__)
    elif arg.location == "files":
        param["type"] = "file"
    else:
        param["type"] = "string" 
Example 26
Project: assetQC   Author: david-cattermole   File: config.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __call__(self, *args):
        if not isinstance(args, collections.Hashable):
            # uncacheable. a list, for instance.
            # better to not cache than blow up.
            return self.func(*args)
        if args in self.cache:
            return self.cache[args]
        else:
            value = self.func(*args)
            self.cache[args] = value
            return value 
Example 27
Project: boogio   Author: Verizon   File: indexers.py    Apache License 2.0 5 votes vote down vote up
def index_by_method(forest, method):
    '''Build an index by values returned by applying a method to structures.

    Arguments:

        forest:
            A list of nested structures composed of lists, dicts and
            scalars.

        method:
            A method that returns a hashable type or a list of
            hashable types when applied to each member of ``forest``.

    Returns:

        (dict) A dictionary in which each key is a value returned by
        ``method`` in one or more of the elements of ``forest``, and
        in which each value is a list of all elements for which that
        value was returned either singly or as an element of a list.

    '''

    index = defaultdict(list)

    for tree in forest:
        result = method(tree)
        if isinstance(result, Hashable):
            result = [result]
        for key in result:
            index[key].append(tree)

    return dict(index)


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Example 28
Project: TFMTL   Author: felicitywang   File: qsub_mtl_jobs.py    Apache License 2.0 5 votes vote down vote up
def dict2hash(d):
    # Convert a dictionary into a hash
    # This should be unique up to key names
    contents = []
    for k, v in d.items():
        if isinstance(v, collections.Hashable):
            contents.append(v)
        elif type(v) == dict:
            contents.append(tuple([dict2hash(v)]))
    return hash(tuple(contents)) 
Example 29
Project: hexgen   Author: eranimo   File: util.py    GNU General Public License v3.0 5 votes vote down vote up
def __call__(self, *args):
        if not isinstance(args, collections.Hashable):
            # uncacheable. a list, for instance.
            # better to not cache than blow up.
            return self.func(*args)
        if args in self.cache:
            return self.cache[args]
        else:
            value = self.func(*args)
            self.cache[args] = value
        return value 
Example 30
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_multiarray.py    MIT License 5 votes vote down vote up
def test_collections_hashable(self):
        x = np.array([])
        assert_(not isinstance(x, collections_abc.Hashable)) 
Example 31
Project: FUTU_Stop_Loss   Author: BigtoC   File: inference.py    MIT License 5 votes vote down vote up
def is_hashable(obj):
    """Return True if hash(obj) will succeed, False otherwise.

    Some types will pass a test against collections.Hashable but fail when they
    are actually hashed with hash().

    Distinguish between these and other types by trying the call to hash() and
    seeing if they raise TypeError.

    Examples
    --------
    >>> a = ([],)
    >>> isinstance(a, collections.Hashable)
    True
    >>> is_hashable(a)
    False
    """
    # Unfortunately, we can't use isinstance(obj, collections.Hashable), which
    # can be faster than calling hash. That is because numpy scalars on Python
    # 3 fail this test.

    # Reconsider this decision once this numpy bug is fixed:
    # https://github.com/numpy/numpy/issues/5562

    try:
        hash(obj)
    except TypeError:
        return False
    else:
        return True 
Example 32
Project: ironpython2   Author: IronLanguages   File: test_hash.py    Apache License 2.0 5 votes vote down vote up
def test_hashable(self):
        objects = (self.default_expected +
                   self.fixed_expected)
        for obj in objects:
            self.assertIsInstance(obj, Hashable) 
Example 33
Project: ironpython2   Author: IronLanguages   File: test_hash.py    Apache License 2.0 5 votes vote down vote up
def test_not_hashable(self):
        for obj in self.error_expected:
            self.assertNotIsInstance(obj, Hashable)


# Issue #4701: Check that some builtin types are correctly hashable
#  (This test only used to fail in Python 3.0, but has been included
#   in 2.x along with the lazy call to PyType_Ready in PyObject_Hash) 
Example 34
Project: MARRtino-2.0   Author: DaniAffCH   File: test_multiarray.py    GNU General Public License v3.0 5 votes vote down vote up
def test_collections_hashable(self):
        x = np.array([])
        assert_(not isinstance(x, collections_abc.Hashable)) 
Example 35
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_multiarray.py    MIT License 5 votes vote down vote up
def test_collections_hashable(self):
        x = np.array([])
        self.assertFalse(isinstance(x, collections.Hashable)) 
Example 36
Project: perfwhiz   Author: cisco-oss-eng   File: umsgpack.py    Apache License 2.0 5 votes vote down vote up
def _unpack_map(code, fp):
    if (ord(code) & 0xf0) == 0x80:
        length = (ord(code) & ~0xf0)
    elif code == b'\xde':
        length = struct.unpack(">H", _read_except(fp, 2))[0]
    elif code == b'\xdf':
        length = struct.unpack(">I", _read_except(fp, 4))[0]
    else:
        raise Exception("logic error, not map: 0x%02x" % ord(code))

    d = {}
    for i in xrange(length):
        # Unpack key
        k = _unpack(fp)

        if isinstance(k, list):
            # Attempt to convert list into a hashable tuple
            k = _deep_list_to_tuple(k)
        elif not isinstance(k, collections.Hashable):
            raise UnhashableKeyException("encountered unhashable key: %s, %s" % (str(k), str(type(k))))
        elif k in d:
            raise DuplicateKeyException("encountered duplicate key: %s, %s" % (str(k), str(type(k))))

        # Unpack value
        v = _unpack(fp)

        try:
            d[k] = v
        except TypeError:
            raise UnhashableKeyException("encountered unhashable key: %s" % str(k))
    return d 
Example 37
Project: vnpy_crypto   Author: birforce   File: test_multiarray.py    MIT License 5 votes vote down vote up
def test_collections_hashable(self):
        x = np.array([])
        assert_(not isinstance(x, collections.Hashable)) 
Example 38
Project: vnpy_crypto   Author: birforce   File: inference.py    MIT License 5 votes vote down vote up
def is_hashable(obj):
    """Return True if hash(obj) will succeed, False otherwise.

    Some types will pass a test against collections.Hashable but fail when they
    are actually hashed with hash().

    Distinguish between these and other types by trying the call to hash() and
    seeing if they raise TypeError.

    Examples
    --------
    >>> a = ([],)
    >>> isinstance(a, collections.Hashable)
    True
    >>> is_hashable(a)
    False
    """
    # Unfortunately, we can't use isinstance(obj, collections.Hashable), which
    # can be faster than calling hash. That is because numpy scalars on Python
    # 3 fail this test.

    # Reconsider this decision once this numpy bug is fixed:
    # https://github.com/numpy/numpy/issues/5562

    try:
        hash(obj)
    except TypeError:
        return False
    else:
        return True 
Example 39
Project: indy-plenum   Author: hyperledger   File: util.py    Apache License 2.0 5 votes vote down vote up
def mostCommonElement(elements: Iterable[T], to_hashable_f: Callable=None):
    """
    Find the most frequent element of a collection.

    :param elements: An iterable of elements
    :param to_hashable_f: (optional) if defined will be used to get
        hashable presentation for non-hashable elements. Otherwise json.dumps
        is used with sort_keys=True
    :return: element which is the most frequent in the collection and
        the number of its occurrences
    """
    class _Hashable(collections.abc.Hashable):
        def __init__(self, orig):
            self.orig = orig

            if isinstance(orig, collections.Hashable):
                self.hashable = orig
            elif to_hashable_f is not None:
                self.hashable = to_hashable_f(orig)
            else:
                self.hashable = json.dumps(orig, sort_keys=True)

        def __eq__(self, other):
            return self.hashable == other.hashable

        def __hash__(self):
            return hash(self.hashable)

    _elements = (_Hashable(el) for el in elements)
    most_common, counter = Counter(_elements).most_common(n=1)[0]
    return most_common.orig, counter 
Example 40
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_functools.py    GNU General Public License v3.0 5 votes vote down vote up
def test_hash(self):
        def mycmp(x, y):
            return y - x
        key = self.cmp_to_key(mycmp)
        k = key(10)
        self.assertRaises(TypeError, hash, k)
        self.assertNotIsInstance(k, collections.Hashable) 
Example 41
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_hash.py    GNU General Public License v3.0 5 votes vote down vote up
def test_hashable(self):
        objects = (self.default_expected +
                   self.fixed_expected)
        for obj in objects:
            self.assertIsInstance(obj, Hashable) 
Example 42
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_hash.py    GNU General Public License v3.0 5 votes vote down vote up
def test_not_hashable(self):
        for obj in self.error_expected:
            self.assertNotIsInstance(obj, Hashable)


# Issue #4701: Check that some builtin types are correctly hashable 
Example 43
Project: ble5-nrf52-mac   Author: tomasero   File: test_multiarray.py    MIT License 5 votes vote down vote up
def test_collections_hashable(self):
        x = np.array([])
        assert_(not isinstance(x, collections_abc.Hashable)) 
Example 44
Project: sxapi   Author: wuttem   File: helper.py    MIT License 5 votes vote down vote up
def __call__(self, *args):
        if not isinstance(args, collections.Hashable):
            # uncacheable. a list, for instance.
            # better to not cache than blow up.
            return self.func(*args)
        if args in self.cache:
            return self.cache[args]
        else:
            value = self.func(*args)
            self.cache[args] = value
            return value 
Example 45
Project: dagian   Author: ianlini   File: frozen_dict.py    MIT License 5 votes vote down vote up
def recursively_froze(cls, value):
        if (isinstance(value, Mapping)
                and not isinstance(value, Hashable)):
            value = cls._recursively_froze_mapping(value)
        elif (isinstance(value, Sequence)
                and not isinstance(value, Hashable)):
            value = cls._recursively_froze_sequence(value)
        return value 
Example 46
Project: oss-ftp   Author: aliyun   File: test_hash.py    MIT License 5 votes vote down vote up
def test_hashable(self):
        objects = (self.default_expected +
                   self.fixed_expected)
        for obj in objects:
            self.assertIsInstance(obj, Hashable) 
Example 47
Project: oss-ftp   Author: aliyun   File: test_hash.py    MIT License 5 votes vote down vote up
def test_not_hashable(self):
        for obj in self.error_expected:
            self.assertNotIsInstance(obj, Hashable)


# Issue #4701: Check that some builtin types are correctly hashable
#  (This test only used to fail in Python 3.0, but has been included
#   in 2.x along with the lazy call to PyType_Ready in PyObject_Hash) 
Example 48
Project: poker   Author: surgebiswas   File: test_multiarray.py    MIT License 5 votes vote down vote up
def test_collections_hashable(self):
        x = np.array([])
        self.assertFalse(isinstance(x, collections.Hashable)) 
Example 49
Project: DDRL   Author: anonymous-author1   File: utils.py    Apache License 2.0 5 votes vote down vote up
def __call__(self, *args):
        if not isinstance(args, collections.Hashable):
            # uncacheable. a list, for instance.
            # better to not cache than blow up.
            return self.func(*args)
        if args in self.cache:
           return self.cache[args]
        else:
            value = self.func(*args)
            self.cache[args] = value
            return value 
Example 50
Project: httprunner   Author: httprunner   File: parser.py    Apache License 2.0 5 votes vote down vote up
def _convert_validators_to_mapping(validators):
    """ convert validators list to mapping.

    Args:
        validators (list): validators in list

    Returns:
        dict: validators mapping, use (check, comparator) as key.

    Examples:
        >>> validators = [
            {"check": "v1", "expect": 201, "comparator": "eq"},
            {"check": {"b": 1}, "expect": 200, "comparator": "eq"}
        ]
        >>> print(_convert_validators_to_mapping(validators))
        {
            ("v1", "eq"): {"check": "v1", "expect": 201, "comparator": "eq"},
            ('{"b": 1}', "eq"): {"check": {"b": 1}, "expect": 200, "comparator": "eq"}
        }

    """
    validators_mapping = {}

    for validator in validators:
        if not isinstance(validator["check"], collections.Hashable):
            check = json.dumps(validator["check"])
        else:
            check = validator["check"]

        key = (check, validator["comparator"])
        validators_mapping[key] = validator

    return validators_mapping 
Example 51
Project: aiohttp_apiset   Author: aamalev   File: loader.py    Apache License 2.0 5 votes vote down vote up
def construct_mapping(self, node, deep=False):
        if isinstance(node, MappingNode):
            self.flatten_mapping(node)
        mapping = FrozenDict()
        for key_node, value_node in node.value:
            key = self.construct_object(key_node, deep=deep)
            if not isinstance(key, Hashable):
                raise ConstructorError(
                    "while constructing a mapping", node.start_mark,
                    "found unhashable key", key_node.start_mark)
            value = self.construct_object(value_node, deep=deep)
            mapping[key] = value
        mapping.freeze()
        return mapping 
Example 52
Project: trax   Author: google   File: arrays_test.py    Apache License 2.0 5 votes vote down vote up
def testHash(self):
    a = t2a(tf.convert_to_tensor(value=10))
    self.assertNotIsInstance(a, collections.Hashable)
    with self.assertRaisesWithPredicateMatch(
        TypeError, r'unhashable type'):
      hash(a) 
Example 53
Project: P3_image_processing   Author: latedude2   File: test_multiarray.py    MIT License 5 votes vote down vote up
def test_collections_hashable(self):
        x = np.array([])
        assert_(not isinstance(x, collections_abc.Hashable)) 
Example 54
Project: citemap   Author: ai-se   File: lib.py    The Unlicense 5 votes vote down vote up
def __call__(self, *args):
    if not isinstance(args, collections.Hashable):
      # uncacheable. a list, for instance.
      # better to not cache than blow up.
      return self.func(*args)
    if args in self.cache:
      return self.cache[args]
    else:
      value = self.func(*args)
      self.cache[args] = value
      return value 
Example 55
Project: Joy_QA_Platform   Author: JoyMobileDevelopmentTeam   File: testcase.py    Apache License 2.0 5 votes vote down vote up
def _get_validators_mapping(validators):
    """ get validators mapping from api or test validators
    @param (list) validators:
        [
            {"check": "v1", "expect": 201, "comparator": "eq"},
            {"check": {"b": 1}, "expect": 200, "comparator": "eq"}
        ]
    @return
        {
            ("v1", "eq"): {"check": "v1", "expect": 201, "comparator": "eq"},
            ('{"b": 1}', "eq"): {"check": {"b": 1}, "expect": 200, "comparator": "eq"}
        }
    """
    validators_mapping = {}

    for validator in validators:
        validator = parse_validator(validator)

        if not isinstance(validator["check"], collections.Hashable):
            check = json.dumps(validator["check"])
        else:
            check = validator["check"]

        key = (check, validator["comparator"])
        validators_mapping[key] = validator

    return validators_mapping 
Example 56
Project: federated   Author: tensorflow   File: caching_executor.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, identifier, hashable_key, type_spec, target_future):
    """Creates a cached value.

    Args:
      identifier: An instance of `CachedValueIdentifier`.
      hashable_key: A hashable source value key, if any, or `None` of not
        applicable in this context, for use during cleanup.
      type_spec: The type signature of the target, an instance of `tff.Type`.
      target_future: An asyncio future that returns an instance of
        `executor_value_base.ExecutorValue` that represents a value embedded in
        the target executor.

    Raises:
      TypeError: If the arguments are of the wrong types.
    """
    py_typecheck.check_type(identifier, CachedValueIdentifier)
    py_typecheck.check_type(hashable_key, collections.Hashable)
    py_typecheck.check_type(type_spec, computation_types.Type)
    if not asyncio.isfuture(target_future):
      raise TypeError('Expected an asyncio future, got {}'.format(
          py_typecheck.type_string(type(target_future))))
    self._identifier = identifier
    self._hashable_key = hashable_key
    self._type_spec = type_spec
    self._target_future = target_future
    self._computed_result = None 
Example 57
Project: tg-chatdig   Author: gumblex   File: umsgpack.py    MIT License 5 votes vote down vote up
def _unpack_map(code, fp):
    if (ord(code) & 0xf0) == 0x80:
        length = (ord(code) & ~0xf0)
    elif code == b'\xde':
        length = struct.unpack(">H", _read_except(fp, 2))[0]
    elif code == b'\xdf':
        length = struct.unpack(">I", _read_except(fp, 4))[0]
    else:
        raise Exception("logic error, not map: 0x%02x" % ord(code))

    d = {}
    for i in range(length):
        # Unpack key
        k = _unpack(fp)

        if isinstance(k, list):
            # Attempt to convert list into a hashable tuple
            k = _deep_list_to_tuple(k)
        elif not isinstance(k, collections.Hashable):
            raise UnhashableKeyException("encountered unhashable key: %s, %s" % (str(k), str(type(k))))
        elif k in d:
            raise DuplicateKeyException("encountered duplicate key: %s, %s" % (str(k), str(type(k))))

        # Unpack value
        v = _unpack(fp)

        try:
            d[k] = v
        except TypeError:
            raise UnhashableKeyException("encountered unhashable key: %s" % str(k))
    return d 
Example 58
Project: DeepBayes   Author: deepgenerativeclassifier   File: attacks.py    MIT License 5 votes vote down vote up
def construct_variables(self, kwargs):
        """
        Construct the inputs to the attack graph to be used by generate_np.
        :param kwargs: Keyword arguments to generate_np.
        :return: Structural and feedable arguments as well as a unique key
                 for the graph given these inputs.
        """
        # the set of arguments that are structural properties of the attack
        # if these arguments are different, we must construct a new graph
        fixed = dict((k, v) for k, v in kwargs.items()
                     if k in self.structural_kwargs)

        # the set of arguments that are passed as placeholders to the graph
        # on each call, and can change without constructing a new graph
        feedable = dict((k, v) for k, v in kwargs.items()
                        if k in self.feedable_kwargs)

        if len(fixed) + len(feedable) < len(kwargs):
            warnings.warn("Supplied extra keyword arguments that are not "
                          "used in the graph computation. They have been "
                          "ignored.")

        if not all(isinstance(value, collections.Hashable)
                   for value in fixed.values()):
            # we have received a fixed value that isn't hashable
            # this means we can't cache this graph for later use,
            # and it will have to be discarded later
            hash_key = None
        else:
            # create a unique key for this set of fixed paramaters
            hash_key = tuple(sorted(fixed.items()))

        return fixed, feedable, hash_key 
Example 59
Project: numpy-ml   Author: ddbourgin   File: data_structures.py    GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, value):
        if self._encoder is not None:
            key = self._encoder(key)
        elif not isinstance(key, Hashable):
            key = tuple(key)
        super(Dict, self).__setitem__(key, value) 
Example 60
Project: numpy-ml   Author: ddbourgin   File: data_structures.py    GNU General Public License v3.0 5 votes vote down vote up
def __getitem__(self, key):
        self._key = copy.deepcopy(key)
        if self._encoder is not None:
            return self._encode_key(key)
        elif not isinstance(key, Hashable):
            key = tuple(key)
        return super(Dict, self).__getitem__(key) 
Example 61
Project: GraphicDesignPatternByPython   Author: Relph1119   File: constructor.py    MIT License 5 votes vote down vote up
def construct_mapping(self, node, deep=False):
        if not isinstance(node, MappingNode):
            raise ConstructorError(None, None,
                    "expected a mapping node, but found %s" % node.id,
                    node.start_mark)
        mapping = {}
        for key_node, value_node in node.value:
            key = self.construct_object(key_node, deep=deep)
            if not isinstance(key, collections.Hashable):
                raise ConstructorError("while constructing a mapping", node.start_mark,
                        "found unhashable key", key_node.start_mark)
            value = self.construct_object(value_node, deep=deep)
            mapping[key] = value
        return mapping 
Example 62
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_multiarray.py    MIT License 5 votes vote down vote up
def test_collections_hashable(self):
        x = np.array([])
        assert_(not isinstance(x, collections_abc.Hashable)) 
Example 63
Project: pandas_vectors   Author: richjoyce   File: pandas_vectors.py    MIT License 5 votes vote down vote up
def __init__(self, veclist):
        if not isinstance(veclist, collections.Hashable):
            # TODO: check that its valid?
            self._veclist = veclist
            return
        if veclist in _vector_lists.keys():
            self._veclist = _vector_lists[veclist]
            return 
Example 64
Project: Redy   Author: thautwarm   File: bytecode_api.py    MIT License 5 votes vote down vote up
def __setitem__(self, key, value):
        if isinstance(key, Hashable):
            try:
                self._dct[key] = value
                return
            except TypeError:
                pass

        asoc_lst = self._asoc_lst
        idx = next(
            (idx for idx, [fst, _] in enumerate(asoc_lst) if fst == key), None)
        if idx is None:
            asoc_lst.append([key, value])
            return
        asoc_lst[idx][1] = value 
Example 65
Project: Fireworks   Author: kellylab   File: message.py    MIT License 5 votes vote down vote up
def __contains__(self, key):
        """
        Returns true if the requested key is a column in this TensorMessage.
        """
        if isinstance(key, Hashable):
            return key in self.tensor_dict
        else:
            return False 
Example 66
Project: dirichletprocess   Author: tdhopper   File: dirichlet.py    MIT License 5 votes vote down vote up
def __init__(self, base_measure, alpha):
        if alpha <= 0:
            raise ValueError("alpha must be a positive number")
        if not hasattr(base_measure, '__call__'):
            raise ValueError("base_measure must be callable")
        if not isinstance(base_measure(), Hashable):
            raise ValueError("base_measure must return hashable object")
        self.base_measure = base_measure
        self.alpha = alpha

        self.cache = []
        self.weights = []
        self.total_stick_used = 0. 
Example 67
Project: predictive-maintenance-using-machine-learning   Author: awslabs   File: test_multiarray.py    Apache License 2.0 5 votes vote down vote up
def test_collections_hashable(self):
        x = np.array([])
        assert_(not isinstance(x, collections_abc.Hashable)) 
Example 68
Project: snorkel   Author: snorkel-team   File: core.py    Apache License 2.0 5 votes vote down vote up
def is_hashable(obj: Any) -> bool:
    """Test if object is hashable via duck typing.

    NB: not using ``collections.Hashable`` as some objects
    (e.g. pandas.Series) have a ``__hash__`` method to throw
    a more specific exception.
    """
    try:
        hash(obj)
        return True
    except Exception:
        return False 
Example 69
Project: snorkel   Author: snorkel-team   File: core.py    Apache License 2.0 5 votes vote down vote up
def get_hashable(obj: Any) -> Hashable:
    """Get a hashable version of a potentially unhashable object.

    This helper is used for caching mapper outputs of data points.
    For common data point formats (e.g. SimpleNamespace, pandas.Series),
    produces hashable representations of the values using a ``frozenset``.
    For objects like ``pandas.Series``, the name/index indentifier is dropped.

    Parameters
    ----------
    obj
        Object to get hashable version of

    Returns
    -------
    Hashable
        Hashable representation of object values

    Raises
    ------
    ValueError
        No hashable proxy for object
    """
    # If hashable already, just return
    if is_hashable(obj):
        return obj
    # Get dictionary from SimpleNamespace
    if isinstance(obj, SimpleNamespace):
        obj = vars(obj)
    # For dictionaries or pd.Series, construct a frozenset from items
    # Also recurse on values in case they aren't hashable
    if isinstance(obj, (dict, pd.Series)):
        return frozenset((k, get_hashable(v)) for k, v in obj.items())
    # For lists, recurse on values
    if isinstance(obj, (list, tuple)):
        return tuple(get_hashable(v) for v in obj)
    # For NumPy arrays, hash the byte representation of the data array
    if isinstance(obj, np.ndarray):
        return obj.data.tobytes()
    raise ValueError(f"Object {obj} has no hashing proxy.") 
Example 70
Project: magicinvoke   Author: haydenflinner   File: constructor.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def construct_mapping(self, node, deep=False):
        if not isinstance(node, MappingNode):
            raise ConstructorError(None, None,
                    "expected a mapping node, but found %s" % node.id,
                    node.start_mark)
        mapping = {}
        for key_node, value_node in node.value:
            key = self.construct_object(key_node, deep=deep)
            if not isinstance(key, collections.Hashable):
                raise ConstructorError("while constructing a mapping", node.start_mark,
                        "found unhashable key", key_node.start_mark)
            value = self.construct_object(value_node, deep=deep)
            mapping[key] = value
        return mapping 
Example 71
Project: recruit   Author: Frank-qlu   File: test_inference.py    Apache License 2.0 4 votes vote down vote up
def test_is_hashable():

    # all new-style classes are hashable by default
    class HashableClass(object):
        pass

    class UnhashableClass1(object):
        __hash__ = None

    class UnhashableClass2(object):

        def __hash__(self):
            raise TypeError("Not hashable")

    hashable = (1,
                3.14,
                np.float64(3.14),
                'a',
                tuple(),
                (1, ),
                HashableClass(), )
    not_hashable = ([], UnhashableClass1(), )
    abc_hashable_not_really_hashable = (([], ), UnhashableClass2(), )

    for i in hashable:
        assert inference.is_hashable(i)
    for i in not_hashable:
        assert not inference.is_hashable(i)
    for i in abc_hashable_not_really_hashable:
        assert not inference.is_hashable(i)

    # numpy.array is no longer collections.Hashable as of
    # https://github.com/numpy/numpy/pull/5326, just test
    # is_hashable()
    assert not inference.is_hashable(np.array([]))

    # old-style classes in Python 2 don't appear hashable to
    # collections.Hashable but also seem to support hash() by default
    if PY2:

        class OldStyleClass():
            pass

        c = OldStyleClass()
        assert not isinstance(c, compat.Hashable)
        assert inference.is_hashable(c)
        hash(c)  # this will not raise 
Example 72
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_inference.py    MIT License 4 votes vote down vote up
def test_is_hashable():

    # all new-style classes are hashable by default
    class HashableClass(object):
        pass

    class UnhashableClass1(object):
        __hash__ = None

    class UnhashableClass2(object):

        def __hash__(self):
            raise TypeError("Not hashable")

    hashable = (1,
                3.14,
                np.float64(3.14),
                'a',
                tuple(),
                (1, ),
                HashableClass(), )
    not_hashable = ([], UnhashableClass1(), )
    abc_hashable_not_really_hashable = (([], ), UnhashableClass2(), )

    for i in hashable:
        assert inference.is_hashable(i)
    for i in not_hashable:
        assert not inference.is_hashable(i)
    for i in abc_hashable_not_really_hashable:
        assert not inference.is_hashable(i)

    # numpy.array is no longer collections.Hashable as of
    # https://github.com/numpy/numpy/pull/5326, just test
    # is_hashable()
    assert not inference.is_hashable(np.array([]))

    # old-style classes in Python 2 don't appear hashable to
    # collections.Hashable but also seem to support hash() by default
    if PY2:

        class OldStyleClass():
            pass

        c = OldStyleClass()
        assert not isinstance(c, collections.Hashable)
        assert inference.is_hashable(c)
        hash(c)  # this will not raise 
Example 73
Project: vnpy_crypto   Author: birforce   File: test_inference.py    MIT License 4 votes vote down vote up
def test_is_hashable():

    # all new-style classes are hashable by default
    class HashableClass(object):
        pass

    class UnhashableClass1(object):
        __hash__ = None

    class UnhashableClass2(object):

        def __hash__(self):
            raise TypeError("Not hashable")

    hashable = (1,
                3.14,
                np.float64(3.14),
                'a',
                tuple(),
                (1, ),
                HashableClass(), )
    not_hashable = ([], UnhashableClass1(), )
    abc_hashable_not_really_hashable = (([], ), UnhashableClass2(), )

    for i in hashable:
        assert inference.is_hashable(i)
    for i in not_hashable:
        assert not inference.is_hashable(i)
    for i in abc_hashable_not_really_hashable:
        assert not inference.is_hashable(i)

    # numpy.array is no longer collections.Hashable as of
    # https://github.com/numpy/numpy/pull/5326, just test
    # is_hashable()
    assert not inference.is_hashable(np.array([]))

    # old-style classes in Python 2 don't appear hashable to
    # collections.Hashable but also seem to support hash() by default
    if PY2:

        class OldStyleClass():
            pass

        c = OldStyleClass()
        assert not isinstance(c, collections.Hashable)
        assert inference.is_hashable(c)
        hash(c)  # this will not raise 
Example 74
Project: dagian   Author: ianlini   File: data_definition.py    MIT License 4 votes vote down vote up
def eval_data_definition(self, args):
        # TODO: refactor
        # evaluate key
        if isinstance(self._key, Argument):
            raw_data_def = self._key.eval(args)
            if isinstance(raw_data_def, Mapping):
                new_keys = [raw_data_def['key']]
                new_args = [dict(raw_data_def.get('args', {}))]
            elif isinstance(raw_data_def, basestring):
                new_keys = [raw_data_def]
                new_args = [{}]
            elif isinstance(raw_data_def, Sequence):
                new_keys = []
                new_args = []
                for _raw_data_def in raw_data_def:
                    if isinstance(_raw_data_def, basestring):
                        new_key = _raw_data_def
                        new_arg = {}
                    else:
                        new_key = _raw_data_def['key']
                        new_arg = dict(_raw_data_def.get('args', {}))
                    new_keys.append(new_key)
                    new_args.append(new_arg)
            else:
                raise ValueError("Evaluated arguments only support dict, list or str.")
        elif isinstance(self._key, basestring):
            new_keys = [self._key.format(**args)]
            new_args = [{}]
        else:
            raise ValueError("RequirementDefinition.key can only be Argument or str.")

        # evaluate arguments
        for key, arg in six.viewitems(self._args._dict):
            if isinstance(arg, Argument):
                arg = arg.eval(args)
            elif isinstance(arg, basestring):
                arg = arg.format(**args)
            elif not isinstance(arg, Hashable):
                raise ValueError(
                    "The values in RequirementDefinition.args can only be Argument or hashable.")
            for new_arg in new_args:
                new_arg[key] = arg

        data_definitions = [DataDefinition(key, arg, self._name)
                            for key, arg in zip(new_keys, new_args)]
        return data_definitions 
Example 75
Project: ruamel-yaml   Author: jayvdb   File: constructor.py    MIT License 4 votes vote down vote up
def construct_mapping(self, node, deep=False):
        # type: (Any, bool) -> Any
        """deep is True when creating an object/mapping recursively,
        in that case want the underlying elements available during construction
        """
        if not isinstance(node, MappingNode):
            raise ConstructorError(
                None, None,
                "expected a mapping node, but found %s" % node.id,
                node.start_mark)
        total_mapping = {}
        if getattr(node, 'merge', None) is not None:
            todo = [(node.merge, False), (node.value, False)]
        else:
            todo = [(node.value, True)]
        for values, check in todo:
            mapping = {}  # type: Dict[Any, Any]
            for key_node, value_node in values:
                # keys can be list -> deep
                key = self.construct_object(key_node, deep=True)
                # lists are not hashable, but tuples are
                if not isinstance(key, collections.Hashable):
                    if isinstance(key, list):
                        key = tuple(key)
                if PY2:
                    try:
                        hash(key)
                    except TypeError as exc:
                        raise ConstructorError(
                            "while constructing a mapping", node.start_mark,
                            "found unacceptable key (%s)" %
                            exc, key_node.start_mark)
                else:
                    if not isinstance(key, collections.Hashable):
                        raise ConstructorError(
                            "while constructing a mapping", node.start_mark,
                            "found unhashable key", key_node.start_mark)

                value = self.construct_object(value_node, deep=deep)
                if check:
                    self.check_mapping_key(node, key_node, mapping, key, value)
                mapping[key] = value
            total_mapping.update(mapping)
        return total_mapping 
Example 76
Project: ruamel-yaml   Author: jayvdb   File: constructor.py    MIT License 4 votes vote down vote up
def construct_setting(self, node, typ, deep=False):
        # type: (Any, Any, bool) -> Any
        if not isinstance(node, MappingNode):
            raise ConstructorError(
                None, None,
                "expected a mapping node, but found %s" % node.id,
                node.start_mark)
        if node.comment:
            typ._yaml_add_comment(node.comment[:2])
            if len(node.comment) > 2:
                typ.yaml_end_comment_extend(node.comment[2], clear=True)
        if node.anchor:
            from ruamel.yaml.serializer import templated_id
            if not templated_id(node.anchor):
                typ.yaml_set_anchor(node.anchor)
        for key_node, value_node in node.value:
            # keys can be list -> deep
            key = self.construct_object(key_node, deep=True)
            # lists are not hashable, but tuples are
            if not isinstance(key, collections.Hashable):
                if isinstance(key, list):
                    key = tuple(key)
            if PY2:
                try:
                    hash(key)
                except TypeError as exc:
                    raise ConstructorError(
                        "while constructing a mapping", node.start_mark,
                        "found unacceptable key (%s)" %
                        exc, key_node.start_mark)
            else:
                if not isinstance(key, collections.Hashable):
                    raise ConstructorError(
                        "while constructing a mapping", node.start_mark,
                        "found unhashable key", key_node.start_mark)
            # construct but should be null
            value = self.construct_object(value_node, deep=deep)  # NOQA
            self.check_mapping_key(node, key_node, typ, key, value)
            if key_node.comment:
                typ._yaml_add_comment(key_node.comment, key=key)
            if value_node.comment:
                typ._yaml_add_comment(value_node.comment, value=key)
            typ.add(key) 
Example 77
Project: webserver   Author: cxsjclassroom   File: constructor.py    MIT License 4 votes vote down vote up
def construct_mapping(self, node, deep=False):
        # type: (Any, bool) -> Any
        """deep is True when creating an object/mapping recursively,
        in that case want the underlying elements available during construction
        """
        if not isinstance(node, MappingNode):
            raise ConstructorError(
                None, None,
                "expected a mapping node, but found %s" % node.id,
                node.start_mark)
        total_mapping = {}
        if getattr(node, 'merge', None) is not None:
            todo = [(node.merge, False), (node.value, False)]
        else:
            todo = [(node.value, True)]
        for values, check in todo:
            mapping = {}  # type: Dict[Any, Any]
            for key_node, value_node in values:
                # keys can be list -> deep
                key = self.construct_object(key_node, deep=True)
                # lists are not hashable, but tuples are
                if not isinstance(key, collections.Hashable):
                    if isinstance(key, list):
                        key = tuple(key)
                if PY2:
                    try:
                        hash(key)
                    except TypeError as exc:
                        raise ConstructorError(
                            "while constructing a mapping", node.start_mark,
                            "found unacceptable key (%s)" %
                            exc, key_node.start_mark)
                else:
                    if not isinstance(key, collections.Hashable):
                        raise ConstructorError(
                            "while constructing a mapping", node.start_mark,
                            "found unhashable key", key_node.start_mark)

                value = self.construct_object(value_node, deep=deep)
                if check:
                    self.check_mapping_key(node, key_node, mapping, key, value)
                mapping[key] = value
            total_mapping.update(mapping)
        return total_mapping 
Example 78
Project: webserver   Author: cxsjclassroom   File: constructor.py    MIT License 4 votes vote down vote up
def construct_setting(self, node, typ, deep=False):
        # type: (Any, Any, bool) -> Any
        if not isinstance(node, MappingNode):
            raise ConstructorError(
                None, None,
                "expected a mapping node, but found %s" % node.id,
                node.start_mark)
        if node.comment:
            typ._yaml_add_comment(node.comment[:2])
            if len(node.comment) > 2:
                typ.yaml_end_comment_extend(node.comment[2], clear=True)
        if node.anchor:
            from ruamel.yaml.serializer import templated_id
            if not templated_id(node.anchor):
                typ.yaml_set_anchor(node.anchor)
        for key_node, value_node in node.value:
            # keys can be list -> deep
            key = self.construct_object(key_node, deep=True)
            # lists are not hashable, but tuples are
            if not isinstance(key, collections.Hashable):
                if isinstance(key, list):
                    key = tuple(key)
            if PY2:
                try:
                    hash(key)
                except TypeError as exc:
                    raise ConstructorError(
                        "while constructing a mapping", node.start_mark,
                        "found unacceptable key (%s)" %
                        exc, key_node.start_mark)
            else:
                if not isinstance(key, collections.Hashable):
                    raise ConstructorError(
                        "while constructing a mapping", node.start_mark,
                        "found unhashable key", key_node.start_mark)
            # construct but should be null
            value = self.construct_object(value_node, deep=deep)  # NOQA
            self.check_mapping_key(node, key_node, typ, key, value)
            if key_node.comment:
                typ._yaml_add_comment(key_node.comment, key=key)
            if value_node.comment:
                typ._yaml_add_comment(value_node.comment, value=key)
            typ.add(key) 
Example 79
Project: federated   Author: tensorflow   File: caching_executor.py    Apache License 2.0 4 votes vote down vote up
def _get_hashable_key(value, type_spec):
  """Return a hashable key for value `value` of TFF type `type_spec`.

  Args:
    value: An argument to `create_value()`.
    type_spec: An optional type signature.

  Returns:
    A hashable key to use such that the same `value` always maps to the same
    key, and different ones map to different keys.

  Raises:
    TypeError: If there is no hashable key for this type of a value.
  """
  if isinstance(type_spec, computation_types.NamedTupleType):
    if isinstance(value, anonymous_tuple.AnonymousTuple):
      v_elem = anonymous_tuple.to_elements(value)
      t_elem = anonymous_tuple.to_elements(type_spec)
      r_elem = []
      for (_, vv), (tk, tv) in zip(v_elem, t_elem):
        r_elem.append((tk, _get_hashable_key(vv, tv)))
      return anonymous_tuple.AnonymousTuple(r_elem)
    else:
      return _get_hashable_key(anonymous_tuple.from_container(value), type_spec)
  elif isinstance(type_spec, computation_types.FederatedType):
    if type_spec.all_equal:
      return _get_hashable_key(value, type_spec.member)
    else:
      return tuple([_get_hashable_key(x, type_spec.member) for x in value])
  elif isinstance(value, pb.Computation):
    return str(value)
  elif isinstance(value, np.ndarray):
    return '<dtype={},shape={},items={}>'.format(value.dtype, value.shape,
                                                 value.flatten())
  elif (isinstance(value, collections.Hashable) and
        not isinstance(value, (tf.Tensor, tf.Variable))):
    # TODO(b/139200385): Currently Tensor and Variable returns True for
    #   `isinstance(value, collections.Hashable)` even when it's not hashable.
    #   Hence this workaround.
    return value
  else:
    return HashableWrapper(value) 
Example 80
Project: predictive-maintenance-using-machine-learning   Author: awslabs   File: test_inference.py    Apache License 2.0 4 votes vote down vote up
def test_is_hashable():

    # all new-style classes are hashable by default
    class HashableClass(object):
        pass

    class UnhashableClass1(object):
        __hash__ = None

    class UnhashableClass2(object):

        def __hash__(self):
            raise TypeError("Not hashable")

    hashable = (1,
                3.14,
                np.float64(3.14),
                'a',
                tuple(),
                (1, ),
                HashableClass(), )
    not_hashable = ([], UnhashableClass1(), )
    abc_hashable_not_really_hashable = (([], ), UnhashableClass2(), )

    for i in hashable:
        assert inference.is_hashable(i)
    for i in not_hashable:
        assert not inference.is_hashable(i)
    for i in abc_hashable_not_really_hashable:
        assert not inference.is_hashable(i)

    # numpy.array is no longer collections.Hashable as of
    # https://github.com/numpy/numpy/pull/5326, just test
    # is_hashable()
    assert not inference.is_hashable(np.array([]))

    # old-style classes in Python 2 don't appear hashable to
    # collections.Hashable but also seem to support hash() by default
    if PY2:

        class OldStyleClass():
            pass

        c = OldStyleClass()
        assert not isinstance(c, compat.Hashable)
        assert inference.is_hashable(c)
        hash(c)  # this will not raise