代码自动生成工具实战-Cursor

简介: 之前看过github copilot 的代码生成能力。可以说解放了码农的双手,基础的代码完全可以来生成。可是后来它收费了。

Cursor code generate介绍

之前看过github copilot 的代码生成能力。可以说解放了码农的双手,基础的代码完全可以来生成。可是后来它收费了。

今天在网上发现这个免费的code generate。

下面官网537721b5cb224942b723223d573c38ad.png

Cursor 代码生成实战

prompt1

prompts : 比较两个数据库的差异,数据库的ip地址是10.50.10.170,…

bd35c4300cb241f7b4caea4f1d13380a.png

prompt2

我的prompts: 是两个数据库比较差异,第二个数据库是的ip是10.50.10.171. 两个数据库的port都是5432

# 导入所需库
import psycopg2
# 连接数据库
connection1 = psycopg2.connect(host='10.50.10.170', port=5432, user='username', password='xxx', dbname='qmstst')
cursor1 = connection1.cursor()
connection2 = psycopg2.connect(host='10.50.10.171', port=5432, user='username', password='xxx', dbname='qmstst')
cursor2 = connection2.cursor()
# 从文件中获取表名
with open("tables.txt", "r") as file:
    table_names = [line.strip() for line in file.readlines()]
differences = []
for i in range(len(table_names) - 1):
    # 查询两个表的数据量
    cursor1.execute(f"SELECT COUNT(*) FROM {table_names[i]}")
    table1_count = cursor1.fetchone()[0]
    cursor2.execute(f"SELECT COUNT(*) FROM {table_names[i+1]}")
    table2_count = cursor2.fetchone()[0]
    # 计算数据量差异
    difference = abs(table1_count - table2_count)
    differences.append(difference)
# 输出结果到文件
with open("output.txt", "w") as output_file:
    for i in range(len(differences)):
        output_file.write(f"表 {table_names[i]} 和表 {table_names[i+1]} 的数据量差异为: {differences[i]}\n")
# 关闭数据库连接
cursor1.close()
connection1.close()
cursor2.close()
connection2.close()

prompt3

promtps : 最终输出结果是有差异的表,结果需要有时间。需要包含哪个ip的哪个表。

1ff9444b0e784165ab73559e276b4a5a.png

# 导入所需库
import psycopg2
from datetime import datetime
# 连接数据库
connection1 = psycopg2.connect(host='10.50.10.170', port=5432, user='username', password='xxx', dbname='qmstst')
cursor1 = connection1.cursor()
connection2 = psycopg2.connect(host='10.50.10.171', port=5432, user='username', password='xxx', dbname='qmstst')
cursor2 = connection2.cursor()
# 从文件中获取表名
with open("tables.txt", "r") as file:
    table_names = [line.strip() for line in file.readlines()]
differences = []
for i in range(len(table_names) - 1):
    # 查询两个表的数据量
    cursor1.execute(f"SELECT COUNT(*) FROM {table_names[i]}")
    table1_count = cursor1.fetchone()[0]
    cursor2.execute(f"SELECT COUNT(*) FROM {table_names[i+1]}")
    table2_count = cursor2.fetchone()[0]
    # 计算数据量差异
    difference = abs(table1_count - table2_count)
    if difference > 0:
        differences.append((table_names[i], table_names[i+1], difference))
# 输出结果
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
for table1, table2, difference in differences:
    message = f"{current_time} - IP: 10.50.10.170 表 {table1} 和 IP: 10.50.10.171 表 {table2} 的数据量差异为: {difference}"
    print(message)
connection1.close()
cursor2.close()
connection2.close()

prompt4

将输出结果推送到企业微信

from datetime import datetime
# 连接数据库
connection1 = psycopg2.connect(host='10.50.10.170', port=5432, user='username', password='xxx', dbname='qmstst')
cursor1 = connection1.cursor()
connection2 = psycopg2.connect(host='10.50.10.171', port=5432, user='username', password='xxx', dbname='qmstst')
cursor2 = connection2.cursor()
# 从文件中获取表名
with open("tables.txt", "r") as file:
    table_names = [line.strip() for line in file.readlines()]
