예제 #1
0
    def test_known_variables(self):
        """
        Test whether known variables work and whether undefined variables raise
        exceptions.
        """

        with self.assertRaisesError('Cannot override keyword True',
                                    exception=NameError):
            parser = expression.Expression_Parser(variables={'True': 42})

        parser = expression.Expression_Parser()
        self.assertIsNone(parser.parse('None'))
        with self.assertRaisesError("NameError: Name 'test' is not defined"):
            parser.parse('test')
예제 #2
0
    def __call__(self, x):
        e = expression.Expression_Parser(variables={"MAX": self.MAX},
                                         functions={
                                             "min": min,
                                             "max": max
                                         })
        try:
            ret = round(e.parse(x))
        except:
            print("Unable to parse expression for concurrency")
            raise ValueError(x)

        if ret <= 0:
            print("concurrency must be at least 1")
            raise ValueError(x)

        if ("MAX" not in x and ret >= self.MAX) or ret > self.MAX:
            print((
                "\nYou cannot have concurrency {} but at most:\n"
                "  a) {} with '--concurrency MAX';\n"
                "  b) {} otherwise.\n"
                "Please use option a) only if your computer is very lightly loaded.\n"
            ).format(
                ret,
                self.MAX,
                self.MAX - 1,
            ))
            raise ValueError(x)

        return x, ret
예제 #3
0
 def setUp(self):
     super(Expression_Parser_Test, self).setUp()
     variables = {
         'data': [1, 2, 3]
     }
     functions = {
         'square': lambda x, y=2: x ** y
     }
     self.parser = expression.Expression_Parser(variables=variables,
                                                functions=functions)
예제 #4
0
    def __call__(self, x):
        e = expression.Expression_Parser(variables={"MAX": self.MAX},
                                         functions={
                                             "min": min,
                                             "max": max
                                         })
        try:
            ret = round(max(min(e.parse(x), self.MAX), 0))
        except:
            print("Unable to parse expression for max_memory")
            raise ValueError(x)

        return x, ret
예제 #5
0
    def test_functions(self):
        """
        Test whether known functions work and whether undefined functions raise
        exceptions.
        """

        self.assertEqual(self.parser.parse('int(4.2)'), 4)
        self.assertEqual(self.parser.parse('square(4)'), 16)
        self.assertEqual(self.parser.parse('square(3, 3)'), 27)
        self.assertEqual(self.parser.parse('square(2, y=3)'), 8)

        parser = expression.Expression_Parser(functions={'x2': lambda: 2})
        with self.assertRaisesError("NameError: Function 'x1' is not defined"):
            parser.parse('x1()')

        with self.assertRaisesError(r"TypeError: .* takes (no|0.*) arguments"):
            parser.parse('x2(1,2,3)')

        with self.assertRaisesError("Star arguments are not supported"):
            parser.parse('x2(1, *data)')
def parseExpression(ledexpression):

    functions = {
        "sequence": ledring.sequence,
        "slow": ledring.slow,
        "fast": ledring.fast,
        "parallel": ledring.parallel,
        "parallel2": ledring.parallel2,
        "shift": ledring.shift,
        "cglinear": ledring.linear_color,
        "cgcolor": ledring.fixed_color,
        "cgrainbow": ledring.rainbow_color,
        # switch colors at each step
        "cgswitch": ledring.switch_color,

        #### Frame constructor functions
        "fg": ledring.fg,
        #
        "fpattern": ledring.fill_patterns,
        "fadd": ledring.add,
        "fpixel": ledring.pixel,
        "fring": ledring.ring,
        "fdots": ledring.dots,

        ### Frame generator functions

        # clear
        "clear": ledring.clear,

        # take color, and speed (default to 5)
        "flash": ledring.flash,

        # take color
        "rain": ledring.rain,

        # create square patterns, take color generator, nbpatterns, square pattern size, and shift
        "square": ledring.colored_square,

        # random dots moves
        "random": ledring.randomDotColor,

        # dots animation
        "dotanim": ledring.dotAnim,

        # dot animation with color generator
        "dotanimcg": ledring.dotAnimCg,

        # take direction, and associated color
        "movering": ledring.movering,

        # take colors
        "wave": ledring.wave
    }

    variables = {
        "black": black,
        "green": green,
        "red": red,
        "blue": blue,
        "white": white,

        # colors from https://materialuicolors.co/
        "uipink": uipink,
        "uired": uired,
        "uiblue": uiblue,
        "uilightblue": uilightblue,
        "uipurple": uipurple,
        "uideeppurple": uideeppurple,
        "uiindigo": uiindigo,
        "uicyan": uicyan,
        "uiteal": uiteal,
        "uigreen": uigreen,
        "uilightgreen": uilightgreen,
        "uilime": uilime,
        "uiyellow": uiyellow,
        "uiamber": uiamber,
        "uiorange": uiorange,
        "uideeporange": uideeporange,
        "uibrown": uibrown,
        "uigrey": uigrey,
        "uibluegrey": uibluegrey
    }

    parser = expression.Expression_Parser(variables=variables,
                                          functions=functions,
                                          assignment=False)
    e = parser.parse(ledexpression)
    return e
예제 #7
0
def solve_vectors(formula, var2val={}):
    formula = formula.replace('[', '').replace(']', '')

    import expression
    parser = expression.Expression_Parser(variables=var2val)
    return parser.parse(formula)
예제 #8
0
 def __init__(self):
     cmd.Cmd.__init__(self)
     self.prompt = '>> '
     self.parser = expression.Expression_Parser(assignment=True)