5个ChatGPT功能帮助你提升日常编码效率

Isis ·
更新时间:2024-09-20
· 391 次阅读

目录

1.生成编码框架

2.研究和比较

3.理解代码

4.向代码添加注释

5.使用某种样式重写代码

ChatGPT 的出现,彻底改变了开发代码的方式。但是目前为止,大多数软件开发人员和数据专业人员仍然没有习惯使用 ChatGPT 来改进和简化自己的工作。

这就是为什么我在这里概述了5个不同的功能,以提高日常工作的速度和质量。

下面让我们一起探索如何在日常工作中使用它们

1.生成编码框架

ChatGPT 是我们从头开始构建新项目的秘密武器。只需几个提示,它就可以生成我们所需的代码框架,以及所选择的技术、框架和版本。它不仅每次为我们节省了至少一个小时左右的工作时间,而且有助于保持文档(或团队的文档)的有效组织和更新,这就像团队中多了一双手,一双勤劳的手!

例如,我们想使用一些外部API,可以请 ChatGPT 为我们起草一个初始代码结构。

问:Generate boiler plate code that integrates an extern API using Python.

或者我们甚至可以要求这个框架使用 Selenium 抓取 LinkedIn

ChatGPT 的输出如下:

from selenium import webdriver from selenium.webdriver.common.keys import Keys import time # Initialize the web driver driver = webdriver.Firefox() # Navigate to LinkedIn driver.get("https://www.linkedin.com/") # Find the email and password input fields email = driver.find_element_by_id("username") password = driver.find_element_by_id("password") # Enter your credentials email.send_keys("YOUR_EMAIL") password.send_keys("YOUR_PASSWORD") # Click the login button driver.find_element_by_xpath("//button[@type='submit']").click() # Wait for the page to load time.sleep(5) # Navigate to the profile of the desired user driver.get("https://www.linkedin.com/in/USER_PROFILE_NAME") # Extract the information you want from the page name = driver.find_element_by_xpath("//span[@class='inline t-24 t-black t-normal break-words']").text location = driver.find_element_by_xpath("//span[@class='t-16 t-black t-normal inline-block']").text print(name) print(location) # close the browser driver.quit()

很简单对吧?尝试用我们能想象的任何项目去挑战 ChatGPT 吧。

2.研究和比较

决定如何实施某件事可能是很困难的,特别是当有多种情况可供选择时。我的方法是为每种选择创建一个基本的概念模型,然后比较它们。

但是,在 ChatGPT 的帮助下,这个过程变得简单多了。

我们现在可以直接向它征求专家级别的意见,以确定哪种选项或库最适合我们的代码开发。这节省了我们在决策过程中的时间和精力,并确保使用了最佳的工具。

让我们想象一下,我想使用地理空间数据,但我不确定是否应该使用 Geostandard 或 Plotly。我们可以要求 ChatGPT 进行比较,它立即回答了两个库之间的主要区别。

如果现在我们想抓取网站,就可以问什么是最好的库。ChatGPT 会用 Python 中最流行的 web 抓取库来回答。

我们甚至可以询问想要抓取的网站的最佳方式是什么——尽管 ChatGPT 很可能会警告你这将违反该网站的内容政策——所以要小心。

问:What’s the best option to scrape a social network?

3.理解代码

在日常工作当中,我们都在努力理解一个不是由我们创建的代码库。浏览一个复杂且组织不良的代码可能是一项令人崩溃的任务。

但是,通过 ChatGPT,理解新的代码库变得容易多了。我现在可以简单地要求它解释代码的功能,不需要再浪费宝贵的时间和精力来破译写得不好的代码。

让我们想象一下,当我们正在尝试抓取 Linkedin,此时在互联网上发现了一个样例代码,该代码应该可以滚动 Linkedin 招聘网站。

问:What does the following code do? [insert code here]

#We find how many jobs are offered. jobs_num = driver.find_element(By.CSS_SELECTOR,"h1>span").get_attribute("innerText") if len(jobs_num.split(',')) > 1: jobs_num = int(jobs_num.split(',')[0])*1000 else: jobs_num = int(jobs_num) jobs_num = int(jobs_num) #Here I choose manually a number of jobs, so it wont take that long: jobs_num = 1000; #We create a while loop to browse all jobs. i = 2 while i <= int(jobs_num/2)+1: #We keep scrollind down to the end of the view. driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") i = i + 1 print("Current at: ", i, "Percentage at: ", ((i+1)/(int(jobs_num/2)+1))*100, "%",end="\r") try: #We try to click on the load more results buttons in case it is already displayed. infinite_scroller_button = driver.find_element(By.XPATH, ".//button[@aria-label='Load more results']") infinite_scroller_button.click() time.sleep(0.1) except: #If there is no button, there will be an error, so we keep scrolling down. time.sleep(0.1) pass

ChatGPT 立即对上面的代码进行了非常详细的解释。我们甚至可以要求在代码的给定部分提供更多的细节。

4.向代码添加注释

ChatGPT 还可以帮助我们提高自己代码的质量和可维护性。通过要求它逐行添加注释,使其他人更容易理解和使用我们的代码。

ChatGPT 可以显著提高可读性、可维护性以及与他人的协作。

让我们假设下面的代码根本没有注释。我们可以要求 ChaGPT 直接向其添加注释。

问:Add comments to the following code [insert code here]

# Find the number of jobs offered on the website jobs_num = driver.find_element(By.CSS_SELECTOR,"h1>span").get_attribute("innerText") # Check if the number of jobs contains a ',' and if so, convert it to number * 1000 if len(jobs_num.split(',')) > 1: jobs_num = int(jobs_num.split(',')[0])*1000 else: jobs_num = int(jobs_num) # Convert jobs_num to an integer jobs_num = int(jobs_num) # Set the number of jobs to scrape to 1000 jobs_num = 1000; # Initialize a counter variable to scroll through the jobs i = 2 # Start a while loop to scroll through the jobs while i <= int(jobs_num/2)+1: # Scroll to the bottom of the page driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # Increment the counter variable i = i + 1 # Print the current number of jobs scrolled through and the percentage of completion print("Current at: ", i, "Percentage at: ", ((i+1)/(int(jobs_num/2)+1))*100, "%",end="\r") try: # Try to locate the "Load more results" button and click on it infinite_scroller_button = driver.find_element(By.XPATH, ".//button[@aria-label='Load more results']") infinite_scroller_button.click() # Sleep for 0.1 seconds time.sleep(0.1) except: # If the button is not present, sleep for 0.1 seconds and continue scrolling time.sleep(0.1) pass 5.使用某种样式重写代码

ChatGPT 不仅是理解不熟悉代码的宝贵工具,还可以帮助我们确保自己的代码符合行业标准和惯例。通过要求它纠正我们的代码以符合 Pep-8 约定,甚至为我们的编码风格创建一个自定义约定,我们可以避免在合并来自不同 repo 或团队的代码时进行昂贵且耗时的重构。

这有助于简化协作流程,提高效率。总之,ChatGPT 是一个多功能工具,可以提高代码库的质量和可维护性。

如果我们要求 ChatGPT 使用 Pep-8 标准编写以前的代码,它将直接为我们提供重构的代码。

问:Can you rewrite the following code using Pep8 standard [Insert code here]

好了,这就是今天分享的5个 ChatGPT 功能,对于提升日常工作效率,还是非常棒的,要不要尝试一下呢~

到此这篇关于5个ChatGPT功能帮助你提升日常编码效率的文章就介绍到这了,更多相关ChatGPT功能内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



编码 效率

需要 登录 后方可回复, 如果你还没有账号请 注册新账号