🎨 supress pep 0515, fix #48

This commit is contained in:
chfw 2018-05-02 23:04:48 +01:00
parent 47915d1cbc
commit 48a8b2ce41
6 changed files with 73 additions and 14 deletions

View File

@ -1,6 +1,14 @@
Change log
================================================================================
0.5.7 - unreleased
--------------------------------------------------------------------------------
fixed
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#. `#48 <https://github.com/pyexcel/pyexcel-io/issues/48>`_, turn off pep 0515
0.5.6 - 11.01.2017
--------------------------------------------------------------------------------

View File

@ -1,6 +1,12 @@
name: pyexcel-io
organisation: pyexcel
releases:
- changes:
- action: fixed
details:
- '`#48`, turn off pep 0515'
date: unreleased
version: 0.5.7
- changes:
- action: fixed
details:

View File

@ -130,6 +130,7 @@ class CSVSheetReader(SheetReader):
ignore_infinity=True,
auto_detect_int=True,
auto_detect_datetime=True,
pep_0515_off=True,
**keywords
):
SheetReader.__init__(self, sheet, **keywords)
@ -139,6 +140,7 @@ class CSVSheetReader(SheetReader):
self.__ignore_infinity = ignore_infinity
self.__auto_detect_datetime = auto_detect_datetime
self.__file_handle = None
self.__pep_0515_off = pep_0515_off
def get_file_handle(self):
""" return me unicde reader for csv """
@ -159,9 +161,11 @@ class CSVSheetReader(SheetReader):
def __convert_cell(self, csv_cell_text):
ret = None
if self.__auto_detect_int:
ret = service.detect_int_value(csv_cell_text)
ret = service.detect_int_value(csv_cell_text, self.__pep_0515_off)
if ret is None and self.__auto_detect_float:
ret = service.detect_float_value(csv_cell_text)
ret = service.detect_float_value(
csv_cell_text, self.__pep_0515_off
)
shall_we_ignore_the_conversion = (
(ret in [float("inf"), float("-inf")])
and self.__ignore_infinity

View File

@ -39,27 +39,35 @@ def detect_date_value(cell_text):
return ret
def detect_float_value(cell_text):
try:
should_we_skip_it = (
cell_text.startswith("0") and cell_text.startswith("0.") is False
)
if should_we_skip_it:
# do not convert if a number starts with 0
# e.g. 014325
def detect_float_value(cell_text, pep_0515_off=True):
should_we_skip_it = (
cell_text.startswith("0") and cell_text.startswith("0.") is False
)
if should_we_skip_it:
# do not convert if a number starts with 0
# e.g. 014325
return None
if pep_0515_off:
pattern = "([0-9]+_)+[0-9]+.[0-9]*$"
if re.match(pattern, cell_text):
return None
else:
return float(cell_text)
try:
return float(cell_text)
except ValueError:
return None
def detect_int_value(cell_text):
def detect_int_value(cell_text, pep_0515_off=True):
if cell_text.startswith("0") and len(cell_text) > 1:
return None
if pep_0515_off:
pattern = "([0-9]+_)+[0-9]+$"
if re.match(pattern, cell_text):
return None
try:
return int(cell_text)

View File

@ -170,5 +170,14 @@ def test_issue_43():
p.get_book(url="https://github.com/pyexcel/pyexcel-xls/raw/master/tests/fixtures/file_with_an_empty_sheet.xls"); # flake8: noqa
def test_pyexcel_issue_138():
array = [['123_122', '123_1.', '123_1.0']]
save_data('test.csv', array)
data = get_data('test.csv')
expected = [['123_122', '123_1.', '123_1.0']]
eq_(data['test.csv'], expected)
os.unlink('test.csv')
def get_fixture(file_name):
return os.path.join("tests", "fixtures", file_name)

View File

@ -1,5 +1,7 @@
from nose.tools import eq_, raises
from pyexcel_io.service import date_value, time_value
from pyexcel_io.service import detect_int_value
from pyexcel_io.service import detect_float_value
def test_date_util_parse():
@ -50,3 +52,25 @@ def test_fake_date_time_20():
def test_issue_1_error():
result = time_value('PT1111')
eq_(result, None)
def test_detect_int_value():
result = detect_int_value('123')
eq_(result, 123)
def test_detect_float_value():
result = detect_float_value('123.1')
eq_(result, 123.1)
def test_suppression_of_pep_0515_int():
result = detect_int_value('123_123')
eq_(result, None)
def test_suppression_of_pep_0515_float():
result = detect_float_value('123_123.')
eq_(result, None)
result = detect_float_value('123_123.1')
eq_(result, None)