def test_robust_tables(self): """Test :func:`humanfriendly.tables.format_robust_table()`.""" column_names = ['One', 'Two', 'Three'] data = [['1', '2', '3'], ['a', 'b', 'c']] assert ansi_strip(format_robust_table(data, column_names)) == dedent(""" -------- One: 1 Two: 2 Three: 3 -------- One: a Two: b Three: c -------- """).strip() column_names = ['One', 'Two', 'Three'] data = [['1', '2', '3'], ['a', 'b', 'Here comes a\nmulti line column!']] assert ansi_strip(format_robust_table(data, column_names)) == dedent(""" ------------------ One: 1 Two: 2 Three: 3 ------------------ One: a Two: b Three: Here comes a multi line column! ------------------ """).strip()
def test_format_robust_table(): post1 = Essay(dict(id="1", title="title 1", body="body 1")) post1_copy = Essay(dict(id="1", title="title 1", body="body 1")) post2 = Essay(dict(id="2", title="title 2", body="body 2")) posts = Essay.Set([post1, post2, post1_copy]) posts.should.be.a(ModelSet) posts.format_robust_table().should.equal( format_robust_table( [["1", "title 1", "body 1"], ["2", "title 2", "body 2"]], ["id", "title", "body"], ) ) posts.format_robust_table(["title"]).should.equal( format_robust_table([["title 1"], ["title 2"]], ["title"]) ) when_called = posts.format_robust_table.when.called_with(["inexistent"]) when_called.should.have.raised( ValueError, "the following columns are not available for <class 'tests.unit.test_set.Essay'>: {'inexistent'}", )
def format_robust_table(self, columns: Iterable[str] = None): """returns a string with a robust table ready to be printed on a terminal. powered by :py:func:`humanfriendly.tables.format_robust_table` """ columns, rows = self.get_table_columns_and_rows(columns) return format_robust_table(rows, columns)
def render_output(self, context): if 'json' in context: print context['json'] elif 'table' in context: print "\n", context['header'] table_params = {'data': context['table']} print format_pretty_table(**table_params), "\n" elif 'robust_table' in context: print "\n", context['header'] table_params = {'data': context['robust_table'], 'column_names': ['code', '']} print format_robust_table(**table_params), "\n" elif 'header' in context: self.render_message(context['header']) else: raise YomError('Not enough parameters for output formatting')
def test_format_robust_table(): chuck = User( {"id": 1, "username": "******", "email": "*****@*****.**"} ) chuck.format_robust_table().should.equal( format_robust_table( [[1, "chucknorris", "*****@*****.**"]], ["id", "username", "email"], ) )
def test_format_robust_table(): post1 = BlogPost(dict(id=1, title="title 1", body="body 1")) post2 = BlogPost(dict(id=2, title="title 2", body="body 2")) posts = BlogPost.List([post1, post2]) posts.should.be.a(ModelList) posts.format_robust_table().should.equal( format_robust_table( [[1, "title 1", "body 1"], [2, "title 2", "body 2"]], ["id", "title", "body"], )) posts.format_robust_table(["title"]).should.equal( format_robust_table([["title 1"], ["title 2"]], ["title"])) when_called = posts.format_robust_table.when.called_with(["inexistent"]) when_called.should.have.raised( ValueError, "the following columns are not available for <class 'tests.unit.test_list.BlogPost'>: {'inexistent'}", )
def format_robust_table(self): columns, rows = self.get_table_columns_and_rows() return format_robust_table(rows, columns)