Skip to content

Commit

Permalink
Merge pull request #474 from vrii14/master
Browse files Browse the repository at this point in the history
Added docs for the method as_text()
  • Loading branch information
adnanhemani committed Oct 9, 2020
2 parents e8f907a + 6f07ee4 commit 6bb01af
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion datascience/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2780,7 +2780,65 @@ def _get_column_formatters(self, max_rows, as_html):
return fmts

def as_text(self, max_rows=0, sep=" | "):
"""Format table as text."""
"""Format table as text
Args:
max_rows(int) The maximum number of rows to be present in the converted string of table. (Optional Argument)
sep(str) The seperator which will appear in converted string between the columns. (Optional Argument)
Returns:
String form of the table
The table is just converted to a string with columns seperated by the seperator(argument- default(' | ')) and rows seperated by '\n'
Few examples of the as_text() method are as follows:
1.
>>> table = Table().with_columns({'name': ['abc', 'xyz', 'uvw'], 'age': [12,14,20],'height': [5.5,6.0,5.9],})
>>> table
name | age | height
abc | 12 | 5.5
xyz | 14 | 6
uvw | 20 | 5.9
>>> table_astext = table.as_text()
>>> table_astext
'name | age | height\\nabc | 12 | 5.5\\nxyz | 14 | 6\\nuvw | 20 | 5.9'
>>> type(table)
<class 'datascience.tables.Table'>
>>> type(table_astext)
<class 'str'>
2.
>>> sizes = Table(['size', 'count']).with_rows([ ['small', 50], ['medium', 100], ['big', 50], ])
>>> sizes
size | count
small | 50
medium | 100
big | 50
>>> sizes_astext = sizes.as_text()
>>> sizes_astext
'size | count\\nsmall | 50\\nmedium | 100\\nbig | 50'
3.
>>> sizes_astext = sizes.as_text(1)
>>> sizes_astext
'size | count\\nsmall | 50\\n... (2 rows omitted)'
4.
>>> sizes_astext = sizes.as_text(2, ' - ')
>>> sizes_astext
'size - count\\nsmall - 50\\nmedium - 100\\n... (1 rows omitted)'
"""
if not max_rows or max_rows > self.num_rows:
max_rows = self.num_rows
omitted = max(0, self.num_rows - max_rows)
Expand Down

0 comments on commit 6bb01af

Please sign in to comment.