コード例 #1
0
def test_iloc_delitem_slice():
    temp = PriorityDict(enumerate(string.lowercase))
    that = list(enumerate(string.lowercase))
    del temp.iloc[5:20:3]
    del that[5:20:3]
    assert temp.items() == that
    temp._check()
コード例 #2
0
def test_isub_small():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(10))
    temp -= that
    assert all(temp[val] == 0 for val in range(10))
    assert all(temp[val] == val for val in range(10, 100))
    temp._check()
コード例 #3
0
def test_len():
    temp = PriorityDict()
    assert len(temp) == 0
    val = dict((letter, pos) for pos, letter in enumerate(string.lowercase))
    temp.update(val)
    assert len(temp) == 26
    temp._check()
コード例 #4
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_isub_small():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(10))
    temp -= that
    assert all(temp[val] == 0 for val in range(10))
    assert all(temp[val] == val for val in range(10, 100))
    temp._check()
コード例 #5
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_iloc_delitem_slice():
    temp = PriorityDict(enumerate(string.lowercase))
    that = list(enumerate(string.lowercase))
    del temp.iloc[5:20:3]
    del that[5:20:3]
    assert temp.items() == that
    temp._check()
コード例 #6
0
def dijkstra(data, source, sink):
    visit = {}
    dis_dict = defaultdict(lambda: math.inf)
    dis_dict[source] = 0
    expand_queue = PriorityDict()
    expand_queue[source] = 0
    # distance = 0
    while len(expand_queue) > 0:
        source = expand_queue.smallest()
        i = expand_queue[source]
        # if i != distance:
        #     print("distance", i)
        # distance = i
        expand_queue.pop_smallest()
        visit[source] = i
        if source == sink:
            return i
        if source in data:
            # print("source", source, " in data", "neighbours", len(data[source]))
            for s in data.get(source):
                if s not in visit or visit[s] > i + 1:
                    if s not in expand_queue or i + 1 < expand_queue[s]:
                        expand_queue[s] = i + 1

    return math.inf
コード例 #7
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_clean():
    temp = PriorityDict((val, num) for num, val in enumerate(string.lowercase))
    assert len(temp) == 26
    temp.clean()
    temp._check()
    assert len(temp) == 25
    temp.clean(10)
    temp._check()
    assert len(temp) == 15
コード例 #8
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_iand_big():
    temp_vals = list((val, rand(100)) for val in range(100))
    that_vals = list((val, rand(100)) for val in range(100))
    temp = PriorityDict(temp_vals)
    that = PriorityDict(that_vals)
    temp &= that
    assert all(temp[pos] == min(temp_vals[pos][1], that_vals[pos][1])
               for pos in range(100))
    temp._check()
コード例 #9
0
def test_iand_big():
    temp_vals = list((val, rand(100)) for val in range(100))
    that_vals = list((val, rand(100)) for val in range(100))
    temp = PriorityDict(temp_vals)
    that = PriorityDict(that_vals)
    temp &= that
    assert all(temp[pos] == min(temp_vals[pos][1], that_vals[pos][1])
               for pos in range(100))
    temp._check()
コード例 #10
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_gt():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(100))
    that[50] = -50
    assert temp > that
    del that[0]
    assert temp > that
    del temp[1]
    assert not (temp > that)
コード例 #11
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_and():
    temp_vals = list((val, rand(100)) for val in range(25, 75))
    that_vals = list((val, rand(100)) for val in range(100))
    temp = PriorityDict(temp_vals)
    that = PriorityDict(that_vals)
    other = temp & that
    other._check()
    assert len(other) == 50
    assert all(other[pos] == min(temp_vals[pos - 25][1], that_vals[pos][1])
               for pos in range(25, 75))
コード例 #12
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_le():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(100))
    assert temp <= that
    temp[50] = -50
    assert temp <= that
    del temp[0]
    assert temp <= that
    del that[1]
    assert not (temp <= that)
