From cd2800180fda848bac342b14101a8a5e5caf6382 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Tue, 9 May 2023 00:10:56 -0700 Subject: [PATCH] Move main() function to main.py and update coder.py to call main() from main.py. # Context: USER: move main() to main.py ASSISTANT: To move the `main()` function to a new file called `main.py`, you'll need to create the new file and move the `main()` function and its related imports to it. Then, you'll need to import and call the `main()` function from `main.py` in `coder.py`. Here are the changes you need to make: Create a new file called `main.py` and add the following content: ```python main.py <<<<<<< ORIGINAL ======= import sys import argparse from pathlib import Path from dotenv import load_dotenv from coder import Coder def main(): load_dotenv() env_prefix = "CODER_" parser = argparse.ArgumentParser(description="Chat with GPT about code") parser.add_argument( "files", metavar="FILE", nargs="+", help="a list of source code files", ) parser.add_argument( "--history-file", metavar="HISTORY_FILE", default=os.environ.get("CODER_HISTORY_FILE", ".coder.history"), help="Specify the history file (default: .coder.history or value from CODER_HISTORY_FILE environment variable)", ) parser.add_argument( "--model", metavar="MODEL", default="gpt-4", help="Specify the model to use for the main chat (default: gpt-4)", ) parser.add_argument( "-3", action="store_const", dest="model", const="gpt-3.5-turbo", help="Use gpt-3.5-turbo model for the main chat", ) parser.add_argument( "-4", action="store_const", dest="model", const="gpt-4", help="Use gpt-4 model for the main chat", ) parser.add_argument( "--no-pretty", action="store_false", dest="pretty", help="Disable prettyd output of GPT responses", default=bool(int(os.environ.get(env_prefix + "PRETTY", 1))), ) parser.add_argument( "--apply", metavar="FILE", help="Apply the changes from the given file instead of running the chat", ) parser.add_argument( "--commit-dirty", action="store_true", help="Commit dirty files without confirmation", default=bool(int(os.environ.get(env_prefix + "COMMIT_DIRTY", 0))), ) args = parser.parse_args() fnames = args.files pretty = args.pretty coder = Coder(args.model, fnames, pretty, args.history_file) coder.commit(ask=not args.commit_dirty, prefix="WIP: ") if args.apply: with open(args.apply, "r") as f: content = f.read() coder.update_files(content, inp="") return coder.run() if __name__ == "__main__": status = main() sys.exit(status) >>>>>>> UPDATED ``` Now, update `coder.py` to remove the `main()` function and its related imports, and call the `main()` function from `main.py`: ```python coder.py <<<<<<< ORIGINAL import sys import re import readline import traceback import argparse from rich.console import Console from rich.prompt import Confirm, Prompt from colorama import Fore, Style from rich.live import Live from rich.text import Text from rich.markdown import Markdown from dotenv import load_dotenv from tqdm import tqdm from pathlib import Path import utils import os import git import openai from dump import dump import prompts openai.api_key = os.getenv("OPENAI_API_KEY") ======= import re import readline import traceback from rich.console import Console from rich.prompt import Confirm, Prompt from colorama import Fore, Style from rich.live import Live from rich.text import Text from rich.markdown import Markdown from tqdm import tqdm from pathlib import Path import utils import os import git import openai from dump import dump import prompts openai.api_key = os.getenv("OPENAI_API_KEY") >>>>>>> UPDATED ``` ```python coder.py <<<<<<< ORIGINAL def main(): load_dotenv() env_prefix = "CODER_" parser = argparse.ArgumentParser(description="Chat with GPT about code") parser.add_argument( "files", metavar="FILE", nargs="+", help="a list of source code files", ) parser.add_argument( "--history-file", metavar="HISTORY_FILE", default=os.environ.get("CODER_HISTORY_FILE", ".coder.history"), help="Specify the history file (default: .coder.history or value from CODER_HISTORY_FILE environment variable)", ) parser.add_argument( "--model", metavar="MODEL", default="gpt-4", help="Specify the model to use for the main chat (default: gpt-4)", ) parser.add_argument( "-3", action="store_const", dest="model", const="gpt-3.5-turbo", help="Use gpt-3.5-turbo model for the main chat", ) parser.add_argument( "-4", action="store_const", dest="model", const="gpt-4", help="Use gpt-4 model for the main chat", ) parser.add_argument( "--no-pretty", action="store_false", dest="pretty", help="Disable prettyd output of GPT responses", default=bool(int(os.environ.get(env_prefix + "PRETTY", 1))), ) parser.add_argument( "--apply", metavar="FILE", help="Apply the changes from the given file instead of running the chat", ) parser.add_argument( "--commit-dirty", action="store_true", help="Commit dirty files without confirmation", default=bool(int(os.environ.get(env_prefix + "COMMIT_DIRTY", 0))), ) args = parser.parse_args() fnames = args.files pretty = args.pretty coder = Coder(args.model, fnames, pretty, args.history_file) coder.commit(ask=not args.commit_dirty, prefix="WIP: ") if args.apply: with open(args.apply, "r") as f: content = f.read() coder.update_files(content, inp="") return coder.run() if __name__ == "__main__": status = main() sys.exit(status) ======= if __name__ == "__main__": from main import main status = main() sys.exit(status) >>>>>>> UPDATED ``` These changes will move the `main()` function to `main.py` and update `coder.py` to call the `main()` function from `main.py`. --- coder.py | 78 ++------------------------------------------------------ 1 file changed, 2 insertions(+), 76 deletions(-) diff --git a/coder.py b/coder.py index c015e7323..d504a2ce7 100755 --- a/coder.py +++ b/coder.py @@ -1,17 +1,14 @@ #!/usr/bin/env python -import sys import re import readline import traceback -import argparse from rich.console import Console from rich.prompt import Confirm, Prompt from colorama import Fore, Style from rich.live import Live from rich.text import Text from rich.markdown import Markdown -from dotenv import load_dotenv from tqdm import tqdm @@ -27,7 +24,6 @@ from dump import dump import prompts openai.api_key = os.getenv("OPENAI_API_KEY") - class Coder: fnames = dict() last_modified = 0 @@ -496,77 +492,7 @@ class Coder: return commit_hash, commit_message -def main(): - load_dotenv() - env_prefix = "CODER_" - parser = argparse.ArgumentParser(description="Chat with GPT about code") - parser.add_argument( - "files", - metavar="FILE", - nargs="+", - help="a list of source code files", - ) - parser.add_argument( - "--history-file", - metavar="HISTORY_FILE", - default=os.environ.get("CODER_HISTORY_FILE", ".coder.history"), - help="Specify the history file (default: .coder.history or value from CODER_HISTORY_FILE environment variable)", - ) - parser.add_argument( - "--model", - metavar="MODEL", - default="gpt-4", - help="Specify the model to use for the main chat (default: gpt-4)", - ) - parser.add_argument( - "-3", - action="store_const", - dest="model", - const="gpt-3.5-turbo", - help="Use gpt-3.5-turbo model for the main chat", - ) - parser.add_argument( - "-4", - action="store_const", - dest="model", - const="gpt-4", - help="Use gpt-4 model for the main chat", - ) - parser.add_argument( - "--no-pretty", - action="store_false", - dest="pretty", - help="Disable prettyd output of GPT responses", - default=bool(int(os.environ.get(env_prefix + "PRETTY", 1))), - ) - parser.add_argument( - "--apply", - metavar="FILE", - help="Apply the changes from the given file instead of running the chat", - ) - parser.add_argument( - "--commit-dirty", - action="store_true", - help="Commit dirty files without confirmation", - default=bool(int(os.environ.get(env_prefix + "COMMIT_DIRTY", 0))), - ) - args = parser.parse_args() - - fnames = args.files - pretty = args.pretty - - coder = Coder(args.model, fnames, pretty, args.history_file) - coder.commit(ask=not args.commit_dirty, prefix="WIP: ") - - if args.apply: - with open(args.apply, "r") as f: - content = f.read() - coder.update_files(content, inp="") - return - - coder.run() - - if __name__ == "__main__": + from main import main status = main() - sys.exit(status) + sys.exit(status) \ No newline at end of file