Remove the "." from the numbering also change the color for the numbers to dark blue.…
- User
Remove the "." from the numbering also change the color for the numbers to dark blue. Python 3.12 code: import math import re from typing import Any, Union, Callable
from PySide6.QtWidgets import ( QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLineEdit, QTextEdit, QScrollBar, QPushButton, QLabel, QFrame, QGridLayout ) from PySide6.QtGui import QFont from PySide6.QtCore import Qt, QSize
def convert_angle(func: Callable[[float], float], x: float, use_degrees: bool) -> float: """Apply trig function with optional degree conversion.""" return func(math.radians(x) if use_degrees else x)
def kilograms_to_pounds(kg: float) -> float: """Convert kilograms to pounds.""" return kg / 0.45359237
def pounds_to_kilograms(lb: float) -> float: """Convert pounds to kilograms.""" return lb * 0.45359237
def celsius_to_kelvin(c: float) -> float: """Convert Celsius to Kelvin.""" return c + 273.15
def kelvin_to_celsius(k: float) -> float: """Convert Kelvin to Celsius.""" return k - 273.15
def celsius_to_fahrenheit(c: float) -> float: """Convert Celsius to Fahrenheit.""" return c * 9 / 5 + 32
def fahrenheit_to_celsius(f: float) -> float: """Convert Fahrenheit to Celsius.""" return (f - 32) * 5 / 9
def capitalize_sentence(sentence: str) -> str: """Capitalize first letter of each word.""" return ' '.join(word.capitalize() for word in sentence.split())
def remove_duplicates(text: str) -> str: """Remove duplicate words and empty lines.""" words = ' '.join(line.strip() for line in text.splitlines() if line.strip()).split() return ' '.join(dict.fromkeys(words))
def get_safe_globals(use_degrees: bool, variables: dict) -> dict: """Provide safe globals for expression evaluation.""" trig_funcs = {'sin', 'cos', 'tan', 'asin', 'acos', 'atan'} safe_globals = { "sqrt": math.sqrt, "log": math.log10, "ln": math.log, "pi": math.pi, "e": math.e, "lb": kilograms_to_pounds, "kg": pounds_to_kilograms, "C": kelvin_to_celsius, "K": celsius_to_kelvin, "F": celsius_to_fahrenheit, "CF": fahrenheit_to_celsius, "capitalize": capitalize_sentence, "duplicates": remove_duplicates, } for func in trig_funcs: safe_globals[func] = lambda x, f=func: convert_angle(getattr(math, f), x, use_degrees) safe_globals.update(variables) return safe_globals
def preprocess_text_functions(expression: str) -> str: """Preprocess text functions with argument quotes.""" def add_quotes(match): func, args = match.groups() return f'{func}("{args}")' if func in ['capitalize', 'duplicates'] else match.group(0) return re.sub(r'\b(capitalize|duplicates)\s*(\s*([^)]+)\s*)', add_quotes, expression)
def validate_inverse_trig(expression: str, safe_globals: dict) -> Union[str, None]: """Check inverse trig function domain validity.""" for func, arg in re.findall(r"(\b\w+\b)\s*(\s*([^)]+)\s*)", expression): if func in ["asin", "acos"]: try: value = eval(arg, {"builtins": None}, safe_globals) if abs(value) > 1: return f"Error: {func}(x) is undefined for |x| > 1" except Exception as e: return f"Error: {e}" return None
- Assistant
To remove the dots from the numbering and change the color for the numbers to dark blue, you need to modify the part of the code responsible for displaying the results in the history. This involves updating the way
history_entryis constructed. Here’s how you can implement these changes:- Remove the dot
.after theself.history_counterin function when constructing .
- Remove the dot
- User
no only the number should be blue not the parentheses something like this [[{self.history_counter}] R]
- Assistant
To modify the code so that only the numbers are displayed in dark blue and the brackets remain unchanged, you should adjust the HTML formatting in the
history_entrystring within thedisplay_resultmethod. Here's how you can achieve that:pythondef display_result(self, expression: str, result: Union[float, str]) -> None: """Show result in history.""" mode = "D" if self.use_degrees else "R" if re.match(r'^\s*[a-zA-Z_]\w*\s*=\s*.+$', expression): history_entry = f"[<span style='color: darkblue;'>{self.history_counter}</span> {mode}] {result}" else: history_entry = f"[<span style='color: darkblue;'>{self.history_counter}</span> {mode}] {expression} = {result}" self.history_entries.append(history_entry) self.refresh_history() self.history_text.verticalScrollBar().setValue( self.history_text.verticalScrollBar().maximum() ) self.history_counter += 1 - User
modify the code so that D for degrees uses D as color and R for Radians uses I
- Assistant
To incorporate the color changes for the
D(Degrees) andR(Radians) indicators, you need to adjust the HTML formatting in thedisplay_resultmethod for themodevariable. Here's how you can make these changes:python