コード例 #13
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_iloc_delitem():
    temp = PriorityDict(enumerate(string.lowercase))
    that = list(enumerate(string.lowercase))

    while len(temp) > 0:
        pos = rand(len(temp))
        del that[pos]
        del temp.iloc[pos]
        assert temp.items() == that
        temp._check()
コード例 #14
0
def test_iloc_delitem():
    temp = PriorityDict(enumerate(string.lowercase))
    that = list(enumerate(string.lowercase))

    while len(temp) > 0:
        pos = rand(len(temp))
        del that[pos]
        del temp.iloc[pos]
        assert temp.items() == that
        temp._check()
コード例 #15
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_ior_small():
    temp_vals = list((val, rand(100)) for val in range(100))
    that_vals = list((val, rand(100)) for val in range(10))
    temp = PriorityDict(temp_vals)
    that = PriorityDict(that_vals)
    temp |= that
    assert all(temp[pos] == max(temp_vals[pos][1], that_vals[pos][1])
               for pos in range(10))
    assert all(temp[pos] == temp_vals[pos][1] for pos in range(10, 100))
    temp._check()
コード例 #16
0
def test_ior_small():
    temp_vals = list((val, rand(100)) for val in range(100))
    that_vals = list((val, rand(100)) for val in range(10))
    temp = PriorityDict(temp_vals)
    that = PriorityDict(that_vals)
    temp |= that
    assert all(temp[pos] == max(temp_vals[pos][1], that_vals[pos][1])
               for pos in range(10))
    assert all(temp[pos] == temp_vals[pos][1]
               for pos in range(10, 100))
    temp._check()
コード例 #17
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_or():
    temp_vals = list((val, rand(100)) for val in range(100))
    that_vals = list((val, rand(100)) for val in range(50))
    temp = PriorityDict(temp_vals)
    that = PriorityDict(that_vals)
    other = temp | that
    other._check()
    assert len(other) == 100
    assert all(other[pos] == max(temp_vals[pos][1], that_vals[pos][1])
               for pos in range(50))
    assert all(other[pos] == temp_vals[pos][1] for pos in range(50, 100))
コード例 #18
0
def test_clean():
    temp = PriorityDict((val, num) for num, val in enumerate(string.lowercase))
    assert len(temp) == 26
    temp.clean()
    temp._check()
    assert len(temp) == 25
    temp.clean(10)
    temp._check()
    assert len(temp) == 15
コード例 #19
0
ファイル: dijkstra.py プロジェクト: sushanttripathy/graphene
    def calculate_shortest_paths_from(self, source_id):
        distance = {}
        previous = {}
        q = None
        if self.use_priority_queue:
            q = PriorityDict()
        else:
            q = []
        ind = []
        distance[source_id] = 0
        for x in self.nodes:
            if x is not source_id:
                distance[x] = float("inf")
                previous[x] = None
            if self.use_priority_queue:
                q[x] = distance[x]
            else:
                q.append(x)

        while len(q):
            u = None
            if self.use_priority_queue:
                u = q.pop_smallest()
            else:
                u = self.get_node_with_minimum_distance(q, distance, ind)
                index = ind.pop()
                if type(index) is int:
                    del q[index]
                else:
                    break

            if isinstance(self.edges[u], dict):
                for v in self.edges[u]:
                    if v in q:
                        alt = distance[u] + self.edges[u][v].strength
                        if alt < distance[v]:
                            distance[v] = alt
                            previous[v] = u
                            if self.use_priority_queue:
                                q[v] = distance[v]
        if not self.num_threads:
            self.shortest_paths[source_id] = distance
            self.shortest_paths_guide[source_id] = previous
        else:
            self.lock.acquire()
            self.shortest_paths[source_id] = distance
            self.shortest_paths_guide[source_id] = previous
            self.lock.release()
        return
コード例 #20
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_setdefault():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert temp.setdefault('d', -1) == 3
    assert temp.setdefault('blah', 10) == 10
    temp._check()
    assert len(temp) == 27
    assert temp['k'] == 10
    temp._check()
