Skip to content

Commit

Permalink
Merge pull request #34 from thombashi/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
thombashi committed Jul 9, 2016
2 parents 5234cbc + 90e0e0f commit 3bc3e47
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 35 deletions.
7 changes: 5 additions & 2 deletions dataproperty/_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ def is_datetime(value):


def get_integer_digit(value):
from ._type_checker import FloatTypeChecker

abs_value = abs(float(value))

if not is_float(value):
if not FloatTypeChecker(value).is_type():
# bool type value reaches this line
raise TypeError("invalid type '%s" % (type(value)))

Expand All @@ -112,9 +114,10 @@ def get_integer_digit(value):
def _get_decimal_places(value, integer_digits):
from collections import namedtuple
from six.moves import range
from ._type_checker import IntegerTypeChecker

float_digit_len = 0
if is_integer(value):
if IntegerTypeChecker(value).is_type():
abs_value = abs(int(value))
else:
abs_value = abs(float(value))
Expand Down
14 changes: 7 additions & 7 deletions dataproperty/_type_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from __future__ import absolute_import
import abc
from decimal import Decimal

import six

Expand Down Expand Up @@ -128,15 +129,14 @@ def _converter_creator(self):
return FloatConverterCreator()

def _is_instance(self):
return any(
[isinstance(self._value, float), self._value == float("inf")])
return any([
isinstance(self._value, float),
isinstance(self._value, Decimal),
])

def _is_exclude_instance(self):
return isinstance(self._value, bool)

def _is_valid_after_convert(self):
return self._converted_value != float("inf")


class BoolTypeChecker(TypeChecker):

Expand Down Expand Up @@ -182,10 +182,10 @@ def _converter_creator(self):
return FloatConverterCreator()

def _is_instance(self):
return self._value == float("inf")
return self._value in (float("inf"), Decimal("inf"))

def _is_valid_after_convert(self):
return self._converted_value == float("inf")
return self._converted_value in (float("inf"), Decimal("inf"))


class NanChecker(TypeChecker):
Expand Down
9 changes: 7 additions & 2 deletions dataproperty/converter/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ def convert(self):
class FloatConverter(ValueConverter):

def convert(self):
import decimal

if isinstance(self._value, float):
return self._value

try:
return float(self._value)
except (TypeError, ValueError):
return decimal.Decimal(self._value)
except (TypeError, ValueError, decimal.InvalidOperation):
raise TypeConversionError


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

setuptools.setup(
name=project_name,
version="0.5.4",
version="0.6.0",
url="https://github.com/thombashi/" + project_name,
bugtrack_url="https://github.com/thombashi/%s/issues" % (project_name),

Expand Down
16 changes: 7 additions & 9 deletions test/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import datetime
from decimal import Decimal
from dateutil.tz import tzoffset
import pytest
import six
Expand Down Expand Up @@ -63,18 +64,15 @@ class Test_FloatConverter_convert:
[0.0, 0.0],
[0.1, 0.1],
[-0.1, -0.1],
[1, 1.0],
[-1, -1.0],
["0.0", 0.0],
["0.1", 0.1],
["-0.1", -0.1],
["1", 1.0],
["-1", -1.0],
[1, Decimal("1")], [-1, Decimal("-1")],
["0.0", Decimal("0.0")], ["0.1", Decimal("0.1")],
["-0.1", Decimal("-0.1")],
["1", Decimal("1")], ["-1", Decimal("-1")],
[.5, .5],
[0., 0.0],
["1e-05", 1e-05],
["1e-05", Decimal('0.00001')],
[inf, inf],
[True, 1.0],
[True, Decimal("1")],
])
def test_normal(self, value, expected):
assert dpcc.FloatConverter(value).convert() == expected
Expand Down
8 changes: 7 additions & 1 deletion test/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import datetime
from decimal import Decimal

from dataproperty import *
import pytest
Expand All @@ -22,6 +23,7 @@ class Test_is_integer:
[0], [six.MAXSIZE], [-six.MAXSIZE],
["0"], [str(six.MAXSIZE)], [str(-six.MAXSIZE)],
[" 1"], ["1 "],
[Decimal(0)],
])
def test_normal(self, value):
assert is_integer(value)
Expand Down Expand Up @@ -66,14 +68,14 @@ class Test_is_float:
[.5], [0.],
["1e-05"],
[nan], [inf],
["inf"],
])
def test_normal(self, value):
assert is_float(value)

