class PythonClass(object):
            def __init__(self, arg):
                self.value = arg

            @staticmethod
            @specs.meta('Usage', 'Extension')
            @specs.parameter('arg', yaqltypes.Integer())
            def python_extension(arg):
                return arg * arg

            @classmethod
            @specs.meta('Usage', 'Extension')
            @specs.parameter('arg', yaqltypes.Integer())
            def python_extension2(cls, arg):
                return cls(2 * arg).value
Esempio n. 2
0
    def test_bool_is_not_an_integer(self):
        @specs.parameter('arg', yaqltypes.Integer())
        def foo(arg):
            return arg

        self.context.register_function(foo)
        self.assertEqual(2, self.eval('foo(2)'))
        self.assertRaises(exceptions.NoMatchingFunctionException, self.eval,
                          'foo(true)')
Esempio n. 3
0
        class PythonClass(object):
            @staticmethod
            @specs.parameter('arg', yaqltypes.Integer())
            @specs.inject('receiver', yaqltypes.Receiver())
            def static_python_method(arg, receiver):
                if isinstance(receiver, dsl_types.MuranoObjectInterface):
                    return 3 * arg
                return 7 * arg

            @classmethod
            @specs.inject('receiver', yaqltypes.Receiver())
            def classmethod_python_method(cls, arg, receiver):
                if isinstance(receiver, dsl_types.MuranoObjectInterface):
                    return cls.__name__.upper() + str(arg)
                return cls.__name__ + str(arg)
Esempio n. 4
0
    def test_any_of(self):
        @specs.parameter(
            'arg', yaqltypes.AnyOf(str, yaqltypes.Integer()))
        def foo(arg):
            if isinstance(arg, str):
                return 1
            if isinstance(arg, int):
                return 2

        self.context.register_function(foo)
        self.assertEqual(1, self.eval('foo($)', data='abc'))
        self.assertEqual(2, self.eval('foo($)', data=123))
        self.assertRaises(
            exceptions.NoMatchingFunctionException,
            self.eval, 'foo($)', data=True)
        self.assertRaises(
            exceptions.NoMatchingFunctionException,
            self.eval, 'foo($)', data=[1, 2])
Esempio n. 5
0
@specs.parameter('format__', yaqltypes.String(True))
def datetime_from_string(string, format__=None):
    if not format__:
        result = parser.parse(string)
    else:
        result = DATETIME_TYPE.strptime(string, format__)
    if not result.tzinfo:
        return result.replace(tzinfo=UTCTZ)
    return result


@specs.name('timespan')
@specs.parameter('days', int)
@specs.parameter('hours', int)
@specs.parameter('minutes', int)
@specs.parameter('seconds', yaqltypes.Integer())
@specs.parameter('milliseconds', yaqltypes.Integer())
@specs.parameter('microseconds', yaqltypes.Integer())
def build_timespan(days=0,
                   hours=0,
                   minutes=0,
                   seconds=0,
                   milliseconds=0,
                   microseconds=0):
    return TIMESPAN_TYPE(days=days,
                         hours=hours,
                         minutes=minutes,
                         seconds=seconds,
                         milliseconds=milliseconds,
                         microseconds=microseconds)