Skip to content

Commit a25db31

Browse files
committed
v1.20.11 Performance improvements
1 parent b669f59 commit a25db31

File tree

5 files changed

+38
-23
lines changed

5 files changed

+38
-23
lines changed

tableschema/VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.20.10
1+
1.20.11

tableschema/field.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def __init__(self, descriptor, missing_values=config.DEFAULT_MISSING_VALUES,
4646
self.__schema = schema
4747
self.__cast_function = self.__get_cast_function()
4848
self.__check_functions = self.__get_check_functions()
49+
self.__preserve_missing_values = os.environ.get('TABLESCHEMA_PRESERVE_MISSING_VALUES')
4950

5051
@cached_property
5152
def schema(self):
@@ -155,7 +156,7 @@ def cast_value(self, value, constraints=True):
155156
# Null value
156157
if value in self.__missing_values:
157158
# Whether missing_values should be preserved without being cast
158-
if os.environ.get('TABLESCHEMA_PRESERVE_MISSING_VALUES'):
159+
if self.__preserve_missing_values:
159160
return value
160161
value = None
161162

tableschema/types/integer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def cast_integer(format, value, **options):
2121

2222
elif isinstance(value, six.string_types):
2323
if not options.get('bareNumber', _DEFAULT_BARE_NUMBER):
24-
value = re.sub(r'((^\D*)|(\D*$))', '', value)
24+
value = _RE_BARE_NUMBER.sub('', value)
2525

2626
try:
2727
value = int(value)
@@ -41,5 +41,5 @@ def cast_integer(format, value, **options):
4141

4242

4343
# Internal
44-
44+
_RE_BARE_NUMBER = re.compile(r'((^\D*)|(\D*$))')
4545
_DEFAULT_BARE_NUMBER = True

tableschema/types/number.py

+30-19
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,42 @@
1313
# Module API
1414

1515
def cast_number(format, value, **options):
16-
group_char = options.get('groupChar', _DEFAULT_GROUP_CHAR)
17-
decimal_char = options.get('decimalChar', _DEFAULT_DECIMAL_CHAR)
18-
if not isinstance(value, Decimal):
19-
if isinstance(value, six.string_types):
20-
value = re.sub(r'\s', '', value)
21-
value = value.replace(decimal_char, '__decimal_char__')
16+
if isinstance(value, six.string_types):
17+
group_char = options.get('groupChar', _DEFAULT_GROUP_CHAR)
18+
decimal_char = options.get('decimalChar', _DEFAULT_DECIMAL_CHAR)
19+
value = _RE_WHITESPACE.sub('', value)
20+
if decimal_char != '.':
21+
if group_char:
22+
value = value.replace(decimal_char, '__decimal_char__')
23+
value = value.replace(group_char, '')
24+
value = value.replace('__decimal_char__', '.')
25+
else:
26+
value = value.replace(decimal_char, '__decimal_char__')
27+
value = value.replace('__decimal_char__', '.')
28+
elif group_char:
2229
value = value.replace(group_char, '')
23-
value = value.replace('__decimal_char__', '.')
24-
if not options.get('bareNumber', _DEFAULT_BARE_NUMBER):
25-
value = re.sub(r'((^\D*)|(\D*$))', '', value)
26-
elif not isinstance(value, six.integer_types + (float,)):
27-
return ERROR
28-
elif value is True or value is False:
29-
return ERROR
30-
try:
31-
if isinstance(value, float):
32-
value = str(value)
33-
value = Decimal(value)
34-
except Exception:
35-
return ERROR
30+
31+
if not options.get('bareNumber', _DEFAULT_BARE_NUMBER):
32+
value = _RE_BARE_NUMBER.sub('', value)
33+
elif isinstance(value, Decimal):
34+
return value
35+
elif not isinstance(value, six.integer_types + (float,)):
36+
return ERROR
37+
elif value is True or value is False:
38+
return ERROR
39+
else:
40+
value = str(value)
41+
try:
42+
value = Decimal(value)
43+
except Exception:
44+
return ERROR
3645
return value
3746

3847

3948
# Internal
4049

50+
_RE_WHITESPACE = re.compile(r'\s')
51+
_RE_BARE_NUMBER = re.compile(r'((^\D*)|(\D*$))')
4152
_DEFAULT_GROUP_CHAR = ''
4253
_DEFAULT_DECIMAL_CHAR = '.'
4354
_DEFAULT_BARE_NUMBER = True

tableschema/types/string.py

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
def cast_string(format, value, **options):
2020
if not isinstance(value, six.string_types):
2121
return ERROR
22+
if format in _SIMPLE_FORMATS:
23+
return value
2224
if format == 'uri':
2325
uri = _uri_from_string(value)
2426
try:
@@ -43,6 +45,7 @@ def cast_string(format, value, **options):
4345

4446
# Internal
4547

48+
_SIMPLE_FORMATS = {'default', None}
4649
_EMAIL_PATTERN = re.compile(r'[^@]+@[^@]+\.[^@]+')
4750
_uri_from_string = rfc3986.uri.URIReference.from_string
4851
_uri_validator = rfc3986.validators.Validator().require_presence_of('scheme')

0 commit comments

Comments
 (0)