コード例 #21
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_copy():
    this = PriorityDict({'a': 0, 'b': 1, 'c': 2, 'd': 3})
    that = this.copy()
    del this['d']
    assert len(that) == 4
    this._check()
    that._check()
コード例 #22
0
def test_setdefault():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert temp.setdefault('d', -1) == 3
    assert temp.setdefault('blah', 10) == 10
    temp._check()
    assert len(temp) == 27
    assert temp['k'] == 10
    temp._check()
コード例 #23
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_len():
    temp = PriorityDict()
    assert len(temp) == 0
    val = dict((letter, pos) for pos, letter in enumerate(string.lowercase))
    temp.update(val)
    assert len(temp) == 26
    temp._check()
コード例 #24
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_iloc_getitem():
    temp = PriorityDict(enumerate(string.lowercase))
    for pos, letter in enumerate(string.lowercase):
        assert temp[temp.iloc[pos]] == letter
    temp._check()
コード例 #25
0
def test_pop():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert temp.pop('c') == 2
    temp._check()
    assert temp.pop('blah', -1) == -1
    temp._check()
コード例 #26
0
def test_clear():
    temp = PriorityDict(enumerate(string.lowercase))
    assert len(temp) == 26
    temp.clear()
    assert len(temp) == 0
    temp._check()
コード例 #27
0
def test_iloc_getitem():
    temp = PriorityDict(enumerate(string.lowercase))
    for pos, letter in enumerate(string.lowercase):
        assert temp[temp.iloc[pos]] == letter
    temp._check()
コード例 #28
0
def test_tally():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    temp.tally(list(string.lowercase))
    for pos, key in enumerate(string.lowercase):
        assert temp[key] == (pos + 1)
    temp._check()
コード例 #29
0
def test_elements():
    temp = PriorityDict({'a': 3, 'b': 2, 'c': 1})
    assert ['c', 'b', 'b', 'a', 'a', 'a'] == list(temp.elements())
コード例 #30
0
def test_get():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert temp.get('a') == 0
    assert temp.get('y') == 24
    assert temp.get('blah') == None
    temp._check()
コード例 #31
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_bisect_right():
    temp = PriorityDict((val, (val / 10) * 10) for val in range(100))
    for val in range(100):
        assert temp.bisect_right(val) == ((val + 10) / 10) * 10
コード例 #32
0
def test_reversed():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert list(reversed(temp)) == list(reversed(string.lowercase))
    temp._check()
コード例 #33
0
def test_update_big():
    temp = PriorityDict((val, pos) for pos, val in enumerate(string.lowercase))
    temp.update((val, -pos) for pos, val in enumerate(string.lowercase[5:25]))
    for pos, val in enumerate(string.lowercase[5:25]):
        assert temp[val] == -pos
コード例 #34
0
def test_iter():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert list(iter(temp)) == list(string.lowercase)
    temp._check()
コード例 #35
0
def test_index():
    temp = PriorityDict((val, pos) for pos, val in enumerate(string.lowercase))
    for key in string.lowercase:
        pos = temp.index(key)
        assert temp.iloc[pos] == key
コード例 #36
0
def test_index_keyerror():
    temp = PriorityDict((val, pos) for pos, val in enumerate(string.lowercase))
    temp.index('aa')
コード例 #37
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_iter():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert list(iter(temp)) == list(string.lowercase)
    temp._check()
コード例 #38
0
def test_delitem():
    temp = PriorityDict(enumerate(string.lowercase))
    del temp[13]
    assert len(temp) == 25
    temp._check()
コード例 #39
0
def test_bisect_right():
    temp = PriorityDict((val, (val / 10) * 10) for val in range(100))
    for val in range(100):
        assert temp.bisect_right(val) == ((val + 10) / 10) * 10
コード例 #40
0
def test_contains():
    temp = PriorityDict(enumerate(string.lowercase))
    assert all(pos in temp for pos in range(len(string.lowercase)))
    assert 26 not in temp
    assert not (-1 in temp)
    temp._check()
コード例 #41
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_iadd_big():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(100))
    temp += that
    assert all(temp[val] == 2 * val for val in range(100))
    temp._check()
