I have a dataclass designed to store the values required to run my application.…
- User
I have a dataclass designed to store the values required to run my application. Additionally, I've created an AppManager class that includes all the variables from the dataclass, along with some helper functions to manage the application. Currently, I'm using an instance of AppManager instead of the dataclass itself. What is the best method to transition from using the AppManager instance to using an instance of the dataclass?
Please provide guidance on how to effectively implement this change, including any necessary code examples or best practices.
@dataclass class AppState: repo_url: str = "" repo_path: str = "" repo_cloned: bool = False language: str = None file_path: str = None file_name: str = None packages: Dict[str, Any] = field(default_factory=dict) complete_methods: Dict[str, Any] = field(default_factory=dict) method_name: str = None method_definition: str = None objective: str = None sample_data: Any = None test_cases: Dict[str, Any] = field(default_factory=dict) test_cases_full: Dict[str, Any] = field(default_factory=dict) pytest_codes: Dict[str, Any] = field(default_factory=dict) pytest_results: Dict[str, Any] = field(default_factory=dict) langgraph_states: List[Any] = field(default_factory=list) selected_scenarios: List[Any] = field(default_factory=list) questions: List[Any] = field(default_factory=list) answers: List[Any] = field(default_factory=list) messages_history: List[Any] = field(default_factory=list) components: List[Any] = field(default_factory=list) class AppManager: """Class for handling app functionalities.""" VARIABLE_ORDER = [field.name for field in AppState.__dataclass_fields__.values()] def __init__(self): self.defaults = AppState() self.reset_all() def reset_all(self): for var in self.VARIABLE_ORDER: setattr(self, var, getattr(self.defaults, var)) def update_and_reset(self, **kwargs: Dict[str, Any]) -> None: reset_index = len(self.VARIABLE_ORDER) for key, value in kwargs.items(): if key in self.VARIABLE_ORDER: setattr(self, key, value) reset_index = min(reset_index, self.VARIABLE_ORDER.index(key)) for i in range(reset_index + 1, len(self.VARIABLE_ORDER)): self.reset_variable(self.VARIABLE_ORDER[i]) def reset_variable(self, var: str) -> None: setattr(self, var, self.get_default_value(var)) def get_default_value(self, var: str) -> Any: """ Return a fresh instance value for the variable. This ensures lists are re-initialized on reset. """ default_value = getattr(self.defaults, var) if isinstance(default_value, list): return [] # Return a new empty list return default_value # Return the default for other types def update(self, **kwargs: Dict[str, Any]) -> None: for key, value in kwargs.items(): if value is not None: setattr(self, key, value) def get_state(self) -> Dict[str, Any]: """Get current state of all variables.""" return { field: getattr(self, field) for field in self.VARIABLE_ORDER } def process_file(self, file_path): """Process the given file to extract modules and methods.""" with open(file_path, "r", encoding="utf-8") as f: file_content = f.read() modules = find_import_dependencies(file_content, self.repo_path, self.language) methods = find_functions_classes(file_content, self.language) return modules, methods def process_langgraph_state(self): """Process the latest state of the LangGraph.""" node, langgraph_state = self.langgraph_states[-1] langgraph_values = langgraph_state.values if len(langgraph_state.next) == 0: self.pytest_codes = langgraph_values["test_code"] self.components = ["scenarios_section", "code_section"] elif langgraph_state.next[0] == "Clarifier": self.questions = [ f"**Types of dependencies**\n{langgraph_values['dependency_descriptions'][self.method_name]}\n **Please verify the io types and suggest the changes if necessary:** " ] self.components = ["verify_io_user_input"] elif langgraph_state.next[0] == "ScenarioGenerator": self.questions = langgraph_values["clarifier_questions"]["questions"] self.answers = [] self.components = ["user_input"] elif langgraph_state.next[0] == "FMCGenerator": self.test_cases_full = langgraph_values["test_scenarios_json"][ "test_scenarios" ] self.test_cases = [case["scenario_name"] for case in self.test_cases_full] self.components = ["scenarios_section"] elif langgraph_state.next[0] == "CodeRunner": self.pytest_codes = langgraph_values["test_code"] self.components = ["scenarios_section", "code_section"] elif langgraph_state.next[0] == "CodeReviewer": self.pytest_results = langgraph_values["pytest_report_str"] self.components = ["scenarios_section", "code_section", "results_section"] def generate_results(self, step): """Generate Response from LangGraph based on the user's input.""" if step == "start": self.langgraph_states.clear() if os.path.isdir(f"{self.repo_path}/test"): shutil.rmtree(f"{self.repo_path}/test") inputs = { "object_definition": self.method_definition, "metadata": { "language": self.language, "repo_path": os.path.abspath(self.repo_path), "file_path": os.path.abspath(self.file_path), "file_name": self.file_name, "object_name": self.method_name, "test_dir": f"{os.path.abspath(self.repo_path)}/test", }, "fca_remaining_iterations": FCA_ITERATION, } langgraph_state = langgraph_assistant(inputs=inputs) else: target_nodes = { "verified_io_types": "DependencyAnalyser", "clarifiction_completed": "Clarifier", "code_generation": "ScenarioGenerator", "run_pytest_code": "TestCodeGenerator", "self_correction": "CodeRunner" } while self.langgraph_states: node, langgraph_state = self.langgraph_states.pop() if node == target_nodes.get(step): self.langgraph_states.append([node, langgraph_state]) break langgraph_values = langgraph_state.values if step == "verified_io_types": langgraph_values["user_provided_info"] = self.answers[0] elif step == "clarifiction_completed": langgraph_values["clarifier_info"] = { q: self.answers[i] for i, q in enumerate(langgraph_values["clarifier_questions"]["questions"]) } elif step == "code_generation": langgraph_values["test_scenarios_str"] = filter_selected_scenarios( scenarios_string=langgraph_values["test_scenarios_str"], scenario_names=self.selected_scenarios, ) elif step == "run_pytest_code": langgraph_values["test_code"] = self.pytest_codes langgraph_state = langgraph_assistant(output=langgraph_values, as_node=node) - Assistant
- User
and how to use this in main code
- Assistant
- User
here also i can see you are creating instance of appManager not Appstate
- Assistant