import os
import inspect

def paste_code_in_importing_file():
    """Detects the script that imports this package and pastes `code.py` content into it."""

    # Get package directory
    package_dir = os.path.dirname(__file__)
    code_file_path = os.path.join(package_dir, "code.py")

    # Read the content of `code.py`
    with open(code_file_path, "r") as code_file:
        code_content = code_file.read()

    # Detect the importing script (the script that imports `sklearnPra`)
    frame = inspect.stack()[-1]  # Get the last frame (original caller script)
    importing_script = os.path.abspath(frame.filename)  # Get its absolute path

    # Ensure we are not modifying `__init__.py` itself
    if importing_script != os.path.abspath(__file__):
        with open(importing_script, "a") as target_file:
            target_file.write("\n\n# --- Pasted Code from sklearnPra ---\n")
            target_file.write(code_content)
            target_file.write("\n# --- End of Pasted Code ---\n")

# Auto-execute when imported
paste_code_in_importing_file()
