コード例 #1
0
def distance():
    args = request.args.to_dict()

    # x1
    if ("x1" not in args):
        return "Expected parameter 'x1'.", status.HTTP_400_BAD_REQUEST

    x1 = args["x1"]

    if not (x1.replace(".", "", 1)).isnumeric():
        return "Parameter 'x1' must be a float.", status.HTTP_400_BAD_REQUEST

    # y1
    if ("y1" not in args):
        return "Expected parameter 'y1'.", status.HTTP_400_BAD_REQUEST

    y1 = args["y1"]

    if not (y1.replace(".", "", 1)).isnumeric():
        return "Parameter 'y1' must be a float.", status.HTTP_400_BAD_REQUEST

    # x2
    if ("x2" not in args):
        return "Expected parameter 'x2'.", status.HTTP_400_BAD_REQUEST

    x2 = args["x2"]

    if not (x2.replace(".", "", 1)).isnumeric():
        return "Parameter 'x2' must be a float.", status.HTTP_400_BAD_REQUEST

    # y2
    if ("y2" not in args):
        return "Expected parameter 'y2'.", status.HTTP_400_BAD_REQUEST

    y2 = args["y2"]

    if not (y2.replace(".", "", 1)).isnumeric():
        return "Parameter 'y2' must be a float.", status.HTTP_400_BAD_REQUEST

    x1 = float(x1)
    y1 = float(y1)
    x2 = float(x2)
    y2 = float(y2)

    distance = distance_calc.calculateDistance(x1, y1, x2, y2)
    return (jsonify({'distance': distance}))
コード例 #2
0
def distance():
    """Return the shortest distance between two points"""

    distance_sentinel = 0
    semantics_dictionary = {0: "x1", 1: "y1", 2: "x2", 3: "y2"}
    coordinate_array = []
    good_input = True

    while (distance_sentinel <= 3):
        good_input = True
        print("Coordinate values (x1, y1) (x2, y2)")
        distance_user_input = input("Please input value for data point " +
                                    semantics_dictionary[distance_sentinel] +
                                    ": ")

        # Error-handling for invalid inputs, increment only when input can be a float
        try:
            distance_user_input = float(distance_user_input)
        except:
            good_input = False

        if good_input == False:
            print("Invalid value, please try again")
        else:
            distance_user_input = float(distance_user_input)
            distance_sentinel += 1
            coordinate_array.append(distance_user_input)

    distance = distance_calc.calculateDistance(coordinate_array[0],
                                               coordinate_array[1],
                                               coordinate_array[2],
                                               coordinate_array[3])

    #Tell the user the results of the calculation
    if distance == False and distance != 0:
        print("One or more of the coordinates provided is invalid.")
    else:
        print("The distance between the two coordinates is: " +
              "{0:.3f}".format(distance) + " units.")
    print()
コード例 #3
0
 def test_input_base(self):
     self.assertEqual(distance_calc.calculateDistance(0, 0, 0, 0), 0)
コード例 #4
0
 def test_input_tuple(self):
     # Tuple test
     self.assertFalse(distance_calc.calculateDistance((8, 7), 0, 0, 0))
コード例 #5
0
 def test_input_boole(self):
     # Boole test
     self.assertFalse(distance_calc.calculateDistance(True, 0, 0, 0))
コード例 #6
0
 def test_input_dictionary(self):
     # Dictionary test
     self.assertFalse(distance_calc.calculateDistance(0, 0, {6, 5}, 0))
コード例 #7
0
 def test_input_complex(self):
     # Complex test
     self.assertFalse(distance_calc.calculateDistance(0, 0, 0, 3 + 1j))
コード例 #8
0
 def test_input_string(self):
     # String test
     self.assertFalse(distance_calc.calculateDistance("1", 0, 0, 0))
コード例 #9
0
 def test_input_array(self):
     # Array test
     self.assertFalse(distance_calc.calculateDistance(0, [2], 0, 0))
コード例 #10
0
 def test_input_accuracy_variables(self):
     a = .8
     b = .6
     c = .3
     d = 8
     self.assertEqual(distance_calc.calculateDistance(a, b, c, d), 7.417)
コード例 #11
0
 def test_input_accuracy_negatives(self):
     self.assertEqual(distance_calc.calculateDistance(.8, -.6, .3, 8),
                      8.615)
コード例 #12
0
 def test_input_accuracy_floats(self):
     self.assertEqual(distance_calc.calculateDistance(.8, .6, .3, 8), 7.417)
コード例 #13
0
 def test_input_accuracy_difference(self):
     self.assertEqual(distance_calc.calculateDistance(0, 0, 0, 1), 1)
コード例 #14
0
 def test_input_accuracy_equivalent(self):
     self.assertEqual(distance_calc.calculateDistance(1, 1, 1, 1), 0)