Commit a119610a authored by Giorgos Korfiatis's avatar Giorgos Korfiatis Committed by Christos Stavrakakis
Browse files

common: Support parsing, printing inf numbers

By convention, infinite is represented by 2**63-1, the max value for
a signed 64 bit field.
parent 8eda5c1e
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
from synnefo.lib.ordereddict import OrderedDict from synnefo.lib.ordereddict import OrderedDict
import re import re
PRACTICALLY_INFINITE = 2**63 - 1
DEFAULT_PARSE_BASE = 1024 DEFAULT_PARSE_BASE = 1024
PARSE_EXPONENTS = { PARSE_EXPONENTS = {
'': 0, '': 0,
...@@ -76,6 +77,9 @@ def _parse_number_with_unit(s): ...@@ -76,6 +77,9 @@ def _parse_number_with_unit(s):
def parse_with_style(s): def parse_with_style(s):
if s in ['inf', 'infinite']:
return PRACTICALLY_INFINITE, 0
n, unit = _parse_number_with_unit(s) n, unit = _parse_number_with_unit(s)
try: try:
exponent = PARSE_EXPONENTS[unit] exponent = PARSE_EXPONENTS[unit]
...@@ -136,14 +140,17 @@ def get_exponent(style): ...@@ -136,14 +140,17 @@ def get_exponent(style):
def show(n, unit, style=None): def show(n, unit, style=None):
if style == 'none':
return str(n)
if n == PRACTICALLY_INFINITE:
return 'inf'
try: try:
unit_dict = UNITS[unit] unit_dict = UNITS[unit]
except KeyError: except KeyError:
return str(n) return str(n)
if style == 'none':
return str(n)
BASE = unit_dict['BASE'] BASE = unit_dict['BASE']
DISPLAY = unit_dict['DISPLAY'] DISPLAY = unit_dict['DISPLAY']
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment