Skip to content

Commit

Permalink
Merge pull request #19 from johnomotani/get-help-table
Browse files Browse the repository at this point in the history
OptionsFactory.get_help_table()
  • Loading branch information
johnomotani authored Dec 31, 2022
2 parents c65c091 + 26a951b commit ee19e26
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 10 deletions.
12 changes: 8 additions & 4 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
[flake8]
max-line-length = 88
ignore =
E741, # 'ambiguous variable names' forbids using 'I', 'O' or 'l'
W503, # 'line break before binary operator', but this is allowed and useful inside brackets
E203, # 'whitespace before ':'', but black formats some slice expressions with space before ':'
E231, # missing whitespace after ',', but black formats some expressions without space after ','
# 'ambiguous variable names' forbids using 'I', 'O' or 'l'
E741,
# 'line break before binary operator', but this is allowed and useful inside brackets
W503,
# 'whitespace before ':'', but black formats some slice expressions with space before ':'
E203,
# missing whitespace after ',', but black formats some expressions without space after ','
E231,
exclude =
optionsfactory/_version.py
versioneer.py
6 changes: 3 additions & 3 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
if: always()
strategy:
matrix:
python-version: [3.6, 3.7, '3.x']
python-version: [3.7, 3.8, 3.9, '3.x']

steps:
- uses: actions/checkout@v2
Expand All @@ -37,7 +37,7 @@ jobs:
if: always()
strategy:
matrix:
python-version: [3.6, 3.7, '3.x']
python-version: ['3.x']

steps:
- uses: actions/checkout@v2
Expand All @@ -61,7 +61,7 @@ jobs:
if: always()
strategy:
matrix:
python-version: [3.6, 3.7, '3.x']
python-version: ['3.x']

steps:
- uses: actions/checkout@v2
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/pythonpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
if: always()
strategy:
matrix:
python-version: [3.6, 3.7, '3.x']
python-version: [3.7, 3.8, 3.9, '3.x']

steps:
- uses: actions/checkout@v2
Expand All @@ -39,7 +39,7 @@ jobs:
if: always()
strategy:
matrix:
python-version: [3.6, 3.7, '3.x']
python-version: ['3.x']

steps:
- uses: actions/checkout@v2
Expand All @@ -63,7 +63,7 @@ jobs:
if: always()
strategy:
matrix:
python-version: [3.6, 3.7, '3.x']
python-version: ['3.x']

steps:
- uses: actions/checkout@v2
Expand Down
70 changes: 70 additions & 0 deletions optionsfactory/optionsfactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,76 @@ def __create_immutable(self, values=None):
# Return new Options instance
return OptionsFactory.Options(mutable_options)

def get_help_table(self, prefix=None):
"""Print a table of the options in this OptionsFactory, with help text and
default values, using ReStructuredText syntax.
Parameters
----------
prefix : str, default None
If a value is passed, add `prefix` to the beginning of each line, e.g. to
add indentation
"""
if prefix is None:
prefix = ""
defaults = self.defaults
keys = sorted(defaults.keys())
docs = self.doc
heading1 = "Option"
heading2 = "Description"
heading3 = "Default"
column1_width = max(max(len(k) for k in keys), len(heading1))
column2_width = max(max(len(d) for d in docs.values()), len(heading2))
column3_width = max(max(len(str(d)) for d in defaults.values()), len(heading3))
separator = (
prefix
+ "+"
+ "-" * column1_width
+ "+"
+ "-" * column2_width
+ "+"
+ "-" * column3_width
+ "+\n"
)
table = separator
table = (
table
+ prefix
+ "|"
+ heading1.ljust(column1_width)
+ "|"
+ heading2.ljust(column2_width)
+ "|"
+ heading3.ljust(column3_width)
+ "|\n"
)
table = table + (
prefix
+ "+"
+ "=" * column1_width
+ "+"
+ "=" * column2_width
+ "+"
+ "=" * column3_width
+ "+\n"
)
for k in keys:
table = (
table
+ prefix
+ "|"
+ k.ljust(column1_width)
+ "|"
+ docs[k].ljust(column2_width)
+ "|"
+ str(defaults[k].evaluate_expression(self.create())).ljust(
column3_width
)
+ "|\n"
)
table = table + separator
return table

class MutableOptions:
"""Provide access to a pre-defined set of options, with default values that may
depend on the values of other options
Expand Down

0 comments on commit ee19e26

Please sign in to comment.