diff --git a/llm/continuous_learning_system.py b/llm/continuous_learning_system.py index 06e82b8..d8721a5 100644 --- a/llm/continuous_learning_system.py +++ b/llm/continuous_learning_system.py @@ -65,6 +65,17 @@ class ContinuousLearningLLM: """ def __init__(self, model_name: str = "continuous_learner"): + """ + Initialize a ContinuousLearningLLM instance for managing continuous, quantum-accelerated LLM training. + + This constructor sets up the model name, connectors for classical and quantum resources, training state variables, model and data storage directories, and performance tracking structures. + + Args: + model_name (str, optional): The name of the model instance. Defaults to "continuous_learner". + + Example: + learner = ContinuousLearningLLM(model_name="my_incremental_llm") + """ self.model_name = model_name self.llm_connector = LLMConnector() self.quantum_connector = DWaveQuantumConnector() @@ -93,13 +104,13 @@ def __init__(self, model_name: str = "continuous_learner"): async def initialize(self, config: Dict[str, Any] = None) -> bool: """ - Initializes the continuous learning system by connecting to LLM and quantum resources, loading or creating the initial model, and starting the background training loop. + Initializes the continuous learning system by connecting to LLM and quantum computing resources, loading or creating the initial model, and starting the background training loop. Args: - config (Optional[Dict[str, Any]]): Configuration dictionary containing connection parameters for LLM and quantum resources. If not provided, defaults are used. + config (Optional[Dict[str, Any]]): Optional configuration dictionary with connection parameters for LLM and quantum resources. If omitted, default settings are used. Returns: - bool: True if initialization succeeds, False otherwise. + bool: True if initialization is successful; False otherwise. Example: success = await continuous_learner.initialize({ diff --git a/run_comprehensive_tests.py b/run_comprehensive_tests.py index d3ffd20..c3cb538 100644 --- a/run_comprehensive_tests.py +++ b/run_comprehensive_tests.py @@ -9,7 +9,25 @@ import time def run_tests(): - """Run comprehensive test suite""" + """ + Runs the comprehensive test suite for utility helper functions, including both standard and performance/stress tests. + + This function executes two categories of tests using `pytest` via subprocess calls: + 1. Standard tests (excluding those marked as slow). + 2. Performance and stress tests (marked as slow). + + Test results are printed to the console, including detailed output if any tests fail. The function returns an exit code indicating overall success or failure. + + Returns: + int: Returns 0 if all tests pass, or 1 if any test fails. + + Example: + status = run_tests() + if status == 0: + print("All tests succeeded.") + else: + print("Some tests failed.") + """ print("Running comprehensive tests for utils/helpers.py...") print("=" * 60) diff --git a/scripts/auto-improve.sh b/scripts/auto-improve.sh index 28068ad..c1e261c 100644 --- a/scripts/auto-improve.sh +++ b/scripts/auto-improve.sh @@ -6,7 +6,7 @@ WAIT_DURATION=${CODERABBIT_WAIT_DURATION:-30} MAX_WAIT_TIME=${CODERABBIT_MAX_WAIT:-300} POLL_INTERVAL=${CODERABBIT_POLL_INTERVAL:-10} -# check_for_new_commits determines if the remote branch has new commits compared to the local HEAD. +# check_for_new_commits checks if the remote branch has commits not present in the local HEAD and returns success if new commits are detected. check_for_new_commits() { local initial_commit=$(git rev-parse HEAD) git fetch origin >/dev/null 2>&1 @@ -19,7 +19,7 @@ check_for_new_commits() { fi } -# wait_for_coderabbit polls the remote repository to detect new commits from CodeRabbit, waiting up to a maximum duration before proceeding. +# wait_for_coderabbit waits for new commits from CodeRabbit by polling the remote repository until a commit is detected or a maximum wait time is reached. wait_for_coderabbit() { local start_time=$(date +%s) local initial_commit=$(git rev-parse HEAD) diff --git a/utils/helpers.py b/utils/helpers.py index d04b188..27d093d 100644 --- a/utils/helpers.py +++ b/utils/helpers.py @@ -11,13 +11,13 @@ def safe_json_parse(json_string: str) -> Optional[Dict[str, Any]]: """ - Parses a JSON string into a dictionary, returning None if parsing fails. + Safely parses a JSON string into a dictionary, returning None if parsing fails. Args: - json_string (str): The JSON string to parse. + json_string (str): JSON-formatted string to parse. Returns: - Optional[Dict[str, Any]]: The parsed dictionary if successful, or None if the input is invalid or cannot be parsed. + Optional[Dict[str, Any]]: Parsed dictionary if successful; None if the input is invalid or cannot be parsed. Example: >>> safe_json_parse('{"key": "value"}') @@ -33,11 +33,11 @@ def safe_json_parse(json_string: str) -> Optional[Dict[str, Any]]: def safe_json_dumps(data: Any, indent: int = 2) -> str: """ - Serialize data to a JSON-formatted string, returning an empty string if serialization fails. + Serializes data into a JSON-formatted string with indentation, returning an empty string if serialization fails. - Args: - data (Any): The data to serialize to JSON. - indent (int, optional): Number of spaces for indentation in the output. Defaults to 2. + Parameters: + data (Any): The object to serialize to JSON. + indent (int): Number of spaces to use for indentation in the output. Defaults to 2. Returns: str: The JSON-formatted string, or an empty string if serialization fails. @@ -54,13 +54,15 @@ def safe_json_dumps(data: Any, indent: int = 2) -> str: def generate_hash(data: Union[str, bytes]) -> str: """ - Generates a SHA256 hash for the given input data. + Generate a SHA256 hash of the input string or bytes. + + If the input is a string, it is encoded as UTF-8 before hashing. Args: - data (str or bytes): The input to hash. If a string is provided, it is encoded as UTF-8 before hashing. + data (str or bytes): Input data to hash. Returns: - str: The hexadecimal representation of the SHA256 hash. + str: Hexadecimal SHA256 hash of the input. Example: >>> generate_hash("hello") @@ -73,17 +75,17 @@ def generate_hash(data: Union[str, bytes]) -> str: def retry_with_backoff(func, max_retries: int = 3, base_delay: float = 1.0): """ - Executes a function with retries using exponential backoff. + Execute a no-argument function with retries using exponential backoff. - Retries the provided function up to `max_retries` times, doubling the delay after each failure starting from `base_delay` seconds. If all attempts fail, the last exception is raised. + Retries the given function up to `max_retries` times, doubling the delay after each failure starting from `base_delay` seconds. If all attempts fail, the last exception is raised. Parameters: - func (Callable[[], Any]): The function to execute. Should take no arguments. - max_retries (int, optional): Maximum number of attempts. Defaults to 3. - base_delay (float, optional): Initial delay in seconds before retrying. Defaults to 1.0. + func: A callable that takes no arguments and returns any value. + max_retries: The maximum number of attempts to execute the function. Defaults to 3. + base_delay: The initial delay in seconds before retrying after a failure. Defaults to 1.0. Returns: - Any: The result returned by `func` if successful. + The result returned by `func` if it succeeds within the allowed retries. Raises: Exception: The last exception raised by `func` if all retries fail. @@ -111,12 +113,12 @@ def flatten_dict(data: Dict[str, Any], prefix: str = "") -> Dict[str, Any]: """ Recursively flattens a nested dictionary into a single-level dictionary with dot-separated keys. - Parameters: + Args: data (Dict[str, Any]): The dictionary to flatten. - prefix (str, optional): A prefix to prepend to each key. Defaults to "". + prefix (str, optional): Prefix to prepend to each key in the flattened dictionary. Defaults to "". Returns: - Dict[str, Any]: A flattened dictionary where nested keys are joined by dots. + Dict[str, Any]: A new dictionary with all nested keys flattened and joined by dots. Example: >>> flatten_dict({'a': {'b': 1, 'c': 2}, 'd': 3}) @@ -134,10 +136,10 @@ def flatten_dict(data: Dict[str, Any], prefix: str = "") -> Dict[str, Any]: def ensure_directory_exists(path: Union[str, Path]) -> Path: """ - Ensures that the specified directory exists, creating it and any necessary parent directories if they do not already exist. + Ensure that a directory exists at the specified path, creating it and any missing parent directories if necessary. - Args: - path (str or Path): The path to the directory to ensure exists. + Parameters: + path (str or Path): The directory path to check or create. Returns: Path: A Path object representing the ensured directory. @@ -153,13 +155,15 @@ def ensure_directory_exists(path: Union[str, Path]) -> Path: def sanitize_filename(filename: str) -> str: """ - Cleans a filename by replacing invalid characters and trimming unwanted characters. + Sanitize a filename by replacing invalid characters and trimming unwanted leading or trailing characters. - Parameters: - filename (str): The original filename to sanitize. + Replaces characters not allowed in filenames (`<>:"/\\|?*`) with underscores, removes leading and trailing spaces and dots, and ensures the result is not empty or only underscores. Returns "unnamed" if the sanitized filename is empty or invalid. + + Args: + filename (str): The filename to sanitize. Returns: - str: A sanitized filename with invalid characters replaced by underscores, leading/trailing spaces and dots removed, and guaranteed to be non-empty. Returns "unnamed" if the sanitized result is empty or only underscores. + str: The sanitized filename, or "unnamed" if the result is empty or only underscores. Example: >>> sanitize_filename(' my:name?.txt ') @@ -178,14 +182,14 @@ def sanitize_filename(filename: str) -> str: def merge_dicts(dict1: Dict[str, Any], dict2: Dict[str, Any]) -> Dict[str, Any]: """ - Recursively merges two dictionaries, combining nested dictionaries and giving precedence to values from the second dictionary. + Recursively merge two dictionaries, combining nested dictionaries and prioritizing values from the second dictionary. - Args: + Parameters: dict1 (Dict[str, Any]): The base dictionary to merge into. dict2 (Dict[str, Any]): The dictionary whose values take precedence. Nested dictionaries are merged recursively. Returns: - Dict[str, Any]: A new dictionary containing the merged keys and values. + Dict[str, Any]: A new dictionary containing the merged result. Example: >>> merge_dicts({'a': 1, 'b': {'x': 2}}, {'b': {'y': 3}, 'c': 4}) @@ -202,14 +206,14 @@ def merge_dicts(dict1: Dict[str, Any], dict2: Dict[str, Any]) -> Dict[str, Any]: def chunk_list(data: List[Any], chunk_size: int) -> List[List[Any]]: """ - Splits a list into sublists (chunks) of a specified size. + Split a list into sublists (chunks) of a specified maximum size. - Args: - data (List[Any]): The list to be divided into chunks. - chunk_size (int): The maximum size of each chunk. Must be a positive integer. + Parameters: + data (List[Any]): The list to split into chunks. + chunk_size (int): The maximum number of elements per chunk. Must be greater than zero. Returns: - List[List[Any]]: A list of sublists, where each sublist contains up to `chunk_size` elements from the original list. The last chunk may contain fewer elements if the total is not divisible by `chunk_size`. + List[List[Any]]: A list of sublists, each containing up to `chunk_size` elements. The final chunk may have fewer elements if the list size is not a multiple of `chunk_size`. Example: >>> chunk_list([1, 2, 3, 4, 5], 2) @@ -220,15 +224,15 @@ def chunk_list(data: List[Any], chunk_size: int) -> List[List[Any]]: def format_duration(seconds: float) -> str: """ - Converts a duration in seconds to a human-readable string in seconds, minutes, or hours. + Convert a duration in seconds to a human-readable string in seconds, minutes, or hours. - Args: - seconds (float): The duration in seconds. + Parameters: + seconds (float): Duration in seconds. Returns: - str: The formatted duration as a string, using seconds with two decimals if under 60, minutes with one decimal if under 3600, or hours with one decimal otherwise. + str: Formatted duration string. Uses seconds with two decimals if under 60, minutes with one decimal if under 3600, or hours with one decimal otherwise. - Example: + Examples: >>> format_duration(45) '45.00s' >>> format_duration(125)