コード例 #1
0
    def test_medium(self):
        # medium test case
        function, arguments, evaluations, success, message = dexi.parse_function(
            self._MEDIUM_TEST)
        self.assertEqual(
            function,
            [[(0, 0), 0], [(0, 1), 1], [(0, 2), 2], [(1, 0), 1], [(1, 1), 2],
             [(1, 2), 3], [(2, 0), 2], [(2, 1), 3], [(2, 2), 4]])
        self.assertEqual(arguments, ["f", "s"])
        self.assertEqual(evaluations, [["1", "1"], ["2", "2"], ["3", "3.5"]])
        self.assertEqual(success, True)
        self.assertEqual(message, "")

        derivatives, evals, image, success, message = dexi.get_derivatives(
            function, evaluations, arguments, False)
        self.assertEqual(derivatives, ["1.00"] * 20)
        self.assertEqual(evals, [(["1", "1"], "2.00"), (["2", "2"], "4.00"),
                                 (["3", "3.5"], "6.50")])

        self.assertNotEqual(image, "")
        self.assertEqual(image[-4:], ".png")
        for c in image[:-4]:
            self.assertTrue(c in self._POSSIBLE_CHARS)

        self.assertEqual(success, True)
        self.assertEqual(message, "")
コード例 #2
0
ファイル: test.py プロジェクト: nejctrdin/DFD
    def test_large_newlines(self):
        # large test case with newlines
        function, arguments, evaluations, success, message = dexi.parse_function(self._LARGE_TEST_NEWLINE)
        self.assertEqual(function, [[(0, 0, 0), 4], [(0, 0, 1), 2], [(0, 0, 2), 3], [(0, 1, 0), 1], [(0, 1, 1), 2],
                                    [(0, 1, 2), 0], [(0, 2, 0), 1], [(0, 2, 1), 2], [(0, 2, 2), 4], [(1, 0, 0), 3],
                                    [(1, 0, 1), 2], [(1, 0, 2), 1], [(1, 1, 0), 2], [(1, 1, 1), 0], [(1, 1, 2), 1],
                                    [(1, 2, 0), 2], [(1, 2, 1), 1], [(1, 2, 2), 2], [(2, 0, 0), 0], [(2, 0, 1), 1],
                                    [(2, 0, 2), 3], [(2, 1, 0), 0], [(2, 1, 1), 2], [(2, 1, 2), 4], [(2, 2, 0), 4],
                                    [(2, 2, 1), 2], [(2, 2, 2), 1]])
        self.assertEqual(arguments, ["f", "s", "t"])
        self.assertEqual(evaluations, [["1", "2", "3"], ["0.1", "0.2", "0.3"]])
        self.assertEqual(success, True)
        self.assertEqual(message, "")

        derivatives, evals, image, success, message = dexi.get_derivatives(function, evaluations, arguments)
        self.assertEqual(derivatives, ["-1.00", "0.00", "-2.00", "1.00", "-2.00", "1.00", "1.00", "-1.00", "-2.00",
                                       "-2.00", "-0.50", "-0.00", "-0.50", "-0.00", "2.00", "1.50", "-0.00", "-1.50",
                                       "-3.00", "-1.00", "2.00", "-2.00", "2.00", "3.00", "2.00", "1.00", "-1.00",
                                       "-0.11", "-3.00", "0.00", "-3.00", "-1.50", "0.00", "0.50", "0.00", "0.00",
                                       "4.00", "-1.00", "-2.00", "0.00", "-0.50", "-0.50", "0.50", "0.00", "1.00",
                                       "1.00", "0.00", "1.00", "1.00", "2.00", "0.50", "-1.00", "4.00", "0.00",
                                       "-3.00", "0.00", "-2.00", "-0.50", "1.00", "1.00", "-0.50", "-2.00", "1.00",
                                       "1.50", "2.00", "-1.00", "-1.00", "-1.00", "-2.00", "-0.50", "1.00", "-1.00",
                                       "-0.00", "1.00", "1.00", "1.50", "2.00", "2.00", "2.00", "2.00", "-2.00",
                                       "-1.50", "-1.00", "0.11"])
        self.assertEqual(evals, [(["1", "2", "3"], "3.00"), (["0.1", "0.2", "0.3"], "2.93")])
        self.assertEqual(image, "")
        self.assertEqual(success, True)
        self.assertEqual(message, "")