differences = []
for i in range(len(table_names) - 1):
    # 查询两个表的数据量
    cursor1.execute(f"SELECT COUNT(*) FROM {table_names[i]}")
    table1_count = cursor1.fetchone()[0]
    cursor2.execute(f"SELECT COUNT(*) FROM {table_names[i+1]}")
    table2_count = cursor2.fetchone()[0]
    # 计算数据量差异
    difference = abs(table1_count - table2_count)
    if difference > 0:
        differences.append((table_names[i], table_names[i+1], difference))
# 导入企业微信库
from wxwork_bot import WxWorkBot
# 初始化企业微信机器人
bot = WxWorkBot("your_bot_key")
# 输出结果并发送到企业微信报警
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
for table1, table2, difference in differences:
    message = f"{current_time} - IP: 10.50.10.170 表 {table1} 和 IP: 10.50.10.171 表 {table2} 的数据量差异为: {difference}"
    bot.send_text(message)
connection1.close()
cursor2.close()
connection2.close()

我的感受

  • 从prompt1可以看出代码生成之后还会提示合并还是丢弃,这个比较人性化。因为不确定cursor对prompts的理解,这样的好处是可以及时调整prompts来生成更加符合要求的结果
  • 从prompt3来看这个还是会一行一行扫描,而不是直接定位到需要修改的地方。这一点比较慢。
  • 其实可以只对部分代码进行框选进行优化,这个速度应该会提升。
  • 竟然可以回滚prompt。


目录
相关文章
|
29天前
|
SQL API Python
Python DB API下规范下cursor对象常用接口
Python DB API下规范下cursor对象常用接口。
21 4
|
4天前
|
SQL 数据库 数据库管理
python自动生成SQL语句自动化
python自动生成SQL语句自动化
23 1
|
11天前
idea自动生成not null判断语句
idea自动生成not null判断语句
|
3月前
|
C++
win32编程 -- 通过空项目学习自动生成的代码框架
win32编程 -- 通过空项目学习自动生成的代码框架
13 0
|
5月前
|
Oracle Java 关系型数据库
Generator【SpringBoot集成】代码生成+knife4j接口文档(2种模板设置、逻辑删除、字段填充 含代码粘贴可用)保姆级教程(注意事项+建表SQL+代码生成类封装+测试类)
Generator【SpringBoot集成】代码生成+knife4j接口文档(2种模板设置、逻辑删除、字段填充 含代码粘贴可用)保姆级教程(注意事项+建表SQL+代码生成类封装+测试类)
28 0
|
5月前
|
JSON 数据库 数据格式
gorm 教程三 gen自动代码生成工具
gorm 教程三 gen自动代码生成工具
152 0
|
10月前
|
SQL 数据可视化 搜索推荐
自动生成HQL代码的工具
本文介绍了一个可视化工具,可以通过界面增删指标并快速选择指标类型,实现快速指标更改增删,将原本1小时的工作量缩减到1分钟,提高开发效率。同时,工具可以保存配置信息快照,方便复用,并且不依赖于第三方编程语言的运行环境,降低了用户使用难度。主界面包括表的基本信息、执行按钮、代码显示窗口和表配置标签,支持一次性生成多个不同表的代码。
116 0
|
Python
Python编程:fire库自动生成命令行接口
Python编程:fire库自动生成命令行接口
103 0
|
SQL JSON DataWorks
【教程】利用宜撘实现自动生成业务SQL功能
模板地址: https://yida.alibaba-inc.com/newApp.html?#/template/TPL_LPS8OWKVUIEHEVM80XFN?_k=z3r1e4 背景: 之前在内网上有讨论SQL算不算是编程语言,一石激起千层浪,大家参与度非常高,有很多运营同学也表示会用SQL,我觉得这个是很好的现象。编程语言只是解决问题的工具,黑猫白猫,能抓到老鼠就是好猫。 这个问
1134 0
|
API 网络架构
docfx 简单使用方法、自动生成目录的工具
docfx 简单使用方法、自动生成目录的工具
292 0