Exemple #1
0
 def test_accumulate(self):
     self.assertEqual(
         [1, 3, 6, 10, 15],
         list(iters.accumulate([1, 2, 3, 4, 5]))
     )
     self.assertEqual(
         [1, 2, 6, 24, 120],
         list(iters.accumulate([1, 2, 3, 4, 5], operator.mul))
     )
Exemple #2
0
def nd_matching_path_wt(G, path, dist):
    '''given a path, and a graph, find the node which is *dist* distance along the path.
    if no such path exists, return the surrounding nodes. '''
    pathinfo = starmap(G.get_edge_data, slider(path, 2))
    weights = map(_['weight'], pathinfo)
    acc_weights =  list(accumulate(weights))
    if dist not in acc_weights:
        #then it is between two nodes
        idx=ilen(takewhile(_<dist, acc_weights))
        return path[idx], path[idx+1]
    return path[acc_weights.index(dist)]
    def on_path_change(self, event):
        notify('on_path_change:', event)
        event_path = os.path.abspath(event.path)
        nodes = {'/' + p for p in accumulate(event_path.split(os.sep),
                                             lambda acc, x: os.path.join(acc, x))}

        indices_to_paths = enumerate(os.path.abspath(p) for p in self.absolute_paths)
        index, _ = next(ifilter(lambda (i, p): p in nodes, indices_to_paths))

        for f, sect, items in _parse_files(_extract_files_from_paths([event_path])):
            changes = self._sections[sect].set_layer((index, event_path), dict(items))
            for k, v in changes.iteritems():
                for path in self.ALL, sect, (sect, k):
                    if path in self.listeners:
                        for listener in self.listeners[path]:
                            listener(sect, k, v)
Exemple #4
0
    def on_path_change(self, event):
        notify('on_path_change:', event)
        event_path = os.path.abspath(event.path)
        nodes = {
            '/' + p
            for p in accumulate(event_path.split(os.sep),
                                lambda acc, x: os.path.join(acc, x))
        }

        indices_to_paths = enumerate(
            os.path.abspath(p) for p in self.absolute_paths)
        index, _ = next(ifilter(lambda (i, p): p in nodes, indices_to_paths))

        for f, sect, items in _parse_files(
                _extract_files_from_paths([event_path])):
            changes = self._sections[sect].set_layer((index, event_path),
                                                     dict(items))
            for k, v in changes.iteritems():
                for path in self.ALL, sect, (sect, k):
                    if path in self.listeners:
                        for listener in self.listeners[path]:
                            listener(sect, k, v)
Exemple #5
0
def rolling_sum(elem_min, elem_max, length):
    return st.lists(
        st.integers(min_value=elem_min, max_value=elem_max),
        max_size=length,
        min_size=length).map(lambda xs: accumulate(xs, operator.add))
Exemple #6
0
def rolling_sum(elem_min, elem_max, length):
    return st.lists(
        st.integers(min_value=elem_min,max_value=elem_max), max_size=length, min_size=length
    ).map(lambda xs: accumulate(xs, operator.add))
Exemple #7
0
def most_even_chunk(string, group):
    """Divide a string into a list of strings as even as possible."""
    counts = [0] + most_even(len(string), group)
    indices = accumulate(counts)
    slices = window(indices, 2)
    return [string[slice(*one)] for one in slices]