コード例 #3
0
ファイル: test.py プロジェクト: nejctrdin/DFD
 def test_multiplicities_type(self):
     # test if multiplicities can be cast to integers
     function, arguments, evaluations, success, message = dexi.parse_function(self._INCORRECT_MULTIPLICITIES_TYPE)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(message, "Multiplicities should be integers! Muliplicity 2 is not!")
コード例 #4
0
ファイル: test.py プロジェクト: nejctrdin/DFD
 def test_eval_arguments_type(self):
     # test if types of evaluation arguments can be cast to floats
     function, arguments, evaluations, success, message = dexi.parse_function(self._INCORRECT_EVAL_ARGS_TYPE)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(message, "All function evaluations should be floats ['a']!")
コード例 #5
0
ファイル: test.py プロジェクト: nejctrdin/DFD
 def test_func_output_type(self):
     # test if inputs to function can be cast to ints
     function, arguments, evaluations, success, message = dexi.parse_function(self._INCORRECT_FUN_OUTPUT)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(message, "All function outputs should be floats!")
コード例 #6
0
ファイル: test.py プロジェクト: nejctrdin/DFD
 def test_empty(self):
     # test if empty function produces errors
     function, arguments, evaluations, success, message = dexi.parse_function(self._EMPTY_FUNCTION)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(message, "Function should be represented in at least 3 lines!")
コード例 #7
0
ファイル: test.py プロジェクト: nejctrdin/DFD
 def test_input_output_size(self):
     # test if function output length is equal to the multiplied multiplicities
     function, arguments, evaluations, success, message = dexi.parse_function(self._INCORRECT_N_INPUT_OUTPUT)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(message, "The input space size (12) does not match output space size (9)!")
コード例 #8
0
ファイル: test.py プロジェクト: nejctrdin/DFD
 def test_multiplicities(self):
     # test if the number of arguments matches the number of supplied multiplicities
     function, arguments, evaluations, success, message = dexi.parse_function(self._INCORRECT_MULTIPLICITIES)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(message, "Number of function arguments (2) and number of multiplicities (3) should be equal!")
コード例 #9
0
ファイル: test.py プロジェクト: nejctrdin/DFD
 def test_eval_arguments(self):
     # test if evaluation arguments have the same length as the inputs
     function, arguments, evaluations, success, message = dexi.parse_function(self._INCORRECT_EVAL_ARGS)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(message, "Number of function arguments (1) does not match number of supplied evaluation arguments (2) - ['1', '2'].")
コード例 #10
0
 def test_func_output_type(self):
     # test if inputs to function can be cast to ints
     function, arguments, evaluations, success, message = dexi.parse_function(
         self._INCORRECT_FUN_OUTPUT)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(message, "All function outputs should be floats!")
コード例 #11
0
 def test_eval_arguments_type(self):
     # test if types of evaluation arguments can be cast to floats
     function, arguments, evaluations, success, message = dexi.parse_function(
         self._INCORRECT_EVAL_ARGS_TYPE)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(message,
                      "All function evaluations should be floats ['a']!")
コード例 #12
0
 def test_empty(self):
     # test if empty function produces errors
     function, arguments, evaluations, success, message = dexi.parse_function(
         self._EMPTY_FUNCTION)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(
         message, "Function should be represented in at least 3 lines!")
