Python more_itertools.collapse() Examples

The following are 26 code examples of more_itertools.collapse(). 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 pipenv with MIT License 5 votes vote down vote up
def test_collapse(self):
        l = [[1], 2, [[3], 4], [[[5]]]]
        self.assertEqual(list(mi.collapse(l)), [1, 2, 3, 4, 5]) 
Example #2
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_collapse_to_list(self):
        l = (1, [2], (3, [4, (5,)], 'ab'))
        actual = list(mi.collapse(l, base_type=list))
        expected = [1, [2], 3, [4, (5,)], 'ab']
        self.assertEqual(actual, expected) 
Example #3
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_collapse_to_level(self):
        l = [[1], 2, [[3], 4], [[[5]]]]
        self.assertEqual(list(mi.collapse(l, levels=2)), [1, 2, 3, 4, [5]])
        self.assertEqual(
            list(mi.collapse(mi.collapse(l, levels=1), levels=1)),
            list(mi.collapse(l, levels=2))
        ) 
Example #4
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_collapse_flatten(self):
        l = [[1], [2], [[3], 4], [[[5]]]]
        self.assertEqual(list(mi.collapse(l, levels=1)), list(mi.flatten(l))) 
Example #5
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_collapse_to_string(self):
        l = [["s1"], "s2", [["s3"], "s4"], [[["s5"]]]]
        self.assertEqual(list(mi.collapse(l)), ["s1", "s2", "s3", "s4", "s5"]) 
Example #6
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_collapse(self):
        l = [[1], 2, [[3], 4], [[[5]]]]
        self.assertEqual(list(mi.collapse(l)), [1, 2, 3, 4, 5]) 
Example #7
Source File: test_files.py    From hypha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_get_files(self):
        project = ProjectFactory()

        files = list(get_files(project))

        self.assertTrue(all(issubclass(f.__class__, File) for f in files))

        fields = project.submission.form_data.values()
        fields = collapse(fields, base_type=StreamFieldFile)
        fields = [f for f in fields if isinstance(f, StreamFieldFile)]

        self.assertEqual(len(files), len(fields))

        for f in files:
            self.assertIn(f, fields) 
Example #8
Source File: dataProcessing.py    From HierarchicalAttentionNetworksForDocumentClassification with MIT License 5 votes vote down vote up
def _index(self, xs):
    """
    desc: apply index to text data and persist unique vocabulary in dataset to pickle file
    args:
      xs: text data 
    returns:
      list of test, train data after it was indexed, the lookup table for the vocabulary,
      and any persisted variables that may be needed
    """
    def _apply_index(txt_data):
      indexed = [[[unqVoc_LookUp[char] for char in seq] for seq in doc] for doc in txt_data]
      return indexed
    # end

    x_train, x_test = xs

    # create look up table for all unique vocab in test and train datasets
    unqVoc = set(list(more_itertools.collapse(x_train[:] + x_test[:])))
    unqVoc_LookUp = {k: v+1 for v, k in enumerate(unqVoc)}
    vocab_size = len(list(unqVoc_LookUp))

    x_train = _apply_index(txt_data=x_train)
    x_test = _apply_index(txt_data=x_test)

    # determine max sequence lengths
    max_seq_len = max([len(seq) for seq in itertools.chain.from_iterable(x_train + x_test)]) # max length of sequence across all documents
    max_sent_len = max([len(sent) for sent in (x_train + x_test)]) # max length of sentence across all documents
   
    persisted_vars = {"max_seq_len":max_seq_len,
                      "max_sent_len":max_sent_len,
                      "vocab_size":vocab_size}

    return [x_train, x_test, unqVoc_LookUp, persisted_vars]
  # end 
Example #9
Source File: terminal.py    From pytest with MIT License 5 votes vote down vote up
def _write_report_lines_from_hooks(
        self, lines: List[Union[str, List[str]]]
    ) -> None:
        lines.reverse()
        for line in collapse(lines):
            self.write_line(line) 
Example #10
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
def test_collapse_to_list(self):
        l = (1, [2], (3, [4, (5,)], 'ab'))
        actual = list(mi.collapse(l, base_type=list))
        expected = [1, [2], 3, [4, (5,)], 'ab']
        self.assertEqual(actual, expected) 
