# Find Unique Functions #### You: write a simple python script using regex that when I run for a given file, it iterates through and looks for def function_name()... and tells me all of the unique functions defined #### ChatGPT: Certainly! Here's a simple Python script that uses regular expressions to iterate through a file and extract unique function names that match the pattern "def function\_name()": ```python import re def find_unique_functions(file_path): function_pattern = re.compile(r'def\s+(\w+)\s*\(') # Regex pattern to match function definitions unique_functions = set() # Set to store unique function names with open(file_path, 'r') as file: for line in file: match = function_pattern.search(line) if match: function_name = match.group(1) unique_functions.add(function_name) return unique_functions # Example usage file_path = 'your_file.py' functions = find_unique_functions(file_path) print("Unique functions found:") for function in functions: print(function) ``` In this script, the `find_unique_functions` function takes a file path as an argument and returns a set of unique function names found in the file. It uses the regular expression pattern `def\s+(\w+)\s*\(` to match function definitions. The pattern captures the function name (composed of one or more word characters) following the "def" keyword and any whitespace characters. You can replace `'your_file.py'` with the path to your file. When you run the script, it will iterate through the file, extract the unique function names, and print them to the console. Please make sure to have the necessary permissions to read the specified file, and ensure that the file contains valid Python code. #### You: rewrite as executable script using pathlib and argparse #### ChatGPT: Certainly! Here's the modified version of the script that uses `pathlib` and `argparse` to make it executable and accept command-line arguments: ```python import re import argparse from pathlib import Path def find_unique_functions(file_path): function_pattern = re.compile(r'def\s+(\w+)\s*\(') # Regex pattern to match function definitions unique_functions = set() # Set to store unique function names with open(file_path, 'r') as file: for line in file: match = function_pattern.search(line) if match: function_name = match.group(1) unique_functions.add(function_name) return unique_functions def main(): parser = argparse.ArgumentParser(description='Find unique functions defined in a file.') parser.add_argument('file', type=str, help='Path to the file') args = parser.parse_args() file_path = Path(args.file) if not file_path.is_file(): print('Error: Invalid file path') return functions = find_unique_functions(file_path) print("Unique functions found:") for function in functions: print(function) if __name__ == '__main__': main() ``` In this version, the script is encapsulated in the `main` function. It uses `argparse` to define and parse the command-line argument `file`, which represents the path to the file. The `Path` object is used from `pathlib` to handle the file path. To execute the script, save it to a file (e.g., `find_functions.py`), make it executable (`chmod +x find_functions.py`), and then run it from the command line, providing the file path as an argument: ``` $ ./find_functions.py your_file.py ``` The script will process the specified file, extract the unique function names, and print them to the console. If an invalid file path is provided, it will display an error message.