ronantakizawa/codereview-bench huggingface dataset:
Repository: Pythagora-io/gpt-pilot
File: pilot/helpers/Project.py (Python)

Code before:
            try:
                if answer.lower() in cbs:
                    return cbs[answer.lower()](convo)
                elif answer != '':
                    return {'user_input': answer}
            except TokenLimitError as e:
                if is_root_task and answer.lower() not in cbs and answer != '':
                    convo.load_branch(reset_branch_id)
                    return {'user_input': answer}
                else:
                    raise e

    def log(self, text, message_type):
        if self.ipc_client_instance is None or self.ipc_client_instance.client is None:
            print(text)
        else:
            self.ipc_client_instance.send({
                'type': MESSAGE_TYPE[message_type],
                'content': str(text),
            })
            if message_type == MESSAGE_TYPE['user_input_request']:
                return self.ipc_client_instance.listen()

    def finish_loading(self):
        print('', type='loadingFinished')
        self.skip_steps = False


Review comment (suggestion):
Have in mind that before on all places in code we were checking
`self.ipc_client_instance is None or self.ipc_client_instance.client is None`
and now we are checking the opposite. 

Code after:

            try:
                if answer.lower() in cbs:
                    return cbs[answer.lower()](convo)
                elif answer != '':
                    return {'user_input': answer}
            except TokenLimitError as e:
                if is_root_task and answer.lower() not in cbs and answer != '':
                    convo.load_branch(reset_branch_id)
                    return {'user_input': answer}
                else:
                    raise e

    def log(self, text, message_type):
        if self.check_ipc():
            self.ipc_client_instance.send({
                'type': MESSAGE_TYPE[message_type],
                'content': str(text),
            })
            if message_type == MESSAGE_TYPE['user_input_request']:
                return self.ipc_client_instance.listen()
        else:
            print(text)

    def check_ipc(self):
        return self.ipc_client_instance is not None and self.ipc_client_instance.client is not None

    def finish_loading(self):
        print('', type='loadingFinished')
        self.skip_steps = False
Repository: Pythagora-io/gpt-pilot
File: pilot/const/common.py (Python)

Code before:
    'architect': ['architecture'],
    'tech_lead': ['development_planning'],
    'full_stack_developer': ['coding'],
    'dev_ops': ['environment_setup'],
    'code_monkey': ['coding']
}
STEPS = [
    'project_description',
    'user_stories',
    'user_tasks',
    'architecture',
    'environment_setup',
    'development_planning',
    'coding',
    'finished'
]

DEFAULT_IGNORE_PATHS = [
    '.git',
    '.gpt-pilot',
    '.idea',
    '.vscode',
    '.next',
    '.DS_Store',
    '__pycache__',
    'node_modules',
    'package-lock.json',
    'venv',
    'dist',
    'build',
    'target',
    "*.min.js",
    "*.min.css",
    "*.svg",
    "*.csv",
    "*.log",
    "go.sum",
]
IGNORE_PATHS = DEFAULT_IGNORE_PATHS + [
    folder for folder
    in os.environ.get('IGNORE_PATHS', '').split(',')
    if folder
]
IGNORE_SIZE_THRESHOLD = 50000  # 50K+ files are ignored by default
PROMPT_DATA_TO_IGNORE = {'directory_tree', 'name'}


EXAMPLE_PROJECT_DESCRIPTION = (
    "A simple webchat application in node/express using MongoDB. "
    "Use Bootstrap and jQuery on the frontend, for a simple, clean UI. "
    "Use socket.io for real-time communication between backend and frontend.\n\n"

Review comment (suggestion):
This fixes problems when people create a Python virtual environment within a project, irrespective of how they name the directory. There's at least a couple of reported case of this exact reason for TokenLimitError, including in telemetry and discord.

Code after:
    'architect': ['architecture'],
    'tech_lead': ['development_planning'],
    'full_stack_developer': ['coding'],
    'dev_ops': ['environment_setup'],
    'code_monkey': ['coding']
}
STEPS = [
    'project_description',
    'user_stories',
    'user_tasks',
    'architecture',
    'environment_setup',
    'development_planning',
    'coding',
    'finished'
]