Example #11
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
def test_collapse_to_level(self):
        l = [[1], 2, [[3], 4], [[[5]]]]
        self.assertEqual(list(mi.collapse(l, levels=2)), [1, 2, 3, 4, [5]])
        self.assertEqual(
            list(mi.collapse(mi.collapse(l, levels=1), levels=1)),
            list(mi.collapse(l, levels=2))
        ) 
Example #12
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
def test_collapse_flatten(self):
        l = [[1], [2], [[3], 4], [[[5]]]]
        self.assertEqual(list(mi.collapse(l, levels=1)), list(mi.flatten(l))) 
Example #13
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
def test_collapse_to_string(self):
        l = [["s1"], "s2", [["s3"], "s4"], [[["s5"]]]]
        self.assertEqual(list(mi.collapse(l)), ["s1", "s2", "s3", "s4", "s5"]) 
Example #14
Source File: terminal.py    From python-netsurv with MIT License 5 votes vote down vote up
def _write_report_lines_from_hooks(self, lines):
        lines.reverse()
        for line in collapse(lines):
            self.write_line(line) 
Example #15
Source File: functions.py    From OpenKiwi with GNU Affero General Public License v3.0 5 votes vote down vote up
def confusion_matrix(hat_y, y, n_classes=None):
    hat_y = np.array(list(collapse(hat_y)))
    y = np.array(list(collapse(y)))

    if n_classes is None:
        classes = np.unique(np.union1d(hat_y, y))
        n_classes = len(classes)

    cnfm = np.zeros((n_classes, n_classes))
    for j in range(y.shape[0]):
        cnfm[y[j], hat_y[j]] += 1
    return cnfm 
Example #16
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_collapse_to_list(self):
        l = (1, [2], (3, [4, (5,)], 'ab'))
        actual = list(mi.collapse(l, base_type=list))
        expected = [1, [2], 3, [4, (5,)], 'ab']
        self.assertEqual(actual, expected) 
Example #17
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_collapse_to_level(self):
        l = [[1], 2, [[3], 4], [[[5]]]]
        self.assertEqual(list(mi.collapse(l, levels=2)), [1, 2, 3, 4, [5]])
        self.assertEqual(
            list(mi.collapse(mi.collapse(l, levels=1), levels=1)),
            list(mi.collapse(l, levels=2))
        ) 
Example #18
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_collapse_to_bytes(self):
        l = [[b"s1"], b"s2", [[b"s3"], b"s4"], [[[b"s5"]]]]
        self.assertEqual(
            list(mi.collapse(l)), [b"s1", b"s2", b"s3", b"s4", b"s5"]
        ) 
Example #19
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_collapse_to_string(self):
        l = [["s1"], "s2", [["s3"], "s4"], [[["s5"]]]]
        self.assertEqual(list(mi.collapse(l)), ["s1", "s2", "s3", "s4", "s5"]) 
Example #20
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_collapse(self):
        l = [[1], 2, [[3], 4], [[[5]]]]
        self.assertEqual(list(mi.collapse(l)), [1, 2, 3, 4, 5]) 
Example #21
Source File: terminal.py    From python-netsurv with MIT License 5 votes vote down vote up
def _write_report_lines_from_hooks(self, lines):
        lines.reverse()
        for line in collapse(lines):
            self.write_line(line) 
Example #22
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_collapse_to_list(self):
        l = (1, [2], (3, [4, (5,)], 'ab'))
        actual = list(mi.collapse(l, base_type=list))
        expected = [1, [2], 3, [4, (5,)], 'ab']
        self.assertEqual(actual, expected) 
Example #23
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_collapse_flatten(self):
        l = [[1], [2], [[3], 4], [[[5]]]]
        self.assertEqual(list(mi.collapse(l, levels=1)), list(mi.flatten(l))) 
Example #24
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_collapse_to_bytes(self):
        l = [[b"s1"], b"s2", [[b"s3"], b"s4"], [[[b"s5"]]]]
        self.assertEqual(
            list(mi.collapse(l)), [b"s1", b"s2", b"s3", b"s4", b"s5"]
        ) 
Example #25
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_collapse_to_string(self):
        l = [["s1"], "s2", [["s3"], "s4"], [[["s5"]]]]
        self.assertEqual(list(mi.collapse(l)), ["s1", "s2", "s3", "s4", "s5"]) 
Example #26
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_collapse(self):
        l = [[1], 2, [[3], 4], [[[5]]]]
        self.assertEqual(list(mi.collapse(l)), [1, 2, 3, 4, 5])