Mercurial > repos > jankanis > blast2html
comparison blast2html.py @ 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 | c0804e6443c6 |
comparison
equal
deleted
inserted
replaced
75:4d2c25baf5a3 | 76:7d0d46168fd5 |
---|---|
19 from lxml import objectify | 19 from lxml import objectify |
20 import jinja2 | 20 import jinja2 |
21 | 21 |
22 | 22 |
23 | 23 |
24 _filters = dict(int='int', float='float') | 24 _filters = dict(float='float') |
25 def filter(func_or_name): | 25 def filter(func_or_name): |
26 "Decorator to register a function as filter in the current jinja environment" | 26 "Decorator to register a function as filter in the current jinja environment" |
27 if isinstance(func_or_name, six.string_types): | 27 if isinstance(func_or_name, six.string_types): |
28 def inner(func): | 28 def inner(func): |
29 _filters[func_or_name] = func.__name__ | 29 _filters[func_or_name] = func.__name__ |
46 return 4 | 46 return 4 |
47 | 47 |
48 @filter | 48 @filter |
49 def fmt(val, fmt): | 49 def fmt(val, fmt): |
50 return format(float(val), fmt) | 50 return format(float(val), fmt) |
51 | |
52 @filter | |
53 def numfmt(val): | |
54 """Format numbers in decimal notation, but without excessive trailing 0's. | |
55 Default python float formatting will use scientific notation for some values, | |
56 or append trailing zeros with the 'f' format type, and the number of digits differs | |
57 between python 2 and 3.""" | |
58 fpart, ipart = math.modf(val) | |
59 if fpart == 0: | |
60 return str(int(val)) | |
61 # round to 10 to get identical representations in python 2 and 3 | |
62 s = format(round(val, 10), '.10f').rstrip('0') | |
63 if s[-1] == '.': | |
64 s += '0' | |
65 return s | |
51 | 66 |
52 @filter | 67 @filter |
53 def firsttitle(hit): | 68 def firsttitle(hit): |
54 return hit.Hit_def.text.split('>')[0] | 69 return hit.Hit_def.text.split('>')[0] |
55 | 70 |