コード例 #13
0
def animation():
    raw_function = ""
    if "names" in request.form:
        # if "names" is in the request, it means it was called from the already
        # created derivatives - the user changed the inputs and retried the
        # derivative creation
        values = {}
        # we read the fields in the request and parse out those that start with a v
        for field in request.form:
            if field[0] == "v":
                # the values and the entries are put into a dictionary
                # before that the keys have "v"s removed and cast to int
                tmp_f = int(field.replace("v", ""))
                values[tmp_f] = request.form[field]

        value_list = []
        # we sort the keys of the dictionary, to get the initial order
        # and put the values into a value list
        for field in sorted(values):
            value_list.append(values[field])

        # function is described in three lines
        function_description = []
        # output values joined, delimited with a comma
        function_description.append(",".join(value_list))
        # names are delimited by a comma (already in the request)
        function_description.append(request.form["names"])
        # multiplicities are delimited by a comma (already in the request)
        function_description.append(request.form["multiplicity"])

        # the raw function is represented as a new-line delimitied string
        raw_function = str("\n".join(function_description))

    else:
        return _MISSING_ARGUMENTS

    # we are interested in the time that took for generating the animation
    t = time()
    # we parse the function, getting the actual function, arguments, needed evaluations
    # success status and possible message
    function, arguments, evaluations, success, message = parse_function(
        raw_function)

    if success:
        # if the previous step succeeded we can construct the function, get derivatives,
        # evaluations and the possible image
        anim_file_name = _create_animation(function, arguments)
        print "Function animation needed {0}s to execute!".format(
            _format_number(time() - t))
    else:
        return _COULD_NOT_PARSE_FUNCTION.format(message)

    # the derivatives page is rendered
    return anim_file_name
コード例 #14
0
 def test_multiplicities_type(self):
     # test if multiplicities can be cast to integers
     function, arguments, evaluations, success, message = dexi.parse_function(
         self._INCORRECT_MULTIPLICITIES_TYPE)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(
         message,
         "Multiplicities should be integers! Muliplicity 2 is not!")
コード例 #15
0
 def test_input_output_size(self):
     # test if function output length is equal to the multiplied multiplicities
     function, arguments, evaluations, success, message = dexi.parse_function(
         self._INCORRECT_N_INPUT_OUTPUT)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(
         message,
         "The input space size (12) does not match output space size (9)!")
コード例 #16
0
 def test_eval_arguments(self):
     # test if evaluation arguments have the same length as the inputs
     function, arguments, evaluations, success, message = dexi.parse_function(
         self._INCORRECT_EVAL_ARGS)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(
         message,
         "Number of function arguments (1) does not match number of supplied evaluation arguments (2) - ['1', '2']."
     )
コード例 #17
0
    def test_content_examples(self):
        for description, example in content._EXAMPLES:
            inp_line = example.replace(" ", "\n")
            self.assertNotEqual(description, "")

            function, arguments, evaluations, success, message = dexi.parse_function(
                inp_line)

            self.assertNotEqual(function, [])
            for point, output in function:
                for arg in point:
                    self.assertTrue(type(arg) is int)
                self.assertTrue(type(output) is float)

            self.assertNotEqual(arguments, [])
            for arg in arguments:
                self.assertNotEqual(arg, "")

            if evaluations:
                for evaluation in evaluations:
                    for point in evaluation:
                        self.assertTrue(type(point) is str)
                        self.assertTrue(type(float(point)) is float)
            self.assertEqual(success, True)
            self.assertEqual(message, "")

            derivatives, evals, image, success, message = dexi.get_derivatives(
                function, evaluations, arguments, False)

            self.assertEqual(len(derivatives),
                             (len(function) + 1) * len(arguments))

            for derivative in derivatives:
                self.assertTrue(type(derivative) is str)
                self.assertTrue(type(float(derivative)) is float)

            if evaluations:
                self.assertNotEqual(evals, [])
                for point, evaluation in evals:
                    self.assertEqual(len(point), len(arguments))
                    for p in point:
                        self.assertTrue(type(p) is str)
                        self.assertTrue(type(float(p)) is float)
                    self.assertTrue(type(evaluation) is str)
                    self.assertTrue(type(float(evaluation)) is float)

            if len(arguments) < 3:
                self.assertRegexpMatches(image, "[a-zA-Z0-9]{10}\.png")
            else:
                self.assertEqual(image, "")

            self.assertEqual(success, True)
            self.assertEqual(message, "")
