> 文章列表 > ChatGPT实战100例 - (09) Python工具类库终结者

ChatGPT实战100例 - (09) Python工具类库终结者

ChatGPT实战100例 - (09) Python工具类库终结者

文章目录

  • ChatGPT实战100例 - (09) Python工具类库终结者
    • 一、需求与思路
    • 二、时间工具
    • 三、扩充工具
    • 四、编写测试
    • 五、 总结

ChatGPT实战100例 - (09) Python工具类库终结者

一、需求与思路

自从用了ChatGPT,再也不用满大街找工具类了。
需要啥工具,咱用ChatGPT自己写一个,还自带测试,它不香么?

以时间工具为例

二、时间工具

问题:

作为一名优秀的Python专家,写一个python的时间工具

回答:
这里是一个简单的Python时间工具类:

import timeclass TimeUtil:"""时间工具类"""@staticmethoddef get_current_timestamp():"""获取当前时间戳"""return time.time()@staticmethoddef get_current_datetime():"""获取当前日期和时间"""return time.strftime("%Y-%m-%d %H:%M:%S")@staticmethoddef get_current_date():"""获取当前日期"""return time.strftime("%Y-%m-%d")@staticmethod    def timestamp_to_datetime(timestamp):"""时间戳转换为日期时间"""return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))@staticmethod    def datetime_to_timestamp(date_str):"""日期时间转换为时间戳"""return time.mktime(time.strptime(date_str, "%Y-%m-%d %H:%M:%S"))@staticmethoddef date_to_timestamp(date_str):"""日期转换为时间戳"""return time.mktime(time.strptime(date_str, "%Y-%m-%d"))@staticmethoddef get_time_diff(start, end):"""计算时间差,返回秒"""return end - start @staticmethoddef get_date_diff(start_date, end_date):"""计算日期差,返回天数"""start_timestamp = TimeUtil.date_to_timestamp(start_date)end_timestamp = TimeUtil.date_to_timestamp(end_date)return (end_timestamp - start_timestamp) // (24 * 60 * 60) 

这个时间工具类提供了如下功能:

  • 获取当前时间戳、日期时间、日期
  • 时间戳与日期时间、日期的互相转换
  • 计算时间差和日期差

我们可以这样使用该工具类:

>>> from time_util import TimeUtil
>>> TimeUtil.get_current_timestamp()
1574657271.9618215
>>> TimeUtil.get_current_datetime()
'2019-11-23 17:14:31'
>>> TimeUtil.timestamp_to_datetime(1574657271.9618215)
'2019-11-23 17:14:31'
>>> TimeUtil.get_date_diff('2019-11-22', '2019-11-25')
3

三、扩充工具

问题:

添加sleep函数

回答:
可以在TimeUtil类中添加一个sleep()静态方法,如下:

import timeclass TimeUtil:"""时间工具类"""# 省略其他方法...@staticmethoddef sleep(seconds):"""睡眠指定秒数"""time.sleep(seconds)

这个sleep()方法可以让当前线程睡眠指定的秒数,用来实现定时效果。
我们可以这样使用sleep()方法:

>>> from time_util import TimeUtil 
>>> TimeUtil.sleep(3)  # 睡眠3秒

3秒后,程序将继续运行下一行代码。
sleep()是一个blocking方法,在睡眠期间,程序不会执行任何操作。
所以,有了这个方法,TimeUtil类的功能更加完备,可以实现时间戳、日期时间转换,时间差和日期差计算,以及线程定时等待等功能。

四、编写测试

问题:

使用pytest编写对这个工具类的测试

回答:
这里使用pytest编写TimeUtil类的测试用例:

import time
from time_util import TimeUtildef test_get_current_timestamp():"""测试获取当前时间戳"""timestamp = TimeUtil.get_current_timestamp()assert isinstance(timestamp, float) and timestamp > 0def test_get_current_datetime():"""测试获取当前日期时间"""datetime = TimeUtil.get_current_datetime()assert isinstance(datetime, str) and len(datetime) == 19def test_get_current_date():"""测试获取当前日期"""date = TimeUtil.get_current_date()assert isinstance(date, str) and len(date) == 10  def test_timestamp_to_datetime():"""测试时间戳转换日期时间"""timestamp = 1574684897.0933521datetime = TimeUtil.timestamp_to_datetime(timestamp)assert datetime == "2019-11-24 11:08:17"   def test_datetime_to_timestamp():"""测试日期时间转换时间戳"""datetime = "2019-11-24 11:08:17"timestamp = TimeUtil.datetime_to_timestamp(datetime)assert timestamp == 1574684897.0def test_get_time_diff():"""测试计算时间差"""start, end = 1574684897.0933521, 1574685997.0933521diff = TimeUtil.get_time_diff(start, end)assert diff == 60     def test_sleep():"""测试睡眠功能"""start = TimeUtil.get_current_timestamp()TimeUtil.sleep(3)end = TimeUtil.get_current_timestamp()assert end - start >= 3 

我们编写了7个测试用例,对应TimeUtil类中方法的主要功能,包括:

  • 获取当前时间戳/日期时间/日期
  • 时间戳与日期时间互相转换
  • 计算时间差
  • 睡眠功能

运行该测试用例,结果如下:

$ pytest test_time_util.py     =========================== test session starts ============================
platform darwin -- Python 3.7.4, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /tmp
collected 7 items                                                              test_time_util.py .......                                                     [100%]=========================== 7 passed in 0.12s ============================

7个测试用例全部通过,这证明TimeUtil类的方法设计都是正确和可靠的。
在实际开发中,编写测试用例对于保障代码质量至关重要。希望这个示例能够鼓励你编写更加完整和严谨的测试。

五、 总结

回顾一下,本节主要prompt知识点:

  • CPU大法
作为一名优秀的{高帽子},请{干啥}
  • 编写工具
写一个{某语言}的{某通用功能}工具
  • 扩充函数
添加函数{说明}
  • 编写测试
使用{测试库}编写对这个工具类的测试