广告位联系
返回顶部
分享到

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

相关技巧 来源:互联网 作者:佚名 发布时间:2023-02-20 22:12:55 人浏览
摘要

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

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

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

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

1.生成编码框架

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

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

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

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

ChatGPT 的输出如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

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]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

#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]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

# 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 功能,对于提升日常工作效率,还是非常棒的,要不要尝试一下呢~


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/qq_34160248/article/details/129073971
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计