コード例 #18
0
 def test_multiplicities(self):
     # test if the number of arguments matches the number of supplied multiplicities
     function, arguments, evaluations, success, message = dexi.parse_function(
         self._INCORRECT_MULTIPLICITIES)
     self.assertEqual(function, None)
     self.assertEqual(arguments, None)
     self.assertEqual(evaluations, None)
     self.assertEqual(success, False)
     self.assertEqual(
         message,
         "Number of function arguments (2) and number of multiplicities (3) should be equal!"
     )
コード例 #19
0
ファイル: server.py プロジェクト: nejctrdin/DFD
def animation():
    raw_function = ""
    if "names" in request.form:
        # if "names" is in the request, it means it was called from the already
        # created derivatives - the user changed the inputs and retried the
        # derivative creation
        values = {}
        # we read the fields in the request and parse out those that start with a v
        for field in request.form:
            if field[0] == "v":
                # the values and the entries are put into a dictionary
                # before that the keys have "v"s removed and cast to int
                tmp_f = int(field.replace("v",""))
                values[tmp_f] = request.form[field]

        value_list = []
        # we sort the keys of the dictionary, to get the initial order
        # and put the values into a value list
        for field in sorted(values):
            value_list.append(values[field])

        # function is described in three lines
        function_description = []
        # output values joined, delimited with a comma
        function_description.append(",".join(value_list))
        # names are delimited by a comma (already in the request)
        function_description.append(request.form["names"])
        # multiplicities are delimited by a comma (already in the request)
        function_description.append(request.form["multiplicity"])

        # the raw function is represented as a new-line delimitied string
        raw_function = str("\n".join(function_description))

    else:
        return _MISSING_ARGUMENTS

    # we are interested in the time that took for generating the animation
    t = time()
    # we parse the function, getting the actual function, arguments, needed evaluations
    # success status and possible message
    function, arguments, evaluations, success, message = parse_function(raw_function)

    if success:
        # if the previous step succeeded we can construct the function, get derivatives,
        # evaluations and the possible image
        anim_file_name = _create_animation(function, arguments)
        print "Function animation needed {0}s to execute!".format(_format_number(time() - t))
    else:
        return _COULD_NOT_PARSE_FUNCTION.format(message)

    # the derivatives page is rendered
    return anim_file_name 
コード例 #20
0
ファイル: test.py プロジェクト: nejctrdin/DFD
    def test_content_examples(self):
        for description, example in content._EXAMPLES:
            inp_line = example.replace(" ", "\n")
            self.assertNotEqual(description, "")

            function, arguments, evaluations, success, message = dexi.parse_function(inp_line)

            self.assertNotEqual(function, [])
            for point, output in function:
                for arg in point:
                    self.assertTrue(type(arg) is int)
                self.assertTrue(type(output) is float)

            self.assertNotEqual(arguments, [])
            for arg in arguments:
                self.assertNotEqual(arg, "")

            if evaluations:
                for evaluation in evaluations:
                    for point in evaluation:
                        self.assertTrue(type(point) is str)
                        self.assertTrue(type(float(point)) is float)
            self.assertEqual(success, True)
            self.assertEqual(message, "")

            derivatives, evals, image, success, message = dexi.get_derivatives(function, evaluations, arguments, False)

            self.assertEqual(len(derivatives), (len(function) + 1)*len(arguments))

            for derivative in derivatives:
                self.assertTrue(type(derivative) is str)
                self.assertTrue(type(float(derivative)) is float)

            if evaluations:
                self.assertNotEqual(evals, [])
                for point, evaluation in evals:
                    self.assertEqual(len(point), len(arguments))
                    for p in point:
                        self.assertTrue(type(p) is str)
                        self.assertTrue(type(float(p)) is float)
                    self.assertTrue(type(evaluation) is str)
                    self.assertTrue(type(float(evaluation)) is float)

            if len(arguments) < 3:
                self.assertRegexpMatches(image, "[a-zA-Z0-9]{10}\.png")
            else:
                self.assertEqual(image, "")

            self.assertEqual(success, True)
            self.assertEqual(message, "")
