Open main menu
首页
专栏
课程
分类
归档
Chat
Sci-Hub
谷歌学术
Libgen
GitHub镜像
登录/注册
搜索
关闭
Previous
Previous
Next
Next
5个ChatGPT功能让你嘎嘎提升写代码效率和质量
sockstack
/
189
/
2024-02-28 12:02:38
<p><span style="color: red; font-size: 18px">ChatGPT 可用网址,仅供交流学习使用,如对您有所帮助,请收藏并推荐给需要的朋友。</span><br><a href="https://ckai.xyz/?sockstack§ion=detail" target="__blank">https://ckai.xyz</a><br><br></p> <article class="baidu_pl"><div id="article_content" class="article_content clearfix"> <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css"> <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-25cebea3f9.css"> <div id="content_views" class="markdown_views prism-atom-one-dark"> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg><h1> <a id="5ChatGPT_1"></a>5个ChatGPT功能嘎嘎提升写代码效率和质量</h1> <p>ChatGPT在代码开发方面带来了巨大变革,尽管如此,仍有许多软件开发人员未能充分利用ChatGPT来优化和简化工作流程。在这里,我们将介绍5个可用于提高你日常工作效率和质量的ChatGPT功能。</p> <p>下面将详细了解如何利用这些功能👇🏻:</p> <h2> <a id="1__8"></a>1. 生成代码骨架</h2> <p>当你需要从头构建一个项目时,GPT可以提供巨大的帮助。只需几个提示,它就可以为你生成所需的代码骨架,包括选择的技术、框架和版本。它不仅能大幅减少你的工作时间,而且还有助于保持你的文档或团队文档的组织和更新。</p> <p>例如,想象一下我想要使用一些外部API。我可以要求ChatGPT为我起草一个初始的代码结构。</p> <blockquote> <p>用 Python 生成一个请求外部API的样版代码</p> </blockquote> <p>ChatGPT立即回答:</p> <pre><code class="prism language-python"><span class="token keyword">import</span> requests<span class="token keyword">def</span> <span class="token function">make_api_request</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>url <span class="token operator">=</span> <span class="token string">"https://api.example.com/endpoint"</span>ChatGPT4镜像站<span class="token comment"># 设置请求头(如果需要)</span>headers <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span><span class="token string">"Content-Type"</span><span class="token punctuation">:</span> <span class="token string">"application/json"</span><span class="token punctuation">,</span><span class="token string">"Authorization"</span><span class="token punctuation">:</span> <span class="token string">"Bearer your_token"</span><span class="token punctuation">}</span><span class="token comment"># 设置请求参数(如果需要)</span>params <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span><span class="token string">"param1"</span><span class="token punctuation">:</span> <span class="token string">"value1"</span><span class="token punctuation">,</span><span class="token string">"param2"</span><span class="token punctuation">:</span> <span class="token string">"value2"</span><span class="token punctuation">}</span><span class="token keyword">try</span><span class="token punctuation">:</span><span class="token comment"># 发送 GET 请求</span>response <span class="token operator">=</span> requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>url<span class="token punctuation">,</span> headers<span class="token operator">=</span>headers<span class="token punctuation">,</span> params<span class="token operator">=</span>params<span class="token punctuation">)</span><span class="token comment"># 检查响应状态码</span><span class="token keyword">if</span> response<span class="token punctuation">.</span>status_code <span class="token operator">==</span> <span class="token number">200</span><span class="token punctuation">:</span><span class="token comment"># 响应正常</span>data <span class="token operator">=</span> response<span class="token punctuation">.</span>json<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment"># 在这里处理返回的数据</span><span class="token keyword">else</span><span class="token punctuation">:</span><span class="token comment"># 响应出现错误</span><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"API 请求失败,状态码:"</span><span class="token punctuation">,</span> response<span class="token punctuation">.</span>status_code<span class="token punctuation">)</span><span class="token keyword">except</span> requests<span class="token punctuation">.</span>exceptions<span class="token punctuation">.</span>RequestException <span class="token keyword">as</span> e<span class="token punctuation">:</span><span class="token comment"># 请求发生异常</span><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"请求发生异常:"</span><span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment"># 调用函数发送 API 请求</span> make_api_request<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre> <p>或者,我甚至可以要求生成使用 Selenium v4 库爬取百度热搜的代码骨架,ChatGPT给我以下输出:</p> <pre><code class="prism language-python"><span class="token keyword">from</span> selenium <span class="token keyword">import</span> webdriver <span class="token keyword">from</span> selenium<span class="token punctuation">.</span>webdriver<span class="token punctuation">.</span>chrome<span class="token punctuation">.</span>service <span class="token keyword">import</span> Service <span class="token keyword">from</span> selenium<span class="token punctuation">.</span>webdriver<span class="token punctuation">.</span>common<span class="token punctuation">.</span>by <span class="token keyword">import</span> By <span class="token keyword">from</span> selenium<span class="token punctuation">.</span>webdriver<span class="token punctuation">.</span>chrome<span class="token punctuation">.</span>options <span class="token keyword">import</span> Options<span class="token keyword">def</span> <span class="token function">scrape_baidu_hot_search</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token comment"># 设置 Chrome 浏览器驱动路径</span>chromedriver_path <span class="token operator">=</span> <span class="token string">"path_to_chromedriver"</span><span class="token comment"># 创建 ChromeOptions 对象</span>chrome_options <span class="token operator">=</span> Options<span class="token punctuation">(</span><span class="token punctuation">)</span>chrome_options<span class="token punctuation">.</span>add_argument<span class="token punctuation">(</span><span class="token string">"--headless"</span><span class="token punctuation">)</span> <span class="token comment"># 可选,以无头模式运行浏览器</span><span class="token comment"># 创建 ChromeDriver 服务</span>service <span class="token operator">=</span> Service<span class="token punctuation">(</span>chromedriver_path<span class="token punctuation">)</span><span class="token comment"># 创建 Chrome 浏览器实例ChatGPT4镜像站</span>driver <span class="token operator">=</span> webdriver<span class="token punctuation">.</span>Chrome<span class="token punctuation">(</span>service<span class="token operator">=</span>service<span class="token punctuation">,</span> options<span class="token operator">=</span>chrome_options<span class="token punctuation">)</span><span class="token keyword">try</span><span class="token punctuation">:</span><span class="token comment"># 打开百度热搜页面</span>driver<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"https://top.baidu.com/board?tab=realtime"</span><span class="token punctuation">)</span><span class="token comment"># 在页面等待一段时间,确保页面加载完成</span>driver<span class="token punctuation">.</span>implicitly_wait<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token comment"># 定位热搜榜单元素</span>hot_search_elements <span class="token operator">=</span> driver<span class="token punctuation">.</span>find_elements<span class="token punctuation">(</span>By<span class="token punctuation">.</span>CSS_SELECTOR<span class="token punctuation">,</span> <span class="token string">".keyword-list li .keyword-link"</span><span class="token punctuation">)</span><span class="token comment"># 遍历热搜榜单元素并提取信息</span><span class="token keyword">for</span> element <span class="token keyword">in</span> hot_search_elements<span class="token punctuation">:</span><span class="token comment"># 提取热搜文本</span>hot_search_text <span class="token operator">=</span> element<span class="token punctuation">.</span>text<span class="token keyword">print</span><span class="token punctuation">(</span>hot_search_text<span class="token punctuation">)</span><span class="token keyword">finally</span><span class="token punctuation">:</span><span class="token comment"># 关闭浏览器实例</span>driver<span class="token punctuation">.</span>quit<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment"># 调用函数开始爬取百度热搜</span> scrape_baidu_hot_search<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre> <p>如果哪里不对,你只要告诉GPT,让它重新生成 。比如上面的热搜内容不对,你只要这样提示</p> <blockquote> <p>修改为获取 class 名为 c-single-text-ellipsis 这个的内容</p> </blockquote> <p>ChatGPT立即回答:<br> 要获取 class 名为 “c-single-text-ellipsis” 的元素内容,你可以使用 Selenium 的 find_element 方法结合 text 属性。以下是示例代码:</p> <pre><code>... # 定位热搜榜单元素hot_search_elements = driver.find_element(By.CLASS_NAME, "c-single-text-ellipsis") ... </code></pre> <p>GPT就会帮你重新修改代码,甚至你可以把错误信息回复给GPT,让它帮你解决问题,很简单,对吧?</p> <h2> <a id="2__120"></a>2. 研究和比较</h2> <p>在选择如何实施某个功能时可能会很困难,特别是当有多个选项可供选择时。我通常的做法是为每种方法创建一个基本的概念验证,然后进行比较。但是,有了ChatGPT的帮助,这个过程变得更加容易。</p> <p>现在我可以直接向ChatGPT询问它对于代码开发中哪个选项或库最好的专家意见。这样可以节省我在决策过程中的时间和精力,并确保我使用的是最适合工作的工具。</p> <p>假设我想要处理地理空间数据,但我不确定是应该使用Geopandas还是Plotly。我可以向ChatGPT询问它们之间的比较 - 包括类型在内 - 它会立即回答两个库之间的主要区别。</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/img_convert/39b334ce91f69fc7a41c9b399b728ae5.png" alt=""><br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/img_convert/9ffe086164d80a4ed15d5c4e66fb1c58.png" alt=""></p> <p>ChatGPT聊天截图。ChatGPT向我解释了geopandas和plotly之间的区别。</p> <p>如果我现在想要爬取一个网站,我可以询问哪个是最好的库来实现这个功能。ChatGPT会回答Python中最流行的网络爬虫库。</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/img_convert/89f27588583b7ecda9a35f2be98a70ac.png" alt=""><br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/img_convert/63a3c63f8a124d4c30ca8082fff95589.png" alt=""></p> <p>ChatGPT聊天截图。ChatGPT解释了最受欢迎的网页爬取库。</p> <p>你甚至可以询问爬取社交网络的最佳选项 - 尽管ChatGPT很可能会警告你这将违反该网站的内容政策。</p> <p><strong>请确保遵守相关政策,注意不要触犯隐私和版权问题。</strong></p> <h2> <a id="3__143"></a>3. 理解代码</h2> <p>我们都有过这种经历,在努力理解一个不是我们创建的代码库时。浏览一个复杂且组织混乱的代码库(也被称为屎山)可能是一项令人崩溃且耗时的任务。</p> <p>但是,有了ChatGPT,理解新代码库变得更加容易。现在我可以简单地询问它解释代码的功能,并且在短时间内理解代码。不再浪费宝贵的时间和精力来解读写得糟糕的代码。</p> <p>只要把代码复制到输入框,并询问这段代码是什么意思?ChatGPT立即给出了对上述代码的详细解释。你甚至可以要求在代码的某个部分提供更多细节。</p> <h2> <a id="4__150"></a>4. 为我们的代码添加注释</h2> <p>ChatGPT还可以帮助我们改进代码的质量和可维护性。通过要求它逐行添加注释,我们可以确保在发布代码之前对其进行适当的文档记录。这消除了无注释的代码的需要,并使他人更容易理解和使用我们的代码。</p> <p>ChatGPT可以显著提高代码的可读性、可维护性和与他人的协作。</p> <p>假设之前的代码完全没有注释。我们只需让ChatGPT直接为其添加注释。</p> <p>给以下代码添加注释 [在这里插入代码]</p> <pre><code class="prism language-#">jobs_num = driver.find_element(By.CSS_SELECTOR,"h1>span").get_attribute("innerText")# 检查工作数量是否包含“,”,如果是,则将其转换为数字* 1000 if len(jobs_num.split(',')) > 1:jobs_num = int(jobs_num.split(',')[0])*1000 else:jobs_num = int(jobs_num)# 将jobs_num转换为整数 jobs_num = int(jobs_num)# 设置要爬取的工作数量为1000 jobs_num = 1000;# 初始化一个计数器变量以浏览工作 i = 2# 开始一个while循环以浏览工作 while i <= int(jobs_num/2)+1:# 滚动到页面底部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:# 尝试定位“Load more results”按钮并点击它infinite_scroller_button = driver.find_element(By.XPATH, ".//button[@aria-label='Load more results']")infinite_scroller_button.click()# 等待0.1秒time.sleep(0.1)except:# 如果按钮不存在,等待0.1秒并继续滚动time.sleep(0.1)pass </code></pre> <p>特别对于英文不好的同学,写开源代码时可以让GPT帮你添加英文注释,真的太好用了吧!</p> <h2> <a id="5__199"></a>5. 使用一些规范重写我们的代码</h2> <p>ChatGPT不仅是理解陌生代码的宝贵工具,还可以帮助我们确保自己的代码符合行业标准和约定。通过要求它将我们的代码修改为符合Pep-8约定 - 或者甚至为我们的编码风格创建自定义约定 - 我们可以避免在合并来自不同代码库或团队的代码时需要进行昂贵和耗时的重构。</p> <p>这有助于简化协作流程并使其更加高效。总体而言,ChatGPT是一个多功能工具,可以提高代码库的质量和可维护性。</p> <p>如果我们要求ChatGPT使用Pep-8标准重新编写之前的代码,它将直接给我们重构后的代码。</p> <p>你能够使用Pep8标准重写以下代码吗?[在这里插入代码]</p> <pre><code class="prism language-python"><span class="token comment"># Find the number of jobs offered on the website</span> jobs_num <span class="token operator">=</span> driver<span class="token punctuation">.</span>find_element<span class="token punctuation">(</span>By<span class="token punctuation">.</span>CSS_SELECTOR<span class="token punctuation">,</span> <span class="token string">"h1>span"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>get_attribute<span class="token punctuation">(</span><span class="token string">"innerText"</span><span class="token punctuation">)</span><span class="token comment"># Check if the number of jobs contains a ',' and if so, convert it to a number * 1000</span> <span class="token keyword">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>jobs_num<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">','</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">:</span>jobs_num <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>jobs_num<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">','</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">1000</span> <span class="token keyword">else</span><span class="token punctuation">:</span>jobs_num <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>jobs_num<span class="token punctuation">)</span><span class="token comment"># Convert jobs_num to an integer</span> jobs_num <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>jobs_num<span class="token punctuation">)</span><span class="token comment"># Set the number of jobs to scrape to 1000</span> jobs_num <span class="token operator">=</span> <span class="token number">1000</span><span class="token comment"># Initialize a counter variable to scroll through the jobs</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token comment"># Start a while loop to scroll through the jobs</span> <span class="token keyword">while</span> i <span class="token operator"><=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>jobs_num <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">:</span><span class="token comment"># Scroll to the bottom of the page</span>driver<span class="token punctuation">.</span>execute_script<span class="token punctuation">(</span><span class="token string">"window.scrollTo(0, document.body.scrollHeight);"</span><span class="token punctuation">)</span><span class="token comment"># Increment the counter variable</span>i <span class="token operator">+=</span> <span class="token number">1</span><span class="token comment"># Print the current number of jobs scrolled through and the percentage of completion</span><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Current at:"</span><span class="token punctuation">,</span> i<span class="token punctuation">,</span> <span class="token string">"Percentage at:"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">(</span>jobs_num <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token string">"%"</span><span class="token punctuation">,</span> end<span class="token operator">=</span><span class="token string">"\r"</span><span class="token punctuation">)</span><span class="token keyword">try</span><span class="token punctuation">:</span><span class="token comment"># Try to locate the "Load more results" button and click on it</span>infinite_scroller_button <span class="token operator">=</span> driver<span class="token punctuation">.</span>find_element<span class="token punctuation">(</span>By<span class="token punctuation">.</span>XPATH<span class="token punctuation">,</span> <span class="token string">".//button[@aria-label='Load more results']"</span><span class="token punctuation">)</span>infinite_scroller_button<span class="token punctuation">.</span>click<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment"># Sleep for 0.1 seconds</span>time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span><span class="token keyword">except</span><span class="token punctuation">:</span><span class="token comment"># If the button is not present, sleep for 0.1 seconds and continue scrolling</span>time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span><span class="token keyword">pass</span> </code></pre> <p>同样,当需要为函数命名时,你只需向GPT介绍你的函数功能,它便能为你提供多个不同的命名选项供你选择,这样就不再为命名而感到烦恼。</p> <p>更多的功能等待你去发现!</p> <h2> <a id="_251"></a>结论</h2> <p>在读完本文之后,我希望你能了解到,ChatGPT能够助力我们提高工作效率,使人工智能成为我们的得力助手。然而,值得注意的是,在与人工智能协作的过程中,我们同样需要保持批判性思维,就像在与人类同事合作时一样。</p> <p>因此,在对基于人工智能所生成的答案急于付诸实践之前,请务必确保留出时间对其进行审查和评估。</p> <p>附: ChatGPT4镜像站</p> </div> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-98b95bb57c.css" rel="stylesheet"> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-c216769e99.css" rel="stylesheet"> </div> <div id="treeSkill"></div></article>
5个ChatGPT功能让你嘎嘎提升写代码效率和质量
作者
sockstack
许可协议
CC BY 4.0
发布于
2024-02-28
修改于
2024-12-25
上一篇:软件:常用 Linux 软件汇总,值得收藏
下一篇:带你玩转ChatGPT提示工程-基本提示
尚未登录
登录 / 注册
文章分类
博客重构之路
5
Spring Boot简单入门
4
k8s 入门教程
0
MySQL 知识
1
NSQ 消息队列
0
ThinkPHP5 源码分析
5
使用 Docker 从零开始搭建私人代码仓库
3
日常开发汇总
4
标签列表
springboot
hyperf
swoole
webman
php
多线程
数据结构
docker
k8s
thinkphp
mysql
tailwindcss
flowbite
css
前端