예제 #1
0
def compare_all_but(dict_a: t.Mapping,
                    dict_b: t.Mapping,
                    keys_to_ignore: t.Optional[t.Iterable] = None) -> bool:
    """
    Compare two dictionaries, with the possibility to ignore some fields.

    :arg dict_a: First dictionary to compare
    :arg dict_b: Second dictionary to compare
    :kwarg keys_to_ignore: An iterable of keys whose values in the dictionaries will not be
        compared.
    :returns: True if the dictionaries have matching values for all of the keys which were not
        ignored.  False otherwise.
    """
    if keys_to_ignore is None:
        return dict_a == dict_b

    if not isinstance(keys_to_ignore, Set):
        keys_to_ignore = frozenset(keys_to_ignore)

    length_a = len(frozenset(dict_a.keys()) - keys_to_ignore)
    length_b = len(frozenset(dict_b.keys()) - keys_to_ignore)

    if length_a != length_b:
        return False

    sentinel = object()

    for key, value in ((k, v) for k, v in dict_a.items()
                       if k not in keys_to_ignore):
        if value != dict_b.get(key, sentinel):
            return False

    return True
예제 #2
0
def replace_qubits(
    circuit: cirq.Circuit,
    qubit_map: typing.Mapping
):
    old_qubits = set(qubit_map.keys())
    new_qubits = set(qubit_map.values())
    if len(old_qubits) != len(new_qubits):
        raise ValueError(
            "`qubit_map` must be a bijective mapping."
        )

    qubits_in_circuit = circuit.all_qubits()
    unknown_old_qubits = old_qubits - qubits_in_circuit
    if unknown_old_qubits:
        raise ValueError(
            "Some qubits in `old_qubits` do not exist in the original circuit: "
            f"{unknown_old_qubits}"
        )

    new_circuit = cirq.Circuit()
    for moment in circuit:
        new_moment = cirq.Moment()
        for operation in moment:
            new_operation = operation.gate.on(
                *(qubit_map[q] for q in operation.qubits)
            )
            new_moment += new_operation
        new_circuit += new_moment

    return new_circuit
예제 #3
0
파일: konch.py 프로젝트: brl0/konch
 def update(self, d: typing.Mapping) -> None:  # type: ignore
     for key in d.keys():
         # Shallow-merge context
         if key == "context":
             self["context"].update(Config.transform_val(d["context"]))
         else:
             self[key] = d[key]
예제 #4
0
    def oauth2client_from_k8s(cls, obj: typing.Mapping) -> api_pb2.Client:
        keep_keys = set(cls.OAuth2Client.DESCRIPTOR.fields_by_name.keys())
        for key in list(obj.keys()):
            if key not in keep_keys:
                obj.pop(key)

        return cls.OAuth2Client(**obj)
예제 #5
0
파일: konch.py 프로젝트: sloria/konch
 def update(self, d: typing.Mapping) -> None:  # type: ignore
     for key in d.keys():
         # Shallow-merge context
         if key == "context":
             self["context"].update(Config.transform_val(d["context"]))
         else:
             self[key] = d[key]
예제 #6
0
파일: utils.py 프로젝트: nndii/payu_fake
async def calc_hash(data: typing.Mapping, secret: str) -> str:
    sorted_keys = sorted((key for key in data.keys() if key != 'ORDER_HASH'))

    body = ''
    for key in sorted_keys:
        value = str(data[key])
        body += str(len(value.encode('utf8'))) + value

    hash_calc = hmac.HMAC(key=secret.encode('utf-8'),
                          msg=body.encode('utf-8'),
                          digestmod=hashlib.md5).hexdigest()

    return hash_calc
예제 #7
0
def sorted_dict(value: typing.Mapping) -> typing.Any:
    """
    Sorts a dict's keys to avoid leaking information about the
    backend's handling of unordered dicts.
    """
    if isinstance(value, typing.Mapping):
        return OrderedDict(
            (key, sorted_dict(value[key])) for key in sorted(value.keys()))

    elif isinstance(value, typing.Sequence) and not isinstance(value, str):
        return list(map(sorted_dict, value))

    else:
        return value