コード例 #21
0
ファイル: test.py プロジェクト: nejctrdin/DFD
    def test_small(self):
        # a small test case
        function, arguments, evaluations, success, message = dexi.parse_function(self._SMALL_TEST)
        self.assertEqual(function, [[(0,), 0], [(1,), 1], [(2,), 2]])
        self.assertEqual(arguments, ["f"])
        self.assertEqual(evaluations, [["1"], ["2"], ["3"]])
        self.assertEqual(success, True)
        self.assertEqual(message, "")

        derivatives, evals, image, success, message = dexi.get_derivatives(function, evaluations, arguments, False)
        self.assertEqual(derivatives, ["1.00"] * 4);
        self.assertEqual(evals, [(["1"], "1.00"), (["2"], "2.00"), (["3"], "3.00")])

        self.assertNotEqual(image, "")
        self.assertEqual(image[-4:], ".png")
        for c in image[:-4]:
            self.assertTrue(c in self._POSSIBLE_CHARS)

        self.assertEqual(success, True)
        self.assertEqual(message, "")
コード例 #22
0
ファイル: test.py プロジェクト: nejctrdin/DFD
    def test_medium(self):
        # medium test case
        function, arguments, evaluations, success, message = dexi.parse_function(self._MEDIUM_TEST)
        self.assertEqual(function, [[(0, 0), 0], [(0, 1), 1], [(0, 2), 2], [(1, 0), 1], [(1, 1), 2],
                                    [(1, 2), 3], [(2, 0), 2], [(2, 1), 3], [(2, 2), 4]])
        self.assertEqual(arguments, ["f", "s"])
        self.assertEqual(evaluations, [["1", "1"], ["2", "2"], ["3", "3.5"]])
        self.assertEqual(success, True)
        self.assertEqual(message, "")

        derivatives, evals, image, success, message = dexi.get_derivatives(function, evaluations, arguments, False)
        self.assertEqual(derivatives, ["1.00"] * 20);
        self.assertEqual(evals, [(["1", "1"], "2.00"), (["2", "2"], "4.00"), (["3", "3.5"], "6.50")])

        self.assertNotEqual(image, "")
        self.assertEqual(image[-4:], ".png")
        for c in image[:-4]:
            self.assertTrue(c in self._POSSIBLE_CHARS)
        
        self.assertEqual(success, True)
        self.assertEqual(message, "")
コード例 #23
0
    def test_large_newlines(self):
        # large test case with newlines
        function, arguments, evaluations, success, message = dexi.parse_function(
            self._LARGE_TEST_NEWLINE)
        self.assertEqual(
            function,
            [[(0, 0, 0), 4], [(0, 0, 1), 2], [(0, 0, 2), 3], [(0, 1, 0), 1],
             [(0, 1, 1), 2], [(0, 1, 2), 0], [(0, 2, 0), 1], [(0, 2, 1), 2],
             [(0, 2, 2), 4], [(1, 0, 0), 3], [(1, 0, 1), 2], [(1, 0, 2), 1],
             [(1, 1, 0), 2], [(1, 1, 1), 0], [(1, 1, 2), 1], [(1, 2, 0), 2],
             [(1, 2, 1), 1], [(1, 2, 2), 2], [(2, 0, 0), 0], [(2, 0, 1), 1],
             [(2, 0, 2), 3], [(2, 1, 0), 0], [(2, 1, 1), 2], [(2, 1, 2), 4],
             [(2, 2, 0), 4], [(2, 2, 1), 2], [(2, 2, 2), 1]])
        self.assertEqual(arguments, ["f", "s", "t"])
        self.assertEqual(evaluations, [["1", "2", "3"], ["0.1", "0.2", "0.3"]])
        self.assertEqual(success, True)
        self.assertEqual(message, "")

        derivatives, evals, image, success, message = dexi.get_derivatives(
            function, evaluations, arguments)
        self.assertEqual(derivatives, [
            "-1.00", "0.00", "-2.00", "1.00", "-2.00", "1.00", "1.00", "-1.00",
            "-2.00", "-2.00", "-0.50", "-0.00", "-0.50", "-0.00", "2.00",
            "1.50", "-0.00", "-1.50", "-3.00", "-1.00", "2.00", "-2.00",
            "2.00", "3.00", "2.00", "1.00", "-1.00", "-0.11", "-3.00", "0.00",
            "-3.00", "-1.50", "0.00", "0.50", "0.00", "0.00", "4.00", "-1.00",
            "-2.00", "0.00", "-0.50", "-0.50", "0.50", "0.00", "1.00", "1.00",
            "0.00", "1.00", "1.00", "2.00", "0.50", "-1.00", "4.00", "0.00",
            "-3.00", "0.00", "-2.00", "-0.50", "1.00", "1.00", "-0.50",
            "-2.00", "1.00", "1.50", "2.00", "-1.00", "-1.00", "-1.00",
            "-2.00", "-0.50", "1.00", "-1.00", "-0.00", "1.00", "1.00", "1.50",
            "2.00", "2.00", "2.00", "2.00", "-2.00", "-1.50", "-1.00", "0.11"
        ])
        self.assertEqual(evals, [(["1", "2", "3"], "3.00"),
                                 (["0.1", "0.2", "0.3"], "2.93")])
        self.assertEqual(image, "")
        self.assertEqual(success, True)
        self.assertEqual(message, "")
