Skip to content

Commit

Permalink
Merge pull request #78 from thinkt4nk/release-0_13_0
Browse files Browse the repository at this point in the history
Release 0.13.0
  • Loading branch information
thinkt4nk authored Mar 7, 2017
2 parents 166931f + 8171eae commit 9b01d25
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
20 changes: 20 additions & 0 deletions data_schema/convert_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ def _preprocess_value(self, value, format_str, transform_case=None):
return value


class DurationConverter(ValueConverter):
"""
Converts durations from [hh]:mm:ss format to integer number of seconds
"""
TIME_FORMAT_DURATION_REGEXP = re.compile(r'^\d{1,2}:\d{1,2}(:\d{1,2})?$')

def __call__(self, value, format_str, default_value, transform_case=None):
if self.is_string(value) and self.TIME_FORMAT_DURATION_REGEXP.match(value) is not None:
return super(DurationConverter, self).__call__(value, format_str, default_value, transform_case)
return NumericConverter(FieldSchemaType.INT, int)(value, format_str, default_value, transform_case)

def _convert_value(self, value, format_str):
duration_constituents = value.split(':')
value = int(duration_constituents[-2]) * 60 + int(duration_constituents[-1])
if len(duration_constituents) == 3:
value += int(duration_constituents[0]) * 3600
return value


class DatetimeConverter(ValueConverter):
"""
Converts datetime values (date and datetime).
Expand Down Expand Up @@ -161,6 +180,7 @@ def _preprocess_value(self, value, format_str, transform_case=None):
FieldSchemaType.FLOAT: NumericConverter(FieldSchemaType.FLOAT, float),
FieldSchemaType.STRING: StringConverter(FieldSchemaType.STRING, six.text_type),
FieldSchemaType.BOOLEAN: BooleanConverter(FieldSchemaType.BOOLEAN, bool),
FieldSchemaType.DURATION: DurationConverter(FieldSchemaType.DURATION, int),
}


Expand Down
1 change: 1 addition & 0 deletions data_schema/field_schema_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class FieldSchemaType(object):
FLOAT = 'FLOAT'
STRING = 'STRING'
BOOLEAN = 'BOOLEAN'
DURATION = 'DURATION'

@classmethod
def choices(cls):
Expand Down
33 changes: 33 additions & 0 deletions data_schema/tests/convert_value_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,36 @@ def test_convert_value_default(self):
"""
self.assertTrue(convert_value(FieldSchemaType.BOOLEAN, None, default_value=True))
self.assertIsNone(convert_value(FieldSchemaType.BOOLEAN, 'invalid', default_value=True))


class DurationConverterTest(SimpleTestCase):

def test_convert_valid_simple_number(self):
"""
Verifies that a simple number conversion will be used
"""
self.assertEqual(70, convert_value(FieldSchemaType.DURATION, '70'))

def test_convert_minutes_seconds(self):
"""
Verifies that a string of the format mm:ss will be converted correctly
"""
self.assertEqual(65, convert_value(FieldSchemaType.DURATION, '01:05'))
self.assertEqual(65, convert_value(FieldSchemaType.DURATION, '1:5'))
self.assertEqual(65, convert_value(FieldSchemaType.DURATION, '01:5'))

def test_convert_hours_minutes_seconds(self):
"""
Verifies that a string of the format hh:mm:ss will be converted correctly
"""
self.assertEqual(7265, convert_value(FieldSchemaType.DURATION, '02:01:05'))
self.assertEqual(7265, convert_value(FieldSchemaType.DURATION, '02:01:5'))
self.assertEqual(7265, convert_value(FieldSchemaType.DURATION, '02:1:5'))
self.assertEqual(7265, convert_value(FieldSchemaType.DURATION, '2:1:5'))

def test_convert_invalid(self):
"""
Verifies that an altogether invalid string results in a value of None
"""
self.assertIsNone(convert_value(FieldSchemaType.DURATION, 'sup'))
self.assertIsNone(convert_value(FieldSchemaType.DURATION, ':::'))
1 change: 1 addition & 0 deletions data_schema/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_choices(self):
('FLOAT', 'FLOAT'),
('STRING', 'STRING'),
('BOOLEAN', 'BOOLEAN'),
('DURATION', 'DURATION'),
])
self.assertEquals(expected_choices, set(FieldSchemaType.choices()))

Expand Down
2 changes: 1 addition & 1 deletion data_schema/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.12.0'
__version__ = '0.13.0'

0 comments on commit 9b01d25

Please sign in to comment.