Python more_itertools.consecutive_groups() Examples

The following are 13 code examples of more_itertools.consecutive_groups(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module more_itertools , or try the search function .
Example #1
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
def test_exotic_ordering(self):
        iterable = [
            ('a', 'b', 'c', 'd'),
            ('a', 'c', 'b', 'd'),
            ('a', 'c', 'd', 'b'),
            ('a', 'd', 'b', 'c'),
            ('d', 'b', 'c', 'a'),
            ('d', 'c', 'a', 'b'),
        ]
        ordering = list(permutations('abcd')).index
        actual = [list(g) for g in mi.consecutive_groups(iterable, ordering)]
        expected = [
            [('a', 'b', 'c', 'd')],
            [('a', 'c', 'b', 'd'), ('a', 'c', 'd', 'b'), ('a', 'd', 'b', 'c')],
            [('d', 'b', 'c', 'a'), ('d', 'c', 'a', 'b')],
        ]
        self.assertEqual(actual, expected) 
Example #2
Source File: test_more.py    From pipenv with MIT License 6 votes vote down vote up
def test_exotic_ordering(self):
        iterable = [
            ('a', 'b', 'c', 'd'),
            ('a', 'c', 'b', 'd'),
            ('a', 'c', 'd', 'b'),
            ('a', 'd', 'b', 'c'),
            ('d', 'b', 'c', 'a'),
            ('d', 'c', 'a', 'b'),
        ]
        ordering = list(permutations('abcd')).index
        actual = [list(g) for g in mi.consecutive_groups(iterable, ordering)]
        expected = [
            [('a', 'b', 'c', 'd')],
            [('a', 'c', 'b', 'd'), ('a', 'c', 'd', 'b'), ('a', 'd', 'b', 'c')],
            [('d', 'b', 'c', 'a'), ('d', 'c', 'a', 'b')],
        ]
        self.assertEqual(actual, expected) 
Example #3
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def test_exotic_ordering(self):
        iterable = [
            ('a', 'b', 'c', 'd'),
            ('a', 'c', 'b', 'd'),
            ('a', 'c', 'd', 'b'),
            ('a', 'd', 'b', 'c'),
            ('d', 'b', 'c', 'a'),
            ('d', 'c', 'a', 'b'),
        ]
        ordering = list(permutations('abcd')).index
        actual = [list(g) for g in mi.consecutive_groups(iterable, ordering)]
        expected = [
            [('a', 'b', 'c', 'd')],
            [('a', 'c', 'b', 'd'), ('a', 'c', 'd', 'b'), ('a', 'd', 'b', 'c')],
            [('d', 'b', 'c', 'a'), ('d', 'c', 'a', 'b')],
        ]
        self.assertEqual(actual, expected) 
Example #4
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_numbers(self):
        iterable = [-10, -8, -7, -6, 1, 2, 4, 5, -1, 7]
        actual = [list(g) for g in mi.consecutive_groups(iterable)]
        expected = [[-10], [-8, -7, -6], [1, 2], [4, 5], [-1], [7]]
        self.assertEqual(actual, expected) 
Example #5
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_custom_ordering(self):
        iterable = ['1', '10', '11', '20', '21', '22', '30', '31']
        ordering = lambda x: int(x)
        actual = [list(g) for g in mi.consecutive_groups(iterable, ordering)]
        expected = [['1'], ['10', '11'], ['20', '21', '22'], ['30', '31']]
        self.assertEqual(actual, expected) 
Example #6
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_numbers(self):
        iterable = [-10, -8, -7, -6, 1, 2, 4, 5, -1, 7]
        actual = [list(g) for g in mi.consecutive_groups(iterable)]
        expected = [[-10], [-8, -7, -6], [1, 2], [4, 5], [-1], [7]]
        self.assertEqual(actual, expected) 
Example #7
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_custom_ordering(self):
        iterable = ['1', '10', '11', '20', '21', '22', '30', '31']
        ordering = lambda x: int(x)
        actual = [list(g) for g in mi.consecutive_groups(iterable, ordering)]
        expected = [['1'], ['10', '11'], ['20', '21', '22'], ['30', '31']]
        self.assertEqual(actual, expected) 
Example #8
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
def test_numbers(self):
        iterable = [-10, -8, -7, -6, 1, 2, 4, 5, -1, 7]
        actual = [list(g) for g in mi.consecutive_groups(iterable)]
        expected = [[-10], [-8, -7, -6], [1, 2], [4, 5], [-1], [7]]
        self.assertEqual(actual, expected) 
Example #9
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
def test_custom_ordering(self):
        iterable = ['1', '10', '11', '20', '21', '22', '30', '31']
        ordering = lambda x: int(x)
        actual = [list(g) for g in mi.consecutive_groups(iterable, ordering)]
        expected = [['1'], ['10', '11'], ['20', '21', '22'], ['30', '31']]
        self.assertEqual(actual, expected) 
Example #10
Source File: utils.py    From ssbio with MIT License 5 votes vote down vote up
def label_sequential_regions(inlist):
    """Input a list of labeled tuples and return a dictionary of sequentially labeled regions.

    Args:
        inlist (list): A list of tuples with the first number representing the index and the second the index label.

    Returns:
        dict: Dictionary of labeled regions.

    Examples:

        >>> label_sequential_regions([(1, 'O'), (2, 'O'), (3, 'O'), (4, 'M'), (5, 'M'), (6, 'I'), (7, 'M'), (8, 'O'), (9, 'O')])
        {'O1': [1, 2, 3], 'M1': [4, 5], 'I1': [6], 'M2': [7], 'O2': [8, 9]}

    """
    import more_itertools as mit

    df = pd.DataFrame(inlist).set_index(0)

    labeled = {}
    for label in df[1].unique():
        iterable = df[df[1] == label].index.tolist()
        labeled.update({'{}{}'.format(label, i + 1): items for i, items in
                        enumerate([list(group) for group in mit.consecutive_groups(iterable)])})

    return labeled 
Example #11
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_numbers(self):
        iterable = [-10, -8, -7, -6, 1, 2, 4, 5, -1, 7]
        actual = [list(g) for g in mi.consecutive_groups(iterable)]
        expected = [[-10], [-8, -7, -6], [1, 2], [4, 5], [-1], [7]]
        self.assertEqual(actual, expected) 
Example #12
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_custom_ordering(self):
        iterable = ['1', '10', '11', '20', '21', '22', '30', '31']
        ordering = lambda x: int(x)
        actual = [list(g) for g in mi.consecutive_groups(iterable, ordering)]
        expected = [['1'], ['10', '11'], ['20', '21', '22'], ['30', '31']]
        self.assertEqual(actual, expected) 
Example #13
Source File: wallet_data_models.py    From dash-masternode-tool with MIT License 4 votes vote down vote up
def update_utxos(self, utxos_to_add: List[UtxoType], utxos_to_update: List[UtxoType], utxos_to_delete: List[Tuple[int, int]]):
        if utxos_to_delete:
            row_indexes_to_remove = []
            for utxo_id in utxos_to_delete:
                utxo = self.utxo_by_id.get(utxo_id)
                if utxo:
                    utxo_index = self.utxos.index(utxo)
                    if utxo_index not in row_indexes_to_remove:
                        row_indexes_to_remove.append(utxo_index)
                    del self.utxo_by_id[utxo_id]
            row_indexes_to_remove.sort(reverse=True)

            for group in consecutive_groups(row_indexes_to_remove, ordering=lambda x: -x):
                l = list(group)
                self.beginRemoveRows(QModelIndex(), l[-1], l[0]) # items are sorted in reversed order
                del self.utxos[l[-1]: l[0]+1]
                self.endRemoveRows()

        if utxos_to_add:
            # in the model, the rows are sorted by the number of confirmations in the descending order, so put
            # the new ones in the right place

            # filter out the already existing utxos
            utxos_to_add_verified = []
            for utxo in utxos_to_add:
                if utxo.id not in self.utxo_by_id:
                    utxos_to_add_verified.append(utxo)

            utxos_to_add_verified.sort(key=lambda x: x.block_height, reverse=True)
            row_idx = 0
            self.beginInsertRows(QModelIndex(), row_idx, row_idx + len(utxos_to_add_verified) - 1)
            try:
                for index, utxo in enumerate(utxos_to_add_verified):
                    if utxo.id not in self.utxo_by_id:
                        self.add_utxo(utxo, index)
            finally:
                self.endInsertRows()

        if utxos_to_update:
            for utxo_new in utxos_to_update:
                utxo = self.utxo_by_id.get(utxo_new.id)
                if utxo:
                    utxo.block_height = utxo_new.block_height  # block_height is the only field that can be updated
                    utxo_index = self.utxos.index(utxo)
                    ui_index = self.index(utxo_index, 0)
                    self.dataChanged.emit(ui_index, ui_index)