5個(gè)ChatGPT功能幫助你提升日常編碼效率
ChatGPT 的出現(xiàn),徹底改變了開(kāi)發(fā)代碼的方式。但是目前為止,大多數(shù)軟件開(kāi)發(fā)人員和數(shù)據(jù)專業(yè)人員仍然沒(méi)有習(xí)慣使用 ChatGPT 來(lái)改進(jìn)和簡(jiǎn)化自己的工作。
這就是為什么我在這里概述了5個(gè)不同的功能,以提高日常工作的速度和質(zhì)量。
下面讓我們一起探索如何在日常工作中使用它們
1.生成編碼框架
ChatGPT 是我們從頭開(kāi)始構(gòu)建新項(xiàng)目的秘密武器。只需幾個(gè)提示,它就可以生成我們所需的代碼框架,以及所選擇的技術(shù)、框架和版本。它不僅每次為我們節(jié)省了至少一個(gè)小時(shí)左右的工作時(shí)間,而且有助于保持文檔(或團(tuán)隊(duì)的文檔)的有效組織和更新,這就像團(tuán)隊(duì)中多了一雙手,一雙勤勞的手!
例如,我們想使用一些外部API,可以請(qǐng) ChatGPT 為我們起草一個(gè)初始代碼結(jié)構(gòu)。
問(wèn):Generate boiler plate code that integrates an extern API using Python.
或者我們甚至可以要求這個(gè)框架使用 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("http://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("http://span[@class='inline t-24 t-black t-normal break-words']").text location = driver.find_element_by_xpath("http://span[@class='t-16 t-black t-normal inline-block']").text print(name) print(location) # close the browser driver.quit()
很簡(jiǎn)單對(duì)吧?嘗試用我們能想象的任何項(xiàng)目去挑戰(zhàn) ChatGPT 吧。
2.研究和比較
決定如何實(shí)施某件事可能是很困難的,特別是當(dāng)有多種情況可供選擇時(shí)。我的方法是為每種選擇創(chuàng)建一個(gè)基本的概念模型,然后比較它們。
但是,在 ChatGPT 的幫助下,這個(gè)過(guò)程變得簡(jiǎn)單多了。
我們現(xiàn)在可以直接向它征求專家級(jí)別的意見(jiàn),以確定哪種選項(xiàng)或庫(kù)最適合我們的代碼開(kāi)發(fā)。這節(jié)省了我們?cè)跊Q策過(guò)程中的時(shí)間和精力,并確保使用了最佳的工具。
讓我們想象一下,我想使用地理空間數(shù)據(jù),但我不確定是否應(yīng)該使用 Geostandard 或 Plotly。我們可以要求 ChatGPT 進(jìn)行比較,它立即回答了兩個(gè)庫(kù)之間的主要區(qū)別。
如果現(xiàn)在我們想抓取網(wǎng)站,就可以問(wèn)什么是最好的庫(kù)。ChatGPT 會(huì)用 Python 中最流行的 web 抓取庫(kù)來(lái)回答。
我們甚至可以詢問(wèn)想要抓取的網(wǎng)站的最佳方式是什么——盡管 ChatGPT 很可能會(huì)警告你這將違反該網(wǎng)站的內(nèi)容政策——所以要小心。
問(wèn):What’s the best option to scrape a social network?
3.理解代碼
在日常工作當(dāng)中,我們都在努力理解一個(gè)不是由我們創(chuàng)建的代碼庫(kù)。瀏覽一個(gè)復(fù)雜且組織不良的代碼可能是一項(xiàng)令人崩潰的任務(wù)。
但是,通過(guò) ChatGPT,理解新的代碼庫(kù)變得容易多了。我現(xiàn)在可以簡(jiǎn)單地要求它解釋代碼的功能,不需要再浪費(fèi)寶貴的時(shí)間和精力來(lái)破譯寫(xiě)得不好的代碼。
讓我們想象一下,當(dāng)我們正在嘗試抓取 Linkedin,此時(shí)在互聯(lián)網(wǎng)上發(fā)現(xiàn)了一個(gè)樣例代碼,該代碼應(yīng)該可以滾動(dòng) Linkedin 招聘網(wǎng)站。
問(wèn):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 立即對(duì)上面的代碼進(jìn)行了非常詳細(xì)的解釋。我們甚至可以要求在代碼的給定部分提供更多的細(xì)節(jié)。
4.向代碼添加注釋
ChatGPT 還可以幫助我們提高自己代碼的質(zhì)量和可維護(hù)性。通過(guò)要求它逐行添加注釋,使其他人更容易理解和使用我們的代碼。
ChatGPT 可以顯著提高可讀性、可維護(hù)性以及與他人的協(xié)作。
讓我們假設(shè)下面的代碼根本沒(méi)有注釋。我們可以要求 ChaGPT 直接向其添加注釋。
問(wèn):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.使用某種樣式重寫(xiě)代碼
ChatGPT 不僅是理解不熟悉代碼的寶貴工具,還可以幫助我們確保自己的代碼符合行業(yè)標(biāo)準(zhǔn)和慣例。通過(guò)要求它糾正我們的代碼以符合 Pep-8 約定,甚至為我們的編碼風(fēng)格創(chuàng)建一個(gè)自定義約定,我們可以避免在合并來(lái)自不同 repo 或團(tuán)隊(duì)的代碼時(shí)進(jìn)行昂貴且耗時(shí)的重構(gòu)。
這有助于簡(jiǎn)化協(xié)作流程,提高效率??傊?,ChatGPT 是一個(gè)多功能工具,可以提高代碼庫(kù)的質(zhì)量和可維護(hù)性。
如果我們要求 ChatGPT 使用 Pep-8 標(biāo)準(zhǔn)編寫(xiě)以前的代碼,它將直接為我們提供重構(gòu)的代碼。
問(wèn):Can you rewrite the following code using Pep8 standard [Insert code here]
好了,這就是今天分享的5個(gè) ChatGPT 功能,對(duì)于提升日常工作效率,還是非常棒的,要不要嘗試一下呢~
到此這篇關(guān)于5個(gè)ChatGPT功能幫助你提升日常編碼效率的文章就介紹到這了,更多相關(guān)ChatGPT功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA 2020最新注冊(cè)碼(親測(cè)有效,可激活至 2089 年
這篇文章主要介紹了IntelliJ IDEA 2020最新注冊(cè)碼,親測(cè)有效,可激活至 2089 年,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05使用selenium自動(dòng)控制瀏覽器找不到Chromedriver問(wèn)題
這篇文章主要介紹了ChromeDriver安裝與配置問(wèn)題的解決方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02如何免費(fèi)獲取 Jetbrain 全家桶使用兌換碼的正確姿勢(shì)(推薦)
這篇文章主要介紹了免費(fèi)獲取 Jetbrain 全家桶使用兌換碼的正確姿勢(shì)(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09IE 打開(kāi)服務(wù)器下的MHT文件的實(shí)現(xiàn)方法
在和同學(xué)做畢業(yè)設(shè)計(jì)的時(shí)候,要能夠提供PPT課件的在線播放.要做到這一點(diǎn)并不難,只需將PPT課件轉(zhuǎn)換為網(wǎng)頁(yè)文件htm或mht文件即可。首先解釋下mht文件。2009-04-04Bottle部署web服務(wù)及postman接口的方法
這篇文章主要介紹了Bottle部署web服務(wù)及postman接口的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01ISO-8859-1 、Latin-1 西歐編碼介紹及應(yīng)用
這篇文章主要介紹了ISO-8859-1 、Latin-1 西歐編碼介紹及應(yīng)用,需要的朋友可以參考下2016-06-06