Skip to content

Commit

Permalink
docs: fix markdown
Browse files Browse the repository at this point in the history
* Add failing test for markdown.

* Fix markdown and adjust test of metadata_table.

* Remove $$ from metadata_table.

* Enable selection of columns in generate_metadata_table.
  • Loading branch information
schymans committed Feb 4, 2020
1 parent fa0f2d2 commit c3751de
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
1 change: 0 additions & 1 deletion essm/variables/units.py
Expand Up @@ -100,7 +100,6 @@ def markdown(unit):
if isinstance(arg, Quantity):
tuples.append((str(arg.abbrev), 1))
tuples.sort(key=itemgetter(1), reverse=True)
tuples.sort(key=itemgetter(0))
for item in tuples:
if item[1] == 1:
str1 = str1 + ' ' + item[0]
Expand Down
29 changes: 22 additions & 7 deletions essm/variables/utils.py
Expand Up @@ -56,31 +56,46 @@ def _repr_html_(self):
return ''.join(html)


def generate_metadata_table(variables=None, include_header=True):
def generate_metadata_table(variables=None, include_header=True, cols=None):
"""Generate table of variables, default values and units.
If variables not provided in list ``variables``, table will contain
all variables in ``Variables.__registry__``.
It is possible to remove columns by providing a tuple with a subste of:
cols=('Symbol', 'Name', 'Description', 'Definition',
'Default value', 'Units')
"""
from ._core import Variable
all_cols = ('Symbol', 'Name', 'Description', 'Definition',
'Default value', 'Units')
cols = cols or all_cols
table = ListTable()
variables = variables or Variable.__registry__.keys()
if include_header:
table.append(
('Symbol', 'Name', 'Description', 'Definition',
'Default value', 'Units')
)
table.append(cols)

for variable in sorted(variables,
key=lambda x: x.definition.latex_name.lower()):
symbol = '$' + variable.definition.latex_name + '$'
name = str(variable)
doc = variable.__doc__
defn = '$' + latex(Variable.__expressions__.get(variable, '')) + '$'
defn1 = Variable.__expressions__.get(variable, '')
if len(defn1) > 1:
defn = '$' + latex(defn1) + '$'
else:
defn = ''
val = str(Variable.__defaults__.get(variable, '-'))
unit = markdown(variable.definition.unit)
dict_col = {}
dict_col['Symbol'] = symbol
dict_col['Name'] = name
dict_col['Description'] = doc
dict_col['Definition'] = defn
dict_col['Default value'] = val
dict_col['Units'] = unit

table.append(
(symbol, name, doc, defn, val, markdown(variable.definition.unit))
tuple(dict_col[item] for item in cols)
)
return table

Expand Down
5 changes: 3 additions & 2 deletions tests/test_variables.py
Expand Up @@ -165,13 +165,14 @@ def test_markdown():
"""Check markdown representation of units."""
assert markdown(kilogram * meter / second ** 2) == 'kg m s$^{-2}$'
assert markdown(meter / second) == 'm s$^{-1}$'
assert markdown(second / meter) == 's m$^{-1}$'


def test_generate_metadata_table():
"""Check display of table of units."""
assert generate_metadata_table([E_l, lambda_E]) \
== [('Symbol', 'Name', 'Description', 'Definition', 'Default value',
'Units'),
('$\\lambda_E$', 'lambda_E', 'Latent heat of evaporation.', '$$',
('$\\lambda_E$', 'lambda_E', 'Latent heat of evaporation.', '',
'2450000.0', 'J kg$^{-1}$'), ('$E_l$', 'E_l',
'Latent heat flux from leaf.', '$$', '-', 'J m$^{-2}$ s$^{-1}$')]
'Latent heat flux from leaf.', '', '-', 'J s$^{-1}$ m$^{-2}$')]

0 comments on commit c3751de

Please sign in to comment.