@pytest.mark.parametrize(["value"], [
[None],
["test"],
["inf"],
[True],
])
def test_abnormal(self, value):
Expand All @@ -84,11 +86,13 @@ class Test_is_nan:

@pytest.mark.parametrize(["value", "expected"], [
[nan, True],
[Decimal("nan"), True],
[None, False],
["nan", False],
["1", False],
[inf, False],
[Decimal("inf"), False],
[1, False],
[0.1, False],
[True, False],
Expand All @@ -111,6 +115,8 @@ class Test_is_not_empty_string:
[[], False],
[1, False],
[True, False],
[nan, False],
[Decimal("nan"), False],
])
def test_normal(self, value, expected):
assert is_not_empty_string(value) == expected
Expand Down
3 changes: 2 additions & 1 deletion test/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import datetime
from decimal import Decimal

import pytest
import six
Expand All @@ -27,7 +28,7 @@ class Test_DataPeroperty_data_typecode:
[-six.MAXSIZE, False, -six.MAXSIZE, Typecode.INT],
[str(-six.MAXSIZE), True, -six.MAXSIZE, Typecode.INT],
[str(six.MAXSIZE), False, str(six.MAXSIZE), Typecode.STRING],
["1.1", True, 1.1, Typecode.FLOAT],
["1.1", True, Decimal("1.1"), Typecode.FLOAT],
["-1.1", False, "-1.1", Typecode.STRING],
["a", True, "a", Typecode.STRING],
["a", False, "a", Typecode.STRING],
Expand Down
3 changes: 2 additions & 1 deletion test/test_property_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import datetime
from decimal import Decimal

import pytest

Expand Down Expand Up @@ -82,7 +83,7 @@ def test_normal(
assert prop.format_str == "d"

prop = prop_matrix[1][0]
assert prop.data == 1.1
assert prop.data == Decimal("1.1")
assert prop.typecode == Typecode.FLOAT
assert prop.align.align_code == Align.RIGHT.align_code
assert prop.align.align_string == Align.RIGHT.align_string
Expand Down
22 changes: 11 additions & 11 deletions test/test_type_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import dataproperty._type_checker as tc
from dataproperty import Typecode
from decimal import Decimal


nan = float("nan")
Expand Down Expand Up @@ -44,6 +45,7 @@ class Test_IntegerTypeChecker:
["0", True],
[" 1 ", True],
[str(six.MAXSIZE), True], [str(-six.MAXSIZE), True],
[Decimal("1"), True],
] + list(
itertools.product(
[0, six.MAXSIZE, -six.MAXSIZE],
Expand All @@ -58,9 +60,9 @@ def test_normal_true(self, value, is_convert):
@pytest.mark.parametrize(["value", "is_convert"], [
["0", False],
["0xff", True], ["0xff", False],
[" 1 ", False],
[str(six.MAXSIZE), False], [str(-six.MAXSIZE), False],
[Decimal("1"), False],
] + list(
itertools.product(
[
Expand All @@ -81,16 +83,15 @@ class Test_FloatTypeChecker:
[1, True],
[-1, True],
["0.0", True],
["0.1", True],
["-0.1", True],
["1", True],
["-1", True],
["0.1", True], ["-0.1", True],
["1", True], ["-1", True],
["1e-05", True],
[six.MAXSIZE, True], [-six.MAXSIZE, True],
[str(six.MAXSIZE), True], [str(-six.MAXSIZE), True],
["inf", True], ["nan", True],
] + list(
itertools.product(
[0.0, 0.1, -0.1, .5, 0., nan, inf],
[0.0, 0.1, -0.1, .5, 0., nan, inf, Decimal("1.1")],
[True, False],
))
)
Expand All @@ -102,17 +103,16 @@ def test_normal_true(self, value, is_convert):
@pytest.mark.parametrize(["value", "is_convert"], [
[1, False],
[-1, False],
["0.1", False],
["0.0", False],
["-0.1", False],
["-1", False],
["1", False],
["0.1", False], ["-0.1", False],
["1", False], ["-1", False],
["1e-05", False],
[six.MAXSIZE, False], [-six.MAXSIZE, False],
[str(six.MAXSIZE), False], [str(-six.MAXSIZE), False],
["inf", False], ["nan", False],
] + list(
itertools.product(
["", None, "test", "inf", True],
["", None, "test", True],
[True, False],
))
)
Expand Down

0 comments on commit 3bc3e47

Please sign in to comment.