Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix markdown #79

Merged
merged 4 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion essm/variables/units.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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}$')]