Add methods to check value type

The following methods were missing:
isInteger
isNumber
isRegex
isDatetime
isTimespan
This patch adds them.

Change-Id: I60d9dff29a2b247d4d86004fde3dcfb2c850ec5f
This commit is contained in:
Tetiana Lashchova 2016-02-09 18:00:32 +02:00
parent f2e0604150
commit 2ebc14e61e
7 changed files with 61 additions and 1 deletions

View File

@ -379,6 +379,14 @@ def format_(dt, format__):
return dt.strftime(format__)
def is_datetime(value):
return isinstance(value, DATETIME_TYPE)
def is_timespan(value):
return isinstance(value, TIMESPAN_TYPE)
def register(context):
functions = (
build_datetime, build_timespan, datetime_from_timestamp,
@ -394,7 +402,8 @@ def register(context):
negative_timespan, positive_timespan,
timespan_by_num, num_by_timespan, div_timespans, div_timespan_by_num,
year, month, day, hour, minute, second, microsecond, weekday,
offset, timestamp, date, time, replace, format_
offset, timestamp, date, time, replace, format_, is_datetime,
is_timespan
)
for func in functions:

View File

@ -196,6 +196,15 @@ def round_(number, ndigits=0):
return round(number, ndigits)
def is_integer(value):
return isinstance(value, six.integer_types) and not isinstance(value, bool)
def is_number(value):
return (isinstance(value, six.integer_types + (float,))
and not isinstance(value, bool))
def register(context):
context.register_function(binary_plus)
context.register_function(binary_minus)
@ -224,3 +233,5 @@ def register(context):
context.register_function(pow_)
context.register_function(sign)
context.register_function(round_)
context.register_function(is_integer)
context.register_function(is_number)

View File

@ -193,6 +193,10 @@ def escape_regex(string):
return re.escape(string)
def is_regex(value):
return isinstance(value, REGEX_TYPE)
def register(context):
context.register_function(regex)
context.register_function(matches)
@ -210,3 +214,4 @@ def register(context):
context.register_function(replace_string)
context.register_function(replace_by_string)
context.register_function(escape_regex)
context.register_function(is_regex)

View File

@ -54,3 +54,9 @@ class TestBoolean(yaql.tests.TestCase):
self.assertFalse(self.eval('false = true'))
self.assertFalse(self.eval('false != false'))
self.assertFalse(self.eval('true != true'))
def test_is_boolean(self):
self.assertTrue(self.eval('isBoolean(true)'))
self.assertTrue(self.eval('isBoolean(false)'))
self.assertFalse(self.eval('isBoolean(123)'))
self.assertFalse(self.eval('isBoolean(abc)'))

View File

@ -159,3 +159,13 @@ class TestDatetime(yaql.tests.TestCase):
self.assertFalse(self.eval('$dt2 - $dt1 < $', delta2))
self.assertFalse(self.eval('$dt2 - $dt1 <= $', delta2))
self.assertTrue(self.eval('($dt2 - $dt1) + $ > $', delta2))
def test_is_datetime(self):
self.assertTrue(self.eval('isDatetime(datetime("2015-8-29"))'))
self.assertFalse(self.eval('isDatetime(123)'))
self.assertFalse(self.eval('isDatetime(abc)'))
def test_is_timespan(self):
self.assertTrue(self.eval('isTimespan(timespan(milliseconds => -1))'))
self.assertFalse(self.eval('isTimespan(123)'))
self.assertFalse(self.eval('isTimespan(abc)'))

View File

@ -200,3 +200,17 @@ class TestMath(yaql.tests.TestCase):
def test_round(self):
self.assertAlmostEqual(2.0, self.eval('round(2.3)'))
self.assertAlmostEqual(2.3, self.eval('round(2.345, 1)'))
def test_is_integer(self):
self.assertTrue(self.eval('isInteger(-2)'))
self.assertTrue(self.eval('isInteger(2)'))
self.assertFalse(self.eval('isInteger(2.3)'))
self.assertFalse(self.eval('isInteger(abc)'))
self.assertFalse(self.eval('isInteger(true)'))
def test_is_number(self):
self.assertTrue(self.eval('isNumber(-2)'))
self.assertTrue(self.eval('isNumber(2)'))
self.assertTrue(self.eval('isNumber(2.3)'))
self.assertFalse(self.eval('isNumber(abc)'))
self.assertFalse(self.eval('isNumber(true)'))

View File

@ -138,3 +138,8 @@ class TestRegex(yaql.tests.TestCase):
self.assertEqual(
'\\[',
self.eval(r"escapeRegex('[')"))
def test_is_regex(self):
self.assertTrue(self.eval('isRegex(regex("a.b"))'))
self.assertFalse(self.eval('isRegex(123)'))
self.assertFalse(self.eval('isRegex(abc)'))