inputProcessor
Input processing functions for triangle program.
1#!/usr/bin/env python3 2"""Input processing functions for triangle program.""" 3 4import math 5from decimal import Decimal 6 7 8def sideLength(point1, point2): 9 """Side lenght. 10 11 Distance between point1 and point2 with 12 coordinates x (index 0) and y (index 1). 13 14 :return: Lenght of side. 15 """ 16 return round(Decimal(math.dist(point1, point2)), 10) 17# vector = (point2[0] - point1[0], point2[1] - point1[1]) 18# return (vector[0] ** 2 + vector[1] ** 2) ** 0.5 19 20 21def inputPoint(point, coordNames=["x", "y"], debug=False): 22 """Input coordinates. 23 24 Ask user to input coordinates of one point. 25 Function support multi-dimension space based on cordNames provided. 26 User is asked to import point coordinates one by one and checks, 27 if inputed coordinate is valid number. If not, it lets user to input 28 coordinate again up to 3 times. Then, if input is not coorect, program 29 is exited with exit code 1. 30 31 :return: List of coordinates entered to console. 32 """ 33 coords = [] 34 for coordName in coordNames: 35 nok = True 36 retries = 0 37 while nok: 38 try: 39 print(f"Please input point {point}, " 40 f"coordinate {coordName}: ", end="") 41 coords.append(float(input())) 42 nok = False 43 except Exception as e: 44 print(e) if debug else None 45 if retries < 2: 46 print("Wrong input! Try again...") 47 retries += 1 48 else: 49 print("Retry limit exceeded, exiting...") 50 exit(1) 51 return coords 52 53 54def getPoints(args, debug=False): 55 """List of points. 56 57 Coordinates are parsed from provided command line arguments 58 list (args) or interactively inputed to console by user. 59 60 :return: List of 3 points with 2 coordinates each. 61 """ 62 try: 63 points = [ 64 [float(args[1]), float(args[2])], 65 [float(args[3]), float(args[4])], 66 [float(args[5]), float(args[6])], 67 ] 68 except (ValueError, IndexError) as e: 69 print("Failed to process provided arguments or no arguments given.") 70 print(f"Reason: {e}") if debug else None 71 print("Please enter points now.") 72 points = [ 73 inputPoint("A", debug=debug), 74 inputPoint("B", debug=debug), 75 inputPoint("C", debug=debug), 76 ] 77 return points 78 79 80def getSides(args, debug=False): 81 """:return: List of lengths of the 3 sides of a triangle.""" 82 points = getPoints(args, debug) 83 84 # Points 85 print(f"\nDebug: Points are {points[0]} {points[1]}" 86 f"{points[2]}\n") if debug else None 87 88 # Side length 89 sides = [] 90 sides.append(sideLength(points[0], points[1])) 91 sides.append(sideLength(points[1], points[2])) 92 sides.append(sideLength(points[2], points[0])) 93 94 return sides
9def sideLength(point1, point2): 10 """Side lenght. 11 12 Distance between point1 and point2 with 13 coordinates x (index 0) and y (index 1). 14 15 :return: Lenght of side. 16 """ 17 return round(Decimal(math.dist(point1, point2)), 10)
Side lenght.
Distance between point1 and point2 with coordinates x (index 0) and y (index 1).
Returns
Lenght of side.
22def inputPoint(point, coordNames=["x", "y"], debug=False): 23 """Input coordinates. 24 25 Ask user to input coordinates of one point. 26 Function support multi-dimension space based on cordNames provided. 27 User is asked to import point coordinates one by one and checks, 28 if inputed coordinate is valid number. If not, it lets user to input 29 coordinate again up to 3 times. Then, if input is not coorect, program 30 is exited with exit code 1. 31 32 :return: List of coordinates entered to console. 33 """ 34 coords = [] 35 for coordName in coordNames: 36 nok = True 37 retries = 0 38 while nok: 39 try: 40 print(f"Please input point {point}, " 41 f"coordinate {coordName}: ", end="") 42 coords.append(float(input())) 43 nok = False 44 except Exception as e: 45 print(e) if debug else None 46 if retries < 2: 47 print("Wrong input! Try again...") 48 retries += 1 49 else: 50 print("Retry limit exceeded, exiting...") 51 exit(1) 52 return coords
Input coordinates.
Ask user to input coordinates of one point. Function support multi-dimension space based on cordNames provided. User is asked to import point coordinates one by one and checks, if inputed coordinate is valid number. If not, it lets user to input coordinate again up to 3 times. Then, if input is not coorect, program is exited with exit code 1.
Returns
List of coordinates entered to console.
55def getPoints(args, debug=False): 56 """List of points. 57 58 Coordinates are parsed from provided command line arguments 59 list (args) or interactively inputed to console by user. 60 61 :return: List of 3 points with 2 coordinates each. 62 """ 63 try: 64 points = [ 65 [float(args[1]), float(args[2])], 66 [float(args[3]), float(args[4])], 67 [float(args[5]), float(args[6])], 68 ] 69 except (ValueError, IndexError) as e: 70 print("Failed to process provided arguments or no arguments given.") 71 print(f"Reason: {e}") if debug else None 72 print("Please enter points now.") 73 points = [ 74 inputPoint("A", debug=debug), 75 inputPoint("B", debug=debug), 76 inputPoint("C", debug=debug), 77 ] 78 return points
List of points.
Coordinates are parsed from provided command line arguments list (args) or interactively inputed to console by user.
Returns
List of 3 points with 2 coordinates each.
81def getSides(args, debug=False): 82 """:return: List of lengths of the 3 sides of a triangle.""" 83 points = getPoints(args, debug) 84 85 # Points 86 print(f"\nDebug: Points are {points[0]} {points[1]}" 87 f"{points[2]}\n") if debug else None 88 89 # Side length 90 sides = [] 91 sides.append(sideLength(points[0], points[1])) 92 sides.append(sideLength(points[1], points[2])) 93 sides.append(sideLength(points[2], points[0])) 94 95 return sides
Returns
List of lengths of the 3 sides of a triangle.