Exemple #1
0
    def test_assoc_in(self):
        D, kw = self.D, self.kw
        assert assoc_in(D({"a": 1}), ["a"], 2, **kw) == D({"a": 2})
        assert (assoc_in(D({"a": D({"b": 1})}), ["a", "b"], 2, **kw) ==
                D({"a": D({"b": 2})}))
        assert assoc_in(D({}), ["a", "b"], 1, **kw) == D({"a": D({"b": 1})})

        # Verify immutability:
        d = D({'x': 1})
        oldd = d
        d2 = assoc_in(d, ['x'], 2, **kw)
        assert d is oldd
        assert d2 is not oldd
    def test_assoc_in(self):
        D, kw = self.D, self.kw
        assert assoc_in(D({"a": 1}), ["a"], 2, **kw) == D({"a": 2})
        assert (assoc_in(D({"a": D({"b": 1})}), ["a", "b"], 2,
                         **kw) == D({"a": D({"b": 2})}))
        assert assoc_in(D({}), ["a", "b"], 1, **kw) == D({"a": D({"b": 1})})

        # Verify immutability:
        d = D({'x': 1})
        oldd = d
        d2 = assoc_in(d, ['x'], 2, **kw)
        assert d is oldd
        assert d2 is not oldd
Exemple #3
0
def explode(d, keys):
    values = get_in(keys, d)
    if isinstance(values, list):
        for v in values:
            yield assoc_in(d, keys, v)
    else:
        yield d
Exemple #4
0
def _update_state(state, action):
    new_worker_pos = _move_projection_tile(state['worker']['pos'], action)
    if new_worker_pos is not None:
        state = tzd.assoc_in(state, ['worker', 'pos'], new_worker_pos)

    # without joining on example 2, 500 iters: 22.36s
    # with joining on example 2, 500 iters: 3.87s
    new_wrapped_shells = state['wrapped_shells'] + [_pt2shell(state['worker']['pos'])]
    wrapped = shapely.ops.unary_union([shapely.geometry.Polygon(sh)
                                       for sh in new_wrapped_shells])
    wrapped_simple = wrapped.simplify(0.01)
    return tzd.assoc(state, 'wrapped_shells', _polygon2shells(wrapped_simple))
Exemple #5
0
def _predict_action(state):
    mine = shapely.geometry.Polygon(state['desc']['mine_shell'])
    obstacles = [shapely.geometry.Polygon(sh) for sh in state['desc']['obstacle_shells']]
    obstacle = shapely.ops.unary_union(obstacles)
    situable = mine.difference(obstacle)
    wrappeds = [shapely.geometry.Polygon(sh) for sh in state['wrapped_shells']]
    wrapped = shapely.ops.unary_union(wrappeds)
    not_wrapped = situable.difference(wrapped)

    if not_wrapped.area < 1.0:
        return None, state

    last_move = state.get('last_move', 'W')
    for move in [last_move, 'W', 'S', 'A', 'D']:
        proj = _move_projection_center(state['worker']['pos'], move)
        if not_wrapped.contains(proj):
            return move, tzd.dissoc(state, 'path_pts_to_not_wrapped')

    if not state.get('path_pts_to_not_wrapped'):
        target_tile = tzf.thread_first(not_wrapped.representative_point(),
                                       _shapely_point2pt,
                                       _snap_to_tile)
        print('Finding shortest path from tile {} to {}'.format(state['worker']['pos'], target_tile))

        if tzd.get_in(['cache', 'incidence_m'], state) is None:
            incidence_m = _incidence_matrix(situable)
            state = tzd.assoc_in(state, ['cache', 'incidence_m'], incidence_m)
        else:
            incidence_m = state['cache']['incidence_m']

        target_vertex_ind = _incidence_ind(target_tile[0], target_tile[1], x_size=math.ceil(situable.bounds[2]))
        path_dists, path_predecessors = sp.sparse.csgraph.shortest_path(csgraph=incidence_m,
                                                                        directed=False,
                                                                        return_predecessors=True,
                                                                        unweighted=True,
                                                                        indices=target_vertex_ind)
        start_vertex_ind = _incidence_ind(state['worker']['pos'][0],
                                          state['worker']['pos'][1],
                                          x_size=math.ceil(situable.bounds[2]))

        path_inds = _path_inds(path_predecessors, start_vertex_ind)
        path_pts = [_incidence_pt(ind, x_size=math.ceil(situable.bounds[2]))
                    for ind in path_inds]
        print('Found path: {}'.format(path_pts))
        state = tzd.assoc(state, 'path_pts_to_not_wrapped', path_pts)

    path_move = _projection_pt_move(state['worker']['pos'], state['path_pts_to_not_wrapped'][0])
    if path_move is not None:
        return path_move, tzd.update_in(state, ['path_pts_to_not_wrapped'], lambda p: p[1:])

    return 'Z', state
Exemple #6
0
def validated_schedule(
        schedule: dict,
        getters: t.Optional[t.List[dict]] = None,
) -> dict:
    """Validates ``schedule`` with schemas and returns modified
    schedule dict if some getters provided.

    Getters are special functions that can be attached to get some
    schedule value from external source by key-path::

        getters = [
            {
                'getter': payload_getter,
                'params': {
                    'path': ['start', 'relative_timeshift', 'delay'],
                    'source': payload_source,
                }
            }
        ]

    In this case, :func:`payload_getter` will receive value by ``path``
    from the ``payload_source``. And ``schedule`` will be modified with
    the result from ``payload_getter`` by the same path.
    """
    if getters:
        getters = GettersSchema(getters)
        for getter in getters:
            modifier = getter['getter']
            params = getter.get('params', {})
            path_to_value = params.get('path')
            if path_to_value:
                path_value = get_in(path_to_value, schedule)
                if path_value is not None:
                    val = modifier(path_value, **params)
                    schedule = assoc_in(schedule, path_to_value, val)

    relative_params = (
        get_in(['periodical', 'relative_day'], schedule) and
        get_in(['periodical', 'relative_day_index'], schedule)
    )
    if relative_params:
        schedule = RelativeScheduleSchema(schedule)
    else:
        schedule = ScheduleSchema(schedule)

    return schedule
Exemple #7
0
def add_word_data(word, key, value):
    freq = word_frequency(word, value['business_ids'])
    return assoc_in(value, ['word_data', word], freq)
Exemple #8
0
def filter_paths(item, paths):
    filtered_item = {}
    for key_path in paths:
        value = get_in(key_path, item)
        filtered_item = assoc_in(filtered_item, key_path, value)
    return filtered_item
Exemple #9
0
def block_member(library_data: object, email: str) -> object:
	return assoc_in(library_data, ['userManagement', 'members', email, 'isBlocked'], True)
Exemple #10
0
def update_feature_maps(model_name, scoring_fn, item):
    return assoc_in(item, ['feature_maps', model_name], scoring_fn(item))