重构Time库的抽象设计

今天,我发现了一个抽象的事情,就是Time库的输出格式很奇怪。因此,不满意自然是要动手重构。下面我写了一个模块,文件是Time_Overwrite.py。代码文件内容如下:

python 复制代码
import subprocess

global character,Tips

Tips = (
        "The Time Style is Error!!\nPlease you input '-' or '_' or '/'\nSet Time as "
        "Year-Month-Day-Weekday or Year_Month_Day_Weekday or Year/Month/Day/Weekday"
)

def YMD_Time(character):
    Dos_Time = subprocess.getoutput('ECHO %DATE%')
    if character == '/':
        DT = Dos_Time.replace(' ',f'{character}')
        return DT
    elif character == '-':
        DT = Dos_Time.replace(' ',f'{character}').replace('/',f'{character}')
        return DT
    elif character == '_':
        DT = Dos_Time.replace(' ',f'{character}').replace('/',f'{character}')
        return DT
    else:
        return Tips

def HMS_Time():
    DosTime = subprocess.getoutput("ECHO %Time%")
    DosTime2 = DosTime.strip()
    Time = DosTime2[:-3]
    return Time

def All_Time(character=None):
    Half_Time1 = YMD_Time(character)
    if Half_Time1 != Tips:
        Half_Time2 = HMS_Time()
        Total_Time = Half_Time1 + ' ' + Half_Time2
        return Total_Time
    else:
        return Tips

我在同一目录下的另一个example.py调用它,测试代码如下:

python 复制代码
import Time_Overwrite as TO

Today_Time = TO.YMD_Time('_')
print(Today_Time)
Today_Time = TO.YMD_Time('-')
print(Today_Time)
Today_Time = TO.YMD_Time('/')
print(Today_Time)
Today_Time = TO.YMD_Time('%')
print(Today_Time)

print()

NowTime = TO.HMS_Time()
print(NowTime)

print()

Total_Time = TO.All_Time()
print(Total_Time)
Total_Time = TO.All_Time('_')
print(Total_Time)
Total_Time = TO.All_Time('/')
print(Total_Time)
Total_Time = TO.All_Time('-')
print(Total_Time)
Total_Time = TO.All_Time('%')
print(Total_Time)

测试结果:

总结

不满意就自己动手重构,没有人是完美的,前人设计的Time库值得肯定,不过能更新继续发展才是正道,而不是一边温水煮青蛙,还要一直硬着头皮坚持原来的旧路。

相关推荐
AI源码1 小时前
基于 Python 的短视频用户行为分析系统
开发语言·python
第九序言T1 小时前
数据分析岗 Pandas 面试题 20 道(上):概念与读代码
python·数据分析·pandas·数据处理
Dmz丶1 小时前
前端转 AI 100 天 Day 37:Few-shot 与 CoT——让模型学会“推理”
python
孟健1 小时前
我用Grok Bot本地剪口播视频,3段提示词搞定气口切除、字幕与封面
人工智能·python·aigc
2601_966949652 小时前
如何封装 qd.klines.batch(),让批量 K 线查询函数在多个策略中复用?
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
神仙别闹3 小时前
基于Python实现英词语自动对齐系统
开发语言·python
老歌老听老掉牙4 小时前
圆柱端面四平面交线及其在yoz面投影的几何解析与 Python 实现
开发语言·python·平面
我要见SA姐14 小时前
Chrome 扩展安全攻防实录:从权限滥用到供应链投毒
运维·人工智能·python·机器学习·自动化
2601_962885724 小时前
给 backtrader 接 A 股数据:用 AlphaFeed 喂PandasData
python