# Path: graphite_beacon/utils.py
# def parse_rule(rule):
#     tokens = _tokenize_rule(rule)
#     level, initial_expr, exprs = _parse_rule(tokens)
# 
#     result = {'level': level, 'raw': rule, 'exprs': [_parse_expr(initial_expr)]}
# 
#     for logical_operator, expr in exprs:
#         result['exprs'].extend([logical_operator, _parse_expr(expr)])
# 
#     return result
# 
# Path: graphite_beacon/utils.py
# IDENTITY = lambda x: x
# 
# def convert_from_format(num, unit=None):
#     if not unit:
#         return float(num)
#     return float(num) * CONVERT_HASH.get(unit, 1)
# 
# def convert_to_format(value, frmt=None):
#     value = float(value)
#     units = CONVERT.get(frmt, [])
#     for name, size in units:
#         if size < value:
#             break
#     else:
#         return value
# 
#     value /= size  # pylint: disable=undefined-loop-variable
#     value = ("%.1f" % value).rstrip('0').rstrip('.')
#     return "{}{}".format(value, name)  # pylint: disable=undefined-loop-variable


import operator as op
import pytest
from funcparserlib.lexer import LexerError
from graphite_beacon.utils import parse_rule as parse_rule
from graphite_beacon.utils import (IDENTITY, convert_from_format,
                                   convert_to_format)




def test_convert():
    assert convert_to_format(789874) == 789874
    assert convert_from_format(789874)
    assert convert_to_format(45, 'percent') == "45%"
    assert convert_from_format('45', '%') == 45

    assert convert_to_format(789, 'bytes') == 789
    assert convert_to_format(456789, 'bytes') == '446.1KB'
    assert convert_from_format('456.8', 'KB') == 467763.2
    assert convert_to_format(45678912, 'bytes') == '43.6MB'
    assert convert_from_format('45.7', 'MB') == 47919923.2
    assert convert_to_format(4567891245, 'bytes') == '4.3GB'
    assert convert_from_format('4.6', 'GB') == 4939212390.4

    assert convert_from_format('456.8', 'Kb') == 467763.2
    assert convert_from_format('456.8', 'Kbps') == 456800

    assert convert_to_format(789, 'short') == 789
    assert convert_to_format(456789, 'short') == '456.8K'
    assert convert_from_format('456.8', 'K') == 456800

# Path: graphite_beacon/utils.py
# def parse_rule(rule):
#     tokens = _tokenize_rule(rule)
#     level, initial_expr, exprs = _parse_rule(tokens)
# 
#     result = {'level': level, 'raw': rule, 'exprs': [_parse_expr(initial_expr)]}
# 
#     for logical_operator, expr in exprs:
#         result['exprs'].extend([logical_operator, _parse_expr(expr)])
# 
#     return result
# 
# Path: graphite_beacon/utils.py
# IDENTITY = lambda x: x
# 
# def convert_from_format(num, unit=None):
#     if not unit:
#         return float(num)
#     return float(num) * CONVERT_HASH.get(unit, 1)
# 
# def convert_to_format(value, frmt=None):
#     value = float(value)
#     units = CONVERT.get(frmt, [])
#     for name, size in units:
#         if size < value:
#             break
#     else:
#         return value
# 
#     value /= size  # pylint: disable=undefined-loop-variable
#     value = ("%.1f" % value).rstrip('0').rstrip('.')
#     return "{}{}".format(value, name)  # pylint: disable=undefined-loop-variable


# Path: tests/unit/utils_test.py
import operator as op
import pytest
from funcparserlib.lexer import LexerError
from graphite_beacon.utils import parse_rule as parse_rule
from graphite_beacon.utils import (IDENTITY, convert_from_format,
                                   convert_to_format)





def test_convert():
    assert convert_to_format(789874) == 789874
    assert convert_from_format(789874)
    assert convert_to_format(45, 'percent') == "45%"
    assert convert_from_format('45', '%') == 45

    assert convert_to_format(789, 'bytes') == 789
    assert convert_to_format(456789, 'bytes') == '446.1KB'
    assert convert_from_format('456.8', 'KB') == 467763.2
    assert convert_to_format(45678912, 'bytes') == '43.6MB'
    assert convert_from_format('45.7', 'MB') == 47919923.2
    assert convert_to_format(4567891245, 'bytes') == '4.3GB'
    assert convert_from_format('4.6', 'GB') == 4939212390.4

    assert convert_from_format('456.8', 'Kb') == 467763.2
    assert convert_from_format('456.8', 'Kbps') == 456800

    assert convert_to_format(789, 'short') == 789
    assert convert_to_format(456789, 'short') == '456.8K'
    assert convert_from_format('456.8', 'K') == 456800

    assert convert_to_format(45678912, 'short') == '45.7Mil'