コード例 #42
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_ne():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(100))
    assert not (temp != that)
    that[50] = -50
    assert temp != that
コード例 #43
0
def test_most_common():
    temp = PriorityDict({'a': 1, 'b': 2, 'c': 3, 'd': 4})
    assert temp.most_common() == [('d', 4), ('c', 3), ('b', 2), ('a', 1)]
    assert temp.most_common(2) == [('d', 4), ('c', 3)]
    temp._check()
コード例 #44
0
def test_pop_error():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    temp.pop('blah')
コード例 #45
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_contains():
    temp = PriorityDict(enumerate(string.lowercase))
    assert all(pos in temp for pos in range(len(string.lowercase)))
    assert 26 not in temp
    assert not (-1 in temp)
    temp._check()
コード例 #46
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_isdisjoint():
    temp = PriorityDict((val, val) for val in range(50))
    that = PriorityDict((val, val) for val in range(50, 100))
    assert temp.isdisjoint(that)
コード例 #47
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_most_common():
    temp = PriorityDict({'a': 1, 'b': 2, 'c': 3, 'd': 4})
    assert temp.most_common() == [('d', 4), ('c', 3), ('b', 2), ('a', 1)]
    assert temp.most_common(2) == [('d', 4), ('c', 3)]
    temp._check()
コード例 #48
0
def test_isdisjoint():
    temp = PriorityDict((val, val) for val in range(50))
    that = PriorityDict((val, val) for val in range(50, 100))
    assert temp.isdisjoint(that)
コード例 #49
0
def test_popitem():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert (temp.popitem() == ('z', 25))
    assert (temp.popitem() == ('y', 24))
    assert (temp.popitem(0) == ('a', 0))
    temp._check()
コード例 #50
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_tally():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    temp.tally(list(string.lowercase))
    for pos, key in enumerate(string.lowercase):
        assert temp[key] == (pos + 1)
    temp._check()
コード例 #51
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_delitem():
    temp = PriorityDict(enumerate(string.lowercase))
    del temp[13]
    assert len(temp) == 25
    temp._check()
コード例 #52
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_count():
    seq = list((val, pos) for pos, val in enumerate(string.lowercase))
    pd = PriorityDict.count(val for val, pos in seq for num in range(pos))
    c = Counter(val for val, pos in seq for num in range(pos))
    assert pd == c
コード例 #53
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_iloc():
    temp = PriorityDict(enumerate(string.lowercase))
    assert len(temp.iloc) == len(string.lowercase)
コード例 #54
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_update_big():
    temp = PriorityDict((val, pos) for pos, val in enumerate(string.lowercase))
    temp.update((val, -pos) for pos, val in enumerate(string.lowercase[5:25]))
    for pos, val in enumerate(string.lowercase[5:25]):
        assert temp[val] == -pos
コード例 #55
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_iloc_getitem_slice():
    temp = PriorityDict(enumerate(string.lowercase))
    that = list(enumerate(string.lowercase))
    assert temp.iloc[5:20:3] == [5, 8, 11, 14, 17]
コード例 #56
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_index():
    temp = PriorityDict((val, pos) for pos, val in enumerate(string.lowercase))
    for key in string.lowercase:
        pos = temp.index(key)
        assert temp.iloc[pos] == key
コード例 #57
0
def test_has_key():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert temp.has_key('r')
    temp._check()
コード例 #58
0
ファイル: test_coverage.py プロジェクト: afcarl/prioritydict
def test_index_keyerror():
    temp = PriorityDict((val, pos) for pos, val in enumerate(string.lowercase))
    temp.index('aa')
コード例 #59
0
def test_count():
    seq = list((val, pos) for pos, val in enumerate(string.lowercase))
    pd = PriorityDict.count(val for val, pos in seq for num in range(pos))
    c = Counter(val for val, pos in seq for num in range(pos))
    assert pd == c
コード例 #60
0
def test_iadd_big():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(100))
    temp += that
    assert all(temp[val] == 2 * val for val in range(100))
    temp._check()