Skip to content
This repository was archived by the owner on Dec 7, 2021. It is now read-only.

Commit 1d85a8f

Browse files
committed
New example.png. Refactored examples. Rel path.
Refactored all three example scripts. Using __name__ == '__main__' and other proper code styling. Updated example.png screenshot with latest output on Win10 (instead of 8), XP (without themes), and OSX. Using relative path to picture in README so different branches and tags show their version of the picture. Instead of it always being master. Having setup.py convert relative path to absolute github path since picture won't be available on PyPI.
1 parent 7272897 commit 1d85a8f

File tree

7 files changed

+137
-81
lines changed

7 files changed

+137
-81
lines changed

README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Install:
3535
Example Implementations
3636
=======================
3737

38-
.. image:: https://github.com/Robpol86/terminaltables/raw/master/example.png?raw=true
38+
.. image:: example.png?raw=true
3939
:alt: Example Scripts Screenshot
4040

4141
Source code for examples: `example1.py <https://github.com/Robpol86/terminaltables/blob/master/example1.py>`_,

example.png

-56.3 KB
Loading

example1.py

+34-24
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,37 @@
66

77
from __future__ import print_function
88

9-
from terminaltables import AsciiTable, DoubleTable
10-
11-
12-
table_data = [
13-
['Platform', 'Years', 'Notes'],
14-
['Mk5', '2007-2009', 'The Golf Mk5 Variant was\nintroduced in January 2007 and\nproduced up to March 2009.'],
15-
['MKVI', '2009-2013', 'Might actually be Mk5.'],
16-
]
17-
table = AsciiTable(table_data, 'Jetta SportWagen')
18-
print()
19-
print(table.table)
20-
21-
table = DoubleTable(table_data, 'Jetta SportWagen')
22-
table.inner_row_border = True
23-
table.justify_columns[2] = 'right'
24-
print()
25-
print(table.table)
26-
27-
table.outer_border = False
28-
table.justify_columns[1] = 'center'
29-
print()
30-
print(table.table)
31-
32-
print()
9+
from terminaltables import AsciiTable, DoubleTable, SingleTable
10+
11+
TABLE_DATA = (
12+
('Platform', 'Years', 'Notes'),
13+
('Mk5', '2007-2009', 'The Golf Mk5 Variant was\nintroduced in 2007.'),
14+
('MKVI', '2009-2013', 'Might actually be Mk5.'),
15+
)
16+
17+
18+
def main():
19+
"""Main function."""
20+
title = 'Jetta SportWagen'
21+
22+
# AsciiTable.
23+
table_instance = AsciiTable(TABLE_DATA, title)
24+
table_instance.justify_columns[2] = 'right'
25+
print(table_instance.table)
26+
print()
27+
28+
# SingleTable.
29+
table_instance = SingleTable(TABLE_DATA, title)
30+
table_instance.justify_columns[2] = 'right'
31+
print(table_instance.table)
32+
print()
33+
34+
# DoubleTable.
35+
table_instance = DoubleTable(TABLE_DATA, title)
36+
table_instance.justify_columns[2] = 'right'
37+
print(table_instance.table)
38+
print()
39+
40+
41+
if __name__ == '__main__':
42+
main()

example2.py