コード例 #24
0
    def test_small(self):
        # a small test case
        function, arguments, evaluations, success, message = dexi.parse_function(
            self._SMALL_TEST)
        self.assertEqual(function, [[(0, ), 0], [(1, ), 1], [(2, ), 2]])
        self.assertEqual(arguments, ["f"])
        self.assertEqual(evaluations, [["1"], ["2"], ["3"]])
        self.assertEqual(success, True)
        self.assertEqual(message, "")

        derivatives, evals, image, success, message = dexi.get_derivatives(
            function, evaluations, arguments, False)
        self.assertEqual(derivatives, ["1.00"] * 4)
        self.assertEqual(evals, [(["1"], "1.00"), (["2"], "2.00"),
                                 (["3"], "3.00")])

        self.assertNotEqual(image, "")
        self.assertEqual(image[-4:], ".png")
        for c in image[:-4]:
            self.assertTrue(c in self._POSSIBLE_CHARS)

        self.assertEqual(success, True)
        self.assertEqual(message, "")
コード例 #25
0
def derivatives():
    # the path which serves post requests for creating derivatives
    raw_function = ""
    if "names" in request.form:
        # if "names" is in the request, it means it was called from the already
        # created derivatives - the user changed the inputs and retried the
        # derivative creation
        values = {}
        # we read the fields in the request and parse out those that start with a v
        for field in request.form:
            if field[0] == "v":
                # the values and the entries are put into a dictionary
                # before that the keys have "v"s removed and cast to int
                tmp_f = int(field.replace("v", ""))
                values[tmp_f] = request.form[field]

        value_list = []
        # we sort the keys of the dictionary, to get the initial order
        # and put the values into a value list
        for field in sorted(values):
            value_list.append(values[field])

        # function is described in three lines
        function_description = []
        # output values joined, delimited with a comma
        function_description.append(",".join(value_list))
        # names are delimited by a comma (already in the request)
        function_description.append(request.form["names"])
        # multiplicities are delimited by a comma (already in the request)
        function_description.append(request.form["multiplicity"])

        # the raw function is represented as a new-line delimitied string
        raw_function = str("\n".join(function_description))

    elif "function" in request.form:
        # if function is present in request, we read it directly
        # parsing is done afterwards
        raw_function = request.form["function"]
    else:
        # if neither of them are present, we return error status
        entries = {
            "raw_function": raw_function,
            "ok": False,
            "message": "Unsupported operation!",
            "title": content._TITLE,
            "subtitle": content._SUBTITLE,
            "about": content._ABOUT,
            "license": content._GPL,
        }
        # render the derivatives page
        return render_template("derivatives.html", entries=entries)

    # we are interested in the time that took for generating the derivatives,
    # evaluations and images
    t = time()

    # we parse the function, getting the actual function, arguments, needed evaluations
    # success status and possible message
    function, arguments, evaluations, success, message = parse_function(
        raw_function)

    # variables are prepared for filling in - these are defaults if anything goes wrong
    raw_function = raw_function.replace("\r", "")
    raw_function = raw_function.split("\n")
    derivatives = None
    success1 = False
    message1 = ""
    multiplicities = ""
    names = ""
    image = ""
    if success:
        # if the previous step succeeded we can construct the function, get derivatives,
        # evaluations and the possible image
        names = ",".join(arguments)
        multiplicities = raw_function[2]
        derivatives, evaluations, image, success1, message1 = get_derivatives(
            function, evaluations, arguments)
        print "Query needed {0}s to execute!".format(_format_number(time() -
                                                                    t))

    # form a dictionary to return to the user
    entries = {
        "raw_function": raw_function,
        "ok": success and success1,
        "message": " ".join([message, message1]),
        "function": function,
        "arguments": arguments,
        "derivatives": derivatives,
        "title": content._TITLE,
        "subtitle": content._SUBTITLE,
        "about": content._ABOUT,
        "license": content._GPL,
        "names": names,
        "multiplicities": multiplicities,
        "evaluations": evaluations,
        "image": image
    }

    # the derivatives page is rendered
    return render_template("derivatives.html", entries=entries)
