Mercurial > repos > jankanis > blast2html
changeset 76:7d0d46168fd5 py2.6
Format all numbers in a predictable way
author | Jan Kanis <jan.code@jankanis.nl> |
---|---|
date | Thu, 19 Jun 2014 14:07:00 +0200 |
parents | 4d2c25baf5a3 |
children | 05500bdd7376 |
files | blast2html.html.jinja blast2html.py |
diffstat | 2 files changed, 18 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/blast2html.html.jinja Wed Jun 18 17:50:32 2014 +0200 +++ b/blast2html.html.jinja Thu Jun 19 14:07:00 2014 +0200 @@ -467,7 +467,7 @@ <div class=graphicrow> <div> {% for s in result|queryscale %} - <div class=graphicitem style="width: {{s.width}}%"> + <div class=graphicitem style="width: {{s.width|numfmt}}%"> {% if s.width > 3.0 %} <div>{{s.label}}</div> {% else %} @@ -490,7 +490,7 @@ <div class="matchrow graphicrow"> {% for hit in line.colors %} <div class="matchitem graphicitem" - style="background-color: {{hit[1]}}; width: {{hit[0]}}%"></div> + style="background-color: {{hit[1]}}; width: {{hit[0]|numfmt}}%"></div> {% endfor %} </div> </a>
--- a/blast2html.py Wed Jun 18 17:50:32 2014 +0200 +++ b/blast2html.py Thu Jun 19 14:07:00 2014 +0200 @@ -21,7 +21,7 @@ -_filters = dict(int='int', float='float') +_filters = dict(float='float') def filter(func_or_name): "Decorator to register a function as filter in the current jinja environment" if isinstance(func_or_name, six.string_types): @@ -50,6 +50,21 @@ return format(float(val), fmt) @filter +def numfmt(val): + """Format numbers in decimal notation, but without excessive trailing 0's. + Default python float formatting will use scientific notation for some values, + or append trailing zeros with the 'f' format type, and the number of digits differs + between python 2 and 3.""" + fpart, ipart = math.modf(val) + if fpart == 0: + return str(int(val)) + # round to 10 to get identical representations in python 2 and 3 + s = format(round(val, 10), '.10f').rstrip('0') + if s[-1] == '.': + s += '0' + return s + +@filter def firsttitle(hit): return hit.Hit_def.text.split('>')[0]