Example #1
0
def test_table_str_and_repr(monkeypatch):
    mock = Mock()
    mock.get_terminal_size().columns = 6
    monkeypatch.setattr(table, 'shutil', mock)

    r = Row({'a': 1, 'b': 2})
    t = Table([r, r])
    expected = '  a    b\n---  ---\n  1    2\n  1    2'

    assert str(t) == expected
    assert repr(t) == expected
    # parse html representation with pandas
    html = pd.read_html(t._repr_html_())[0]
    assert html.to_dict(orient='list') == {'a': [1, 1], 'b': [2, 2]}
Example #2
0
    def status(self, **kwargs):
        """Returns a table with tasks status
        """
        # FIXME: delete this, make dag.render() return this
        self.render()

        return Table([self[name].status(**kwargs) for name in self])
Example #3
0
    def status(self, clear_cached_status=False, **kwargs):
        """Returns a table with tasks status
        """
        if clear_cached_status:
            self._clear_cached_outdated_status()

        self.render()

        return Table([self._G.nodes[name]['task'].status(**kwargs)
                      for name in self._G])
Example #4
0
def test_table_auto_size(monkeypatch):
    TerminalSize = namedtuple('TerminalSize', ['columns'])
    monkeypatch.setattr(shutil, 'get_terminal_size', lambda: TerminalSize(80))

    r = Row({'a': '1' * 60, 'b': '1' * 60})
    table = Table([r, r], column_width='auto')

    assert max([len(line) for line in str(table).splitlines()]) == 80

    # simulate resize
    monkeypatch.setattr(shutil, 'get_terminal_size', lambda: TerminalSize(120))
    assert max([len(line) for line in str(table).splitlines()]) == 120
Example #5
0
def _list_examples(path):
    with open(path / 'index.csv', newline='', encoding='utf-8-sig') as f:
        rows = list(csv.DictReader(f))

    by_type = defaultdict(lambda: [])

    for row in rows:
        type_ = row.pop('type')
        del row['entry']
        by_type[type_].append(row)

    tw = TerminalWriter()

    tw.sep('=', 'Ploomber examples', blue=True)

    for type_ in ['basic', 'intermediate', 'advanced']:
        tw.sep(' ', type_.capitalize(), green=True)
        print(Table.from_dicts(by_type[type_]).to_format('simple'))

    tw.sep('=', blue=True)

    tw.write(f'\nTo run these examples in a hosted environment: {_URL}\n')
    tw.write('\nTo copy locally: ploomber examples -n {name}\n\n')
Example #6
0
def test_convert_to_dict():
    d = {'a': 1, 'b': 2}
    r = Row(d)
    t = Table([r, r], column_width=None)
    assert t.to_dict() == {'a': [1, 1], 'b': [2, 2]}
Example #7
0
def test_convert_to_pandas():
    d = {'a': 1, 'b': 2}
    r = Row(d)
    t = Table([r, r], column_width=None)
    expected = pd.DataFrame({'a': [1, 1], 'b': [2, 2]})
    assert expected.equals(t.to_pandas())
Example #8
0
def test_table_values():
    d = {'a': 1, 'b': 2}
    r = Row(d)
    t = Table([r, r], column_width=None)
    assert t.values == {'a': [1, 1], 'b': [2, 2]}
Example #9
0
def test_select_multiple_cols_in_table():
    d = {'a': 1, 'b': 2}
    r = Row(d)
    t = Table([r, r], column_width=None)
    assert t[['a', 'b']] == {'a': [1, 1], 'b': [2, 2]}
Example #10
0
def test_select_col_in_table():
    r = Row({'a': 1, 'b': 2})
    t = Table([r, r], column_width=None)
    assert t['a'] == [1, 1]
Example #11
0
def test_empty_table():
    return Table({})
Example #12
0
def test_table_wrap():
    r = Row({'a': 'abc d', 'b': 'abc d'})
    table = Table([r, r], column_width=3)
    # Max expected length: 3 (col a) + 2 (whitespace) + 3 (col b) = 8
    assert max([len(line) for line in str(table).splitlines()]) == 8
Example #13
0
def test_table_iter():
    r = Row({'a': 1, 'b': 2})
    t = Table([r, r])
    assert set(iter(t)) == {'a', 'b'}