DEFAULT_IGNORE_PATHS = [
    '.git',
    '.gpt-pilot',
    '.idea',
    '.vscode',
    '.next',
    '.DS_Store',
    '__pycache__',
    "site-packages",
    'node_modules',
    'package-lock.json',
    'venv',
    'dist',
    'build',
    'target',
    "*.min.js",
    "*.min.css",
    "*.svg",
    "*.csv",
    "*.log",
    "go.sum",
]
IGNORE_PATHS = DEFAULT_IGNORE_PATHS + [
    folder for folder
    in os.environ.get('IGNORE_PATHS', '').split(',')
    if folder
]
IGNORE_SIZE_THRESHOLD = 50000  # 50K+ files are ignored by default
PROMPT_DATA_TO_IGNORE = {'directory_tree', 'name'}


EXAMPLE_PROJECT_DESCRIPTION = (
    "A simple webchat application in node/express using MongoDB. "
    "Use Bootstrap and jQuery on the frontend, for a simple, clean UI. "
Repository: Pythagora-io/gpt-pilot
File: pilot/helpers/files.py (Python)

Code before:
    return return_array


def clear_directory(directory: str, ignore: Optional[list[str]] = None):
    """
    Delete all files and directories (except ignored ones) in the given directory.

    :param dir_path: Full path to the directory to clear
    :param ignore: List of files or folders to ignore (optional)
    """
    matcher = IgnoreMatcher(ignore, root_path=directory)

    # TODO: Convert to use pathlib.Path.walk()
    for dpath, dirs, files in os.walk(directory, topdown=True):
        # In-place update of dirs so that os.walk() doesn't traverse them
        dirs[:] = [
            d for d in dirs
            if not matcher.ignore(os.path.join(dpath, d))
        ]

        for file in files:
            full_path = os.path.join(dpath, file)
            if matcher.ignore(full_path):
                continue

            os.remove(full_path)

        # Delete empty subdirectories not in ignore list
        for d in dirs:
            subdir_path = os.path.join(dpath, d)
            if not os.listdir(subdir_path):
                os.rmdir(subdir_path)


Review comment (bug):
Also catches some weird edge cases. If we can't remove a file here, oh well, not such a big deal, we don't want to crash over it.

Code after:
    return return_array


def clear_directory(directory: str, ignore: Optional[list[str]] = None):
    """
    Delete all files and directories (except ignored ones) in the given directory.

    :param dir_path: Full path to the directory to clear
    :param ignore: List of files or folders to ignore (optional)
    """
    matcher = IgnoreMatcher(ignore, root_path=directory)

    # TODO: Convert to use pathlib.Path.walk()
    for dpath, dirs, files in os.walk(directory, topdown=True):
        # In-place update of dirs so that os.walk() doesn't traverse them
        dirs[:] = [
            d for d in dirs
            if not matcher.ignore(os.path.join(dpath, d))
        ]

        for file in files:
            full_path = os.path.join(dpath, file)
            if matcher.ignore(full_path):
                continue

            try:
                os.remove(full_path)
            except:  # noqa
                # Gracefully handle some weird edge cases instead of crashing
                pass

        # Delete empty subdirectories not in ignore list
        for d in dirs:
            try:
                subdir_path = os.path.join(dpath, d)
                if not os.listdir(subdir_path):
                    os.rmdir(subdir_path)
            except:  # noqa
                # Gracefully handle some weird edge cases instead of crashing
                pass
Repository: Pythagora-io/gpt-pilot
File: core/agents/task_completer.py (Python)

Code before:

log = get_logger(__name__)


class TaskCompleter(BaseAgent):
    agent_type = "pythagora"
    display_name = "Pythagora"

    async def run(self) -> AgentResponse:
        current_task_index1 = self.current_state.tasks.index(self.current_state.current_task) + 1
        self.next_state.action = f"Task #{current_task_index1} complete"
        self.next_state.complete_task()
        await self.state_manager.log_task_completed()
        tasks = self.current_state.tasks
        source = self.current_state.current_epic.get("source", "app")
        await self.ui.send_task_progress(
            tasks.index(self.current_state.current_task) + 1,
            len(tasks),
            self.current_state.current_task["description"],
            source,
            "done",
            self.current_state.get_source_index(source),
            tasks,
        )
        await telemetry.trace_code_event(
            "task-end", {"task-num": current_task_index1, "num-iterations": len(self.current_state.iterations)}
        )

        return AgentResponse.done(self)


Review comment (suggestion):
we should either update how trace_code_event works or add data to each trace. Here are some suggestions of what data might be useful:
- number of epics (is it the initial app or feature?)
- number of all tasks (is it the last task? Could be that we have most iterations on the last task because everyone clicks "continue" until it is the last task)
- some data so we can find and analyze the db

Code after:

log = get_logger(__name__)


class TaskCompleter(BaseAgent):
    agent_type = "pythagora"
    display_name = "Pythagora"

    async def run(self) -> AgentResponse:
        current_task_index1 = self.current_state.tasks.index(self.current_state.current_task) + 1
        self.next_state.action = f"Task #{current_task_index1} complete"
        self.next_state.complete_task()
        await self.state_manager.log_task_completed()
        tasks = self.current_state.tasks
        source = self.current_state.current_epic.get("source", "app")
        await self.ui.send_task_progress(
            tasks.index(self.current_state.current_task) + 1,
            len(tasks),
            self.current_state.current_task["description"],
            source,
            "done",
            self.current_state.get_source_index(source),
            tasks,
        )
        await telemetry.trace_code_event(
            "task-end",
            {
                "task_index": current_task_index1,
                "num_tasks": len(self.current_state.tasks),
                "num_epics": len(self.current_state.epics),
                "num_iterations": len(self.current_state.iterations),
            },
        )

        return AgentResponse.done(self)
Repository: Python-World/python-mini-projects
File: projects/Time to load Website/time_to_load_website.py (Python)

Code before:
from urllib.request import urlopen
import time

#Storing user defined URl
URL = input("Enter the url whose loading time you want to check: ")

#Open the url as entered by the user and checking protocols
if ("https" or "http") in URL:
    open_this_url = urlopen(URL)

else:
    open_this_url = urlopen("https://" + URL)

#Time stamp before the reading of url starts
start_time = time.time()

#Reading the user defined url
read_the_url = open_this_url.read()

#Time stamp after the reading of the url 
end_time = time.time()

#Closing the instance of the urlopen object
open_this_url.close()

print()
print(f"The time taken to load {URL} is {end_time - start_time} seconds.")


Review comment (suggestion):
name 'URL' should be lowercase(Constant should be defined in Upper case)
```suggestion
url = input("Enter the url whose loading time you want to check: ")
```

Code after:
from urllib.request import urlopen
import time


def get_load_time(url):
    """This function takes a user defined url as input
    and returns the time taken to load that url in seconds.

    Args:
        url (string): The user defined url.

    Returns:
        time_to_load (float): The time taken to load the website in seconds.
    """

    if ("https" or "http") in url:  # Checking for presence of protocols
        open_this_url = urlopen(url)  # Open the url as entered by the user
    else:
        open_this_url = urlopen("https://" + url)  # Adding https to the url
    start_time = time.time()  # Time stamp before the reading of url starts
    open_this_url.read()  # Reading the user defined url
    end_time = time.time()  # Time stamp after the reading of the url
    open_this_url.close()  # Closing the instance of the urlopen object
    time_to_load = end_time - start_time

    return time_to_load


if __name__ == '__main__':
    url = input("Enter the url whose loading time you want to check: ")
Repository: Python-World/python-mini-projects
File: projects/Time to load Website/time_to_load_website.py (Python)

Code before:
from urllib.request import urlopen
import time

#Storing user defined URl
URL = input("Enter the url whose loading time you want to check: ")

#Open the url as entered by the user and checking protocols
if ("https" or "http") in URL:
    open_this_url = urlopen(URL)

else:
    open_this_url = urlopen("https://" + URL)

#Time stamp before the reading of url starts
start_time = time.time()

#Reading the user defined url
read_the_url = open_this_url.read()

#Time stamp after the reading of the url 
end_time = time.time()

#Closing the instance of the urlopen object
open_this_url.close()

print()
print(f"The time taken to load {URL} is {end_time - start_time} seconds.")


Review comment (suggestion):
block and inline comment should start with '# '
```suggestion
# Storing user defined URl
```

Code after:
from urllib.request import urlopen
import time


def get_load_time(url):
    """This function takes a user defined url as input
    and returns the time taken to load that url in seconds.

    Args:
        url (string): The user defined url.

    Returns:
        time_to_load (float): The time taken to load the website in seconds.
    """

    if ("https" or "http") in url:  # Checking for presence of protocols
        open_this_url = urlopen(url)  # Open the url as entered by the user
    else:
        open_this_url = urlopen("https://" + url)  # Adding https to the url
    start_time = time.time()  # Time stamp before the reading of url starts
    open_this_url.read()  # Reading the user defined url
    end_time = time.time()  # Time stamp after the reading of the url
    open_this_url.close()  # Closing the instance of the urlopen object
    time_to_load = end_time - start_time

    return time_to_load


if __name__ == '__main__':
Repository: Python-World/python-mini-projects
File: projects/Time to load Website/time_to_load_website.py (Python)

Code before:
from urllib.request import urlopen
import time

#Storing user defined URl
URL = input("Enter the url whose loading time you want to check: ")

#Open the url as entered by the user and checking protocols
if ("https" or "http") in URL:
    open_this_url = urlopen(URL)

else:
    open_this_url = urlopen("https://" + URL)

#Time stamp before the reading of url starts
start_time = time.time()

#Reading the user defined url
read_the_url = open_this_url.read()

#Time stamp after the reading of the url 
end_time = time.time()

#Closing the instance of the urlopen object
open_this_url.close()

print()
print(f"The time taken to load {URL} is {end_time - start_time} seconds.")


Review comment (suggestion):
use inline comments for single statement
```suggestion
start_time = time.time()  # Time stamp before the reading of url starts
```

Code after:
from urllib.request import urlopen
import time


def get_load_time(url):
    """This function takes a user defined url as input
    and returns the time taken to load that url in seconds.

    Args:
        url (string): The user defined url.

    Returns:
        time_to_load (float): The time taken to load the website in seconds.
    """

    if ("https" or "http") in url:  # Checking for presence of protocols
        open_this_url = urlopen(url)  # Open the url as entered by the user
    else:
        open_this_url = urlopen("https://" + url)  # Adding https to the url
    start_time = time.time()  # Time stamp before the reading of url starts
    open_this_url.read()  # Reading the user defined url
    end_time = time.time()  # Time stamp after the reading of the url
    open_this_url.close()  # Closing the instance of the urlopen object
    time_to_load = end_time - start_time

    return time_to_load


if __name__ == '__main__':
    url = input("Enter the url whose loading time you want to check: ")
    print(f"\nThe time taken to load {url} is {get_load_time(url):.2} seconds.")
Repository: Python-World/python-mini-projects
File: projects/Time to load Website/time_to_load_website.py (Python)

Code before:
from urllib.request import urlopen
import time

#Storing user defined URl
URL = input("Enter the url whose loading time you want to check: ")

#Open the url as entered by the user and checking protocols
if ("https" or "http") in URL:
    open_this_url = urlopen(URL)

else:
    open_this_url = urlopen("https://" + URL)

#Time stamp before the reading of url starts
start_time = time.time()

#Reading the user defined url
read_the_url = open_this_url.read()

#Time stamp after the reading of the url 
end_time = time.time()

#Closing the instance of the urlopen object
open_this_url.close()

print()
print(f"The time taken to load {URL} is {end_time - start_time} seconds.")


Review comment (suggestion):
Remove unused variable
```suggestion
open_this_url.read()
```

Code after:
from urllib.request import urlopen
import time


def get_load_time(url):
    """This function takes a user defined url as input
    and returns the time taken to load that url in seconds.

    Args:
        url (string): The user defined url.

    Returns:
        time_to_load (float): The time taken to load the website in seconds.
    """

    if ("https" or "http") in url:  # Checking for presence of protocols
        open_this_url = urlopen(url)  # Open the url as entered by the user
    else:
        open_this_url = urlopen("https://" + url)  # Adding https to the url
    start_time = time.time()  # Time stamp before the reading of url starts
    open_this_url.read()  # Reading the user defined url
    end_time = time.time()  # Time stamp after the reading of the url
    open_this_url.close()  # Closing the instance of the urlopen object
    time_to_load = end_time - start_time

    return time_to_load


if __name__ == '__main__':
    url = input("Enter the url whose loading time you want to check: ")
    print(f"\nThe time taken to load {url} is {get_load_time(url):.2} seconds.")
Repository: Python-World/python-mini-projects
File: projects/Time to load Website/time_to_load_website.py (Python)

Code before:
    and returns the time taken to load that url in seconds.

    Args:
        url (string): The user defined url.

    Returns:
        time_to_load (float): The time taken to load the website in seconds.
    """

    if ("https" or "http") in url:  # Checking for presence of protocols
        open_this_url = urlopen(url)  # Open the url as entered by the user
    else:
        open_this_url = urlopen("https://" + url)  # Adding https to the url

    start_time = time.time()  # Time stamp before the reading of url starts

    open_this_url.read()  # Reading the user defined url

    end_time = time.time()  # Time stamp after the reading of the url

    open_this_url.close()  # Closing the instance of the urlopen object

    time_to_load = end_time - start_time

    return time_to_load


if __name__ == '__main__':

    url = input("Enter the url whose loading time you want to check: ")

    print(
        f"\nThe time taken to load {url} is {get_load_time(url):.2} seconds.")


Review comment (suggestion):
- looks great!
- can avoid extra variable ` time_to_load`
```python
return end_time - start_time
```

Code after:
    and returns the time taken to load that url in seconds.

    Args:
        url (string): The user defined url.

    Returns:
        time_to_load (float): The time taken to load the website in seconds.
    """

    if ("https" or "http") in url:  # Checking for presence of protocols
        open_this_url = urlopen(url)  # Open the url as entered by the user
    else:
        open_this_url = urlopen("https://" + url)  # Adding https to the url
    start_time = time.time()  # Time stamp before the reading of url starts
    open_this_url.read()  # Reading the user defined url
    end_time = time.time()  # Time stamp after the reading of the url
    open_this_url.close()  # Closing the instance of the urlopen object
    time_to_load = end_time - start_time

    return time_to_load


if __name__ == '__main__':
    url = input("Enter the url whose loading time you want to check: ")
    print(f"\nThe time taken to load {url} is {get_load_time(url):.2} seconds.")
Repository: Python-World/python-mini-projects
File: projects/Time to load Website/time_to_load_website.py (Python)

Code before:
    """

    if ("https" or "http") in url:  # Checking for presence of protocols
        open_this_url = urlopen(url)  # Open the url as entered by the user
    else:
        open_this_url = urlopen("https://" + url)  # Adding https to the url

    start_time = time.time()  # Time stamp before the reading of url starts

    open_this_url.read()  # Reading the user defined url

    end_time = time.time()  # Time stamp after the reading of the url

    open_this_url.close()  # Closing the instance of the urlopen object

    time_to_load = end_time - start_time

    return time_to_load


if __name__ == '__main__':

    url = input("Enter the url whose loading time you want to check: ")

    print(
        f"\nThe time taken to load {url} is {get_load_time(url):.2} seconds.")


Review comment (suggestion):
```python
print(f"\nThe time taken to load {url} is {get_load_time(url):.2} seconds.")
```

Code after:
from urllib.request import urlopen
import time


def get_load_time(url):
    """This function takes a user defined url as input
    and returns the time taken to load that url in seconds.

    Args:
        url (string): The user defined url.

    Returns:
        time_to_load (float): The time taken to load the website in seconds.
    """

    if ("https" or "http") in url:  # Checking for presence of protocols
        open_this_url = urlopen(url)  # Open the url as entered by the user
    else:
        open_this_url = urlopen("https://" + url)  # Adding https to the url
    start_time = time.time()  # Time stamp before the reading of url starts
    open_this_url.read()  # Reading the user defined url
    end_time = time.time()  # Time stamp after the reading of the url
    open_this_url.close()  # Closing the instance of the urlopen object
    time_to_load = end_time - start_time

    return time_to_load


if __name__ == '__main__':
    url = input("Enter the url whose loading time you want to check: ")
    print(f"\nThe time taken to load {url} is {get_load_time(url):.2} seconds.")