+74-41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
"""Example usage of terminaltables using colorclass.
2+
"""Example usage of terminaltables with colorclass.
33
44
Just prints sample text and exits.
55
"""
@@ -11,43 +11,76 @@
1111
from terminaltables import SingleTable
1212

1313

14-
Windows.enable(auto_colors=True, reset_atexit=True) # Does nothing if not on Windows.
15-
16-
table_data = [
17-
[Color('{autogreen}<10ms{/autogreen}'), '192.168.0.100, 192.168.0.101'],
18-
[Color('{autoyellow}10ms <= 100ms{/autoyellow}'), '192.168.0.102, 192.168.0.103'],
19-
[Color('{autored}>100ms{/autored}'), '192.168.0.105'],
20-
]
21-
table = SingleTable(table_data)
22-
table.inner_heading_row_border = False
23-
print()
24-
print(table.table)
25-
26-
table.title = '192.168.0.105'
27-
table.justify_columns = {0: 'center', 1: 'center', 2: 'center'}
28-
table.inner_row_border = True
29-
table.table_data = [
30-
[Color('Low Space'), Color('{autocyan}Nominal Space{/autocyan}'), Color('Excessive Space')],
31-
[Color('Low Load'), Color('Nominal Load'), Color('{autored}High Load{/autored}')],
32-
[Color('{autocyan}Low Free RAM{/autocyan}'), Color('Nominal Free RAM'), Color('High Free RAM')],
33-
]
34-
print()
35-
print(table.table)
36-
37-
table.title = None
38-
table.outer_border = False
39-
table.table_data = [['A', 'B'], ['C', 'D']]
40-
print()
41-
print(table.table)
42-
43-
table.outer_border = True
44-
table.inner_row_border = False
45-
table.inner_column_border = False
46-
print()
47-
print(table.table)
48-
49-
table = SingleTable([['Obey Obey Obey Obey']], 'Instructions')
50-
print()
51-
print(table.table)
52-
53-
print()
14+
def table_server_timings():
15+
"""Return table string to be printed."""
16+
table_data = [
17+
[Color('{autogreen}<10ms{/autogreen}'), '192.168.0.100, 192.168.0.101'],
18+
[Color('{autoyellow}10ms <= 100ms{/autoyellow}'), '192.168.0.102, 192.168.0.103'],
19+
[Color('{autored}>100ms{/autored}'), '192.168.0.105'],
20+
]
21+
table_instance = SingleTable(table_data)
22+
table_instance.inner_heading_row_border = False
23+
return table_instance.table
24+
25+
26+
def table_server_status():
27+
"""Return table string to be printed."""
28+
table_data = [
29+
[Color('Low Space'), Color('{autocyan}Nominal Space{/autocyan}'), Color('Excessive Space')],
30+
[Color('Low Load'), Color('Nominal Load'), Color('{autored}High Load{/autored}')],
31+
[Color('{autocyan}Low Free RAM{/autocyan}'), Color('Nominal Free RAM'), Color('High Free RAM')],
32+
]
33+
table_instance = SingleTable(table_data, '192.168.0.105')
34+
table_instance.inner_heading_row_border = False
35+
table_instance.inner_row_border = True
36+
table_instance.justify_columns = {0: 'center', 1: 'center', 2: 'center'}
37+
return table_instance.table
38+
39+
40+
def table_abcd():
41+
"""Return table string to be printed. Two tables on one line."""
42+
table_instance = SingleTable([['A', 'B'], ['C', 'D']])
43+
44+
# Get first table lines.
45+
table_instance.outer_border = False
46+
table_inner_borders = table_instance.table.splitlines()
47+
48+
# Get second table lines.
49+
table_instance.outer_border = True
50+
table_instance.inner_heading_row_border = False
51+
table_instance.inner_column_border = False
52+
table_outer_borders = table_instance.table.splitlines()
53+
54+
# Combine.
55+
smallest, largest = sorted([table_inner_borders, table_outer_borders], key=len)
56+
smallest += [''] * (len(largest) - len(smallest)) # Make both same size.
57+
combined = list()
58+
for i, row in enumerate(largest):
59+
combined.append(row.ljust(10) + ' ' + smallest[i])
60+
return '\n'.join(combined)
61+
62+
63+
def main():
64+
"""Main function."""
65+
Windows.enable(auto_colors=True, reset_atexit=True) # Does nothing if not on Windows.
66+
67+
# Server timings.
68+
print(table_server_timings())
69+
print()
70+
71+
# Server status.
72+
print(table_server_status())
73+
print()
74+
75+
# Two A B C D tables.
76+
print(table_abcd())
77+
print()
78+
79+
# Instructions.
80+
table_instance = SingleTable([['Obey Obey Obey Obey']], 'Instructions')
81+
print(table_instance.table)
82+
print()
83+
84+
85+
if __name__ == '__main__':
86+
main()

example3.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,27 @@
1010

1111
from terminaltables import SingleTable
1212

13-
14-
# Setup string and table.
15-
long_string = ('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore '
13+
LONG_STRING = ('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore '
1614
'et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut '
1715
'aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum '
1816
'dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui '
1917
'officia deserunt mollit anim id est laborum.')
20-
table = SingleTable([['Long String', '']])
2118

22-
# Calculate newlines.
23-
max_width = table.column_max_width(1)
24-
wrapped_string = '\n'.join(wrap(long_string, max_width))
25-
table.table_data[0][1] = wrapped_string
2619

27-
print(table.table)
20+
def main():
21+
"""Main function."""
22+
table_data = [
23+
['Long String', ''], # One row. Two columns. Long string will replace this empty string.
24+
]
25+
table = SingleTable(table_data)
26+
27+
# Calculate newlines.
28+
max_width = table.column_max_width(1)
29+
wrapped_string = '\n'.join(wrap(LONG_STRING, max_width))
30+
table.table_data[0][1] = wrapped_string
31+
32+
print(table.table)
33+
34+
35+
if __name__ == '__main__':
36+
main()

setup.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
from setuptools import setup
1010

11+
NAME = 'terminaltables'
12+
VERSION = '3.0.0'
13+
1114

1215
def readme():
1316
"""Try to read README.rst or return empty string if failed.
@@ -17,9 +20,10 @@ def readme():
1720
"""
1821
path = os.path.realpath(os.path.join(os.path.dirname(__file__), 'README.rst'))
1922
handle = None
23+
url_prefix = 'https://raw.githubusercontent.com/Robpol86/{name}/v{version}/'.format(name=NAME, version=VERSION)
2024
try:
2125
handle = codecs.open(path, encoding='utf-8')
22-
return handle.read(131072)
26+
return handle.read(131072).replace('.. image:: example', '.. image:: {0}example'.format(url_prefix))
2327
except IOError:
2428
return ''
2529
finally:
@@ -55,9 +59,9 @@ def readme():
5559
keywords='Shell Bash ANSI ASCII terminal tables',
5660
license='MIT',
5761
long_description=readme(),
58-
name='terminaltables',
59-
packages=['terminaltables'],
60-
url='https://github.com/Robpol86/terminaltables',
61-
version='3.0.0',
62+
name=NAME,
63+
packages=[NAME],
64+
url='https://github.com/Robpol86/' + NAME,
65+
version=VERSION,
6266
zip_safe=True,
6367
)

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ commands =
5151
python -c "assert '{[general]version}' == __import__('{[general]name}').__version__"
5252
python -c "assert 'author=\'{[general]author}\'' in open('setup.py').read(102400)"
5353
python -c "assert 'license=\'{[general]license}\'' in open('setup.py').read(102400)"
54-
python -c "assert 'version=\'{[general]version}\'' in open('setup.py').read(102400)"
54+
python -c "assert 'VERSION = \'{[general]version}\'' in open('setup.py').read(102400)"
5555
python -c "assert '\n{[general]version} - ' in open('README.rst').read(102400)"
5656
deps =
5757
coverage==4.0.3

0 commit comments

Comments
 (0)