Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Error Handling and Data Validation in Python Files #201 #213

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file added software/__pycache__/report.cpython-310.pyc
Binary file not shown.
117 changes: 117 additions & 0 deletions software/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button, Slider

# Define allowed functions
allowed_functions = {
'sin': np.sin,
'cos': np.cos,
'tan': np.tan,
'sqrt': np.sqrt,
'log': np.log,
'exp': np.exp,
'abs': np.abs,
'pi': np.pi,
'e': np.e
}

# Global variables
x_range = [-10, 10]
y_range = [-10, 10]
equation_rhs = ""

def calculate_y(x_vals):
global equation_rhs
try:
return eval(equation_rhs, {"__builtins__": None}, {"x": x_vals, **allowed_functions})
except Exception as e:
print(f"Error evaluating the function: {e}")
return np.zeros_like(x_vals)

def update_plot(val=None):
global line, x_range, y_range
x_vals = np.linspace(x_range[0], x_range[1], 400)
y_vals = calculate_y(x_vals)
line.set_xdata(x_vals)
line.set_ydata(y_vals)
ax.set_xlim(x_range)
ax.set_ylim(y_range)
fig.canvas.draw_idle()

def update_x_range(val):
global x_range
x_range = [sxmin.val, sxmax.val]
update_plot()

def update_y_range(val):
global y_range
y_range = [symin.val, symax.val]
update_plot()

def reset(event):
global x_range, y_range
x_range = [-10, 10]
y_range = [-10, 10]
sxmin.reset()
sxmax.reset()
symin.reset()
symax.reset()
update_plot()

# Input equation with validation
while True:
equation = input("Enter an equation in the form y = f(x) (e.g., y = 2*x + 3 or y = sin(x)): ")
try:
lhs, equation_rhs = equation.replace(' ', '').split('=')
if lhs != 'y':
raise ValueError("Invalid equation format. Please use the format 'y = f(x)'.")
# Additional validation to ensure rhs is a valid expression
if not all(char.isalnum() or char in ['+', '-', '*', '/', '(', ')', 'x', ' ', '.', 'sin', 'cos', 'tan', 'sqrt', 'log', 'exp', 'abs', 'pi', 'e'] for char in equation_rhs):
raise ValueError("Invalid characters in the equation. Only numerical values, operators, and allowed functions are permitted.")
break
except ValueError as ve:
print(ve)
except Exception as e:
print(f"An unexpected error occurred: {e}")

plt.ion()
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.1, bottom=0.35)
x_vals = np.linspace(x_range[0], x_range[1], 400)
y_vals = calculate_y(x_vals)
line, = ax.plot(x_vals, y_vals, label=equation)
ax.set_xlim(x_range)
ax.set_ylim(y_range)
ax.legend()
ax.grid(True)
ax.axhline(0, color='black', linewidth=0.5)
ax.axvline(0, color='black', linewidth=0.5)

# Create widgets
axcolor = 'lightgoldenrodyellow'
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
button.on_clicked(reset)

axxmin = plt.axes([0.1, 0.25, 0.65, 0.03], facecolor=axcolor)
sxmin = Slider(axxmin, 'X Min', -100.0, 0.0, valinit=x_range[0])
sxmin.on_changed(update_x_range)

axxmax = plt.axes([0.1, 0.2, 0.65, 0.03], facecolor=axcolor)
sxmax = Slider(axxmax, 'X Max', 0.0, 100.0, valinit=x_range[1])
sxmax.on_changed(update_x_range)

axymin = plt.axes([0.1, 0.15, 0.65, 0.03], facecolor=axcolor)
symin = Slider(axymin, 'Y Min', -100.0, 0.0, valinit=y_range[0])
symin.on_changed(update_y_range)

axymax = plt.axes([0.1, 0.1, 0.65, 0.03], facecolor=axcolor)
symax = Slider(axymax, 'Y Max', 0.0, 100.0, valinit=y_range[1])
symax.on_changed(update_y_range)

# Show plot and keep interactive mode active
plt.show()

while True:
update_plot()
plt.pause(0.1)
Loading