예제 #8
0
    def iter(
        self, value: typing.Mapping
    ) -> typing.Generator[typing.Tuple[str, typing.Any], None, None]:
        """
        Iterator version of :py:meth:`apply`.
        """
        if value is not None:
            # Apply filtered values first.
            for key, filter_chain in self._filters.items():
                if key in value:
                    yield key, self._apply_item(key, value[key], filter_chain)

                elif self._missing_key_allowed(key):
                    # Filter the missing value as if it was set to
                    # ``None``.
                    yield key, self._apply_item(key, None, filter_chain)

                else:
                    # Treat the missing value as invalid.
                    yield key, self._invalid_value(
                        value=None,
                        reason=self.CODE_MISSING_KEY,
                        sub_key=key,
                    )

            # Extra values go last.
            # Note that we iterate in sorted order, in case the result
            # type preserves ordering.
            # https://github.com/eflglobal/filters/issues/13
            for key in sorted(set(value.keys()) - set(self._filters.keys())):
                if self._extra_key_allowed(key):
                    yield key, value[key]
                else:
                    unicode_key = self.unicodify_key(key)

                    # Handle the extra value just like any other
                    # invalid value, but do not include it in the
                    # result (note that there is no ``yield`` here).
                    self._invalid_value(
                        value=value[key],
                        reason=self.CODE_EXTRA_KEY,
                        sub_key=unicode_key,

                        # https://github.com/eflglobal/filters/issues/15
                        template_vars={
                            'actual_key': unicode_key,
                        },
                    )
예제 #9
0
def serialize_object(object_: t.Mapping,
                     signing: bool = False,
                     terminate: bool = True) -> bytes:
    fields = [FIELDS_BY_NAME[name] for name in object_.keys()]
    fields = [
        field for field in fields
        if field['isSerialized'] and (not signing or field['isSigningField'])
    ]
    fields = sorted(fields, key=field_key)

    blob = bytearray()
    for field in fields:
        blob.extend(serialize_field(field, object_[field['name']]))
    if terminate:
        blob.extend(OBJECT_END_MARKER)
    return bytes(blob)
예제 #10
0
    def saturate_mapping(self, value: t.Mapping) -> None:
        try:
            assert isinstance(value, t.Mapping), "must be a mapping"
            assert value, "must not be empty"
            assert dict_depth(value) == 1, "must be a mapping of depth 1"
            assert all(isinstance(_, int) for _ in value.values()), "must have int values"
        except AssertionError as e:
            raise LayerMisconfigured("saturate_mapping {}, got {}".format(*e.args, value))

        if any(isinstance(_, str) for _ in value.keys()):
            _ = {}
            for k, v in value.items():
                _[int(k)] = v
            value = _

        self._saturate_mapping = dict(sorted(value.items(), key=lambda i: i[0]))
예제 #11
0
def associate_data(root_map: typing.Mapping,
                   *args: typing.Mapping) -> typing.List[typing.List]:
    """
    Convert a number of maps key->value to a list of lists
    [[key, map1[key], map2[key] map3[key] ...] ...]

    The list will be sorted in key order
    Returned inner lists will be in the same order as they are passed as arguments.

    The first map passed is considered the reference point for the list of keys,
    :param root_map: The first map to associate
    :param args: Additional maps to associate to the first one
    :return:
    """
    if len(args) <= 0:
        # Nothing to associate, flatten the root map and return
        return sorted([k, v] for k, v in root_map.items())
    root_keys = set(root_map.keys())
    all_same = True
    # First, check if all the maps have the same list of keys
    for other_map in args:
        if set(other_map.keys()) != root_keys:
            all_same = False
            break
    if all_same:
        # All the maps have the same set of keys, just flatten them
        return sorted([key, root_map[key]] +
                      [other_map[key] for other_map in args]
                      for key in root_keys)
    else:
        # We need to associate the maps, the timestamps are a little out
        rekeyed_maps = []
        for other_map in args:
            matches = ass.associate(root_map,
                                    other_map,
                                    offset=0,
                                    max_difference=3)
            rekeyed_map = {
                root_key: other_map[other_key]
                for root_key, other_key in matches
            }
            root_keys &= set(rekeyed_map.keys())
            rekeyed_maps.append(rekeyed_map)
        return sorted([key, root_map[key]] +
                      [rekeyed_map[key] for rekeyed_map in rekeyed_maps]
                      for key in root_keys)
예제 #12
0
 def from_dict(cls, dikt: typing.Mapping) -> 'About':
     dikt = dict(zip((x.strip('__') for x in dikt.keys()), dikt.values()))
     sig = inspect.signature(cls)
     return cls(**sig.bind(
         **{x: y
            for x, y in dikt.items() if x in sig.parameters}).arguments)
예제 #13
0
def sort_dict(d: typing.Mapping) -> collections.OrderedDict:
    result = collections.OrderedDict()
    for key in sorted(list(d.keys())):
        result[key] = d[key]

    return result
예제 #14
0
def is_synapse_annotations(annotations: typing.Mapping):
    """Tests if the given object is a Synapse-style Annotations object."""
    if not isinstance(annotations, collections.abc.Mapping):
        return False
    return annotations.keys() >= {'id', 'etag', 'annotations'}
예제 #15
0
def mis_keys(d: typ.Mapping) -> typ.Generator[typ.Any, None, None]:
    yield from d.keys()
def keys_as(cast: ty.Callable[[ty.Iterable], ty.Iterable],
            mapping: ty.Mapping) -> ty.Iterable:

    return cast(mapping.keys())