コード例 #26
0
ファイル: server.py プロジェクト: nejctrdin/DFD
def derivatives():
    # the path which serves post requests for creating derivatives
    raw_function = ""
    if "names" in request.form:
        # if "names" is in the request, it means it was called from the already
        # created derivatives - the user changed the inputs and retried the
        # derivative creation
        values = {}
        # we read the fields in the request and parse out those that start with a v
        for field in request.form:
            if field[0] == "v":
                # the values and the entries are put into a dictionary
                # before that the keys have "v"s removed and cast to int
                tmp_f = int(field.replace("v",""))
                values[tmp_f] = request.form[field]

        value_list = []
        # we sort the keys of the dictionary, to get the initial order
        # and put the values into a value list
        for field in sorted(values):
            value_list.append(values[field])

        # function is described in three lines
        function_description = []
        # output values joined, delimited with a comma
        function_description.append(",".join(value_list))
        # names are delimited by a comma (already in the request)
        function_description.append(request.form["names"])
        # multiplicities are delimited by a comma (already in the request)
        function_description.append(request.form["multiplicity"])

        # the raw function is represented as a new-line delimitied string
        raw_function = str("\n".join(function_description))

    elif "function" in request.form:
        # if function is present in request, we read it directly
        # parsing is done afterwards
        raw_function = request.form["function"]
    else:
        # if neither of them are present, we return error status
        entries = {"raw_function": raw_function,
                   "ok": False,
                   "message": "Unsupported operation!",
                   "title": content._TITLE,
                   "subtitle": content._SUBTITLE,
                   "about": content._ABOUT,
                   "license": content._GPL,
        }
        # render the derivatives page
        return render_template("derivatives.html", entries=entries)

    # we are interested in the time that took for generating the derivatives,
    # evaluations and images
    t = time()

    # we parse the function, getting the actual function, arguments, needed evaluations
    # success status and possible message
    function, arguments, evaluations, success, message = parse_function(raw_function)

    # variables are prepared for filling in - these are defaults if anything goes wrong
    raw_function = raw_function.replace("\r", "")
    raw_function = raw_function.split("\n")
    derivatives = None
    success1 = False
    message1 = ""
    multiplicities = ""
    names = ""
    image = ""
    if success:
        # if the previous step succeeded we can construct the function, get derivatives,
        # evaluations and the possible image
        names = ",".join(arguments)
        multiplicities = raw_function[2]
        derivatives, evaluations, image, success1, message1 = get_derivatives(function, evaluations, arguments)
        print "Query needed {0}s to execute!".format(_format_number(time() - t))

    # form a dictionary to return to the user
    entries = {"raw_function": raw_function,
               "ok": success and success1,
               "message": " ".join([message, message1]),
               "function": function,
               "arguments": arguments,
               "derivatives": derivatives,
               "title": content._TITLE,
               "subtitle": content._SUBTITLE,
               "about": content._ABOUT,
               "license": content._GPL,
               "names": names,
               "multiplicities": multiplicities,
               "evaluations": evaluations,
               "image": image
    }

    # the derivatives page is rendered
    return render_template("derivatives.html", entries=entries)