Python之xToolkit库

文章目录

  • 一、xToolkit是什么?
  • 二、准备工作
    • 1.引入库
    • 2.导入数据
  • 三、使用
    • 时间模块-xdatetime
      • 判断时间格式是否正确
      • get方法
        • 获取时间戳
        • 获取年月日时分秒
        • 时间推移计算
        • 时间替换
        • 时间扩展
          • 两个时间的差值
          • 开始与结束时间
          • 时间是否在指定区间中
    • 字符串模块-xstring
      • 字符串格式校验
      • 多种姿势的字符串处理
    • 多线程模块-xthread
    • 列表模块-xlist
      • 值的频率
    • 文件模块-xfile


一、xToolkit是什么?

xToolkit是python内置库的一个扩展库.把python的datetime,string,list,dist等数据结构重新进行了封装,扩展了部分功能.

库xToolkit的中文名字叫X工具集.是python内置库的一个扩展库.把python的datetime,string,list,dist等数据结构重新进行了封装,扩展了部分功能.
说明文档:https://www.showdoc.com.cn/xToolkit?page_id=2133295307421808

二、准备工作

1.引入库

代码如下(示例):

pip install xToolkit -i https://mirrors.aliyun.com/pypi/simple/

更新

升级方法
pip install --upgrade xToolkit -i https://mirrors.aliyun.com/pypi/simple/

2.导入数据

from xToolkit import xstring, xdatetime, xthreading, xlise, xfile

三、使用

时间模块-xdatetime

判断时间格式是否正确

支持判断的类型包括 date,datetime,time,int,float,str 其他类型默认为False

# 判断时间格式"1988-07-20"是否合规
xdatetime.shape("1988-07-20")
>>True

# 判断时间格式"1988-07-88"是否合规
xdatetime.shape("1988-07-88")
>>False

# 判断时间格式"98787987"是否合规
xdatetime.shape("98787987")
>>True

xdatetime.shape("2020-03-")
>> False

xdatetime.shape("english")
>> False

xdatetime.shape("258741")
>> True

xdatetime.shape("2020/03/20T10:09:06.252525+0800")
>> True

xdatetime.shape(datetime.datetime(2020, 9, 29, 8, 12))
>> True

xdatetime.shape(datetime.date(2020, 9, 29))
>> True

xdatetime.shape(datetime.time(8, 9, 29))
>> True

get方法

并且创建的方法还比较多,可以传入时间戳,时间字符串,datetime对象,date对象等

# 时间戳方法
xdatetime.get(98787987)
>> 1973-02-17T17:06:27+08:00


#字符串方式
xdatetime.get("1988-07-20")
>> 1988-07-20T00:00:00

# datetime对象
xdatetime.get((datetime(2020, 3, 23, 21, 56, 12))
>> 2020-03-23T21:56:12

# date对象等
xdatetime.get(date(2020, 3, 23))
>> 2020-03-23T00:00:00

获取时间戳
# 此方法获取的时间戳没有微妙部分,如果需要获取微妙部分,用time.time()
xdatetime.get().timestamp
>> 1585833834.0

xdatetime.get("2020-04-02 21:26:54").timestamp
>> 1585834014.0
获取年月日时分秒
# 年
xdatetime.get().year
>> 2020

# 月
xdatetime.get().month
>> 4

# 日
xdatetime.get().day
>> 2

# 时
xdatetime.get().hour
>> 21

# 分
xdatetime.get().minute
>> 37

# 秒
xdatetime.get().second
>> 48

# 微妙
xdatetime.get().microsecond
>> 70815


# 星期
xdatetime.get().weekday
# 返回数字 1-7代表周一到周日
>> 5

# 周
xdatetime.get().weed
# 返回整数代表,当前是本年第多少个周
>> 35

时间推移计算

shift方法获取某个时间之前或之后的时间,
关键字参数:years, months, days, hours,minutes,seconds,microseconds, weeks

# 一年以前
xdatetime.get().shift(years=-1)
>> 2019-04-03T21:10:49.095790+08:00

# 一年以后
xdatetime.get().shift(years=1)
>> 2021-04-03T21:10:49.095790+08:00

#一个月之后
xdatetime.get().shift(months=1)
>> 2020-05-03T21:12:17.332790+08:00

#一天以后
xdatetime.get().shift(days=1)
>> 2020-04-04T21:14:30.914443+08:00

#一个小时以后
xdatetime.get().shift(hours=1)
>> 2020-04-03T22:14:08.301192+08:00

#一分钟以后
xdatetime.get().shift(minutes=1)
>> 2020-04-03T21:17:27.956196+08:00

#一秒钟以后
xdatetime.get().shift(seconds=1)
>> 2020-04-03T21:16:45.380686+08:00

#一毫秒以后
xdatetime.get().shift(microseconds=1)
>> 2020-04-03T21:16:58.252929+08:00

#一周以后
xdatetime.get().shift(weeks=1)
>> 2020-04-10T21:17:11.827210+08:00


时间替换

替换datetime对象,年月日时分秒某一部分,返回一个被替换后的datetime对象,原对象不变
关键字参数:year, month, day, hour,minute,second,microsecond

# 把年替换会成2018
xdatetime.get().replace(year=2018)
>> 2018-04-03T21:23:42.819295+08:00

# 把月替换会成10
xdatetime.get().replace(month=10)
>> 2018-10-03T21:23:42.819295+08:00

# 把日替换会成7
xdatetime.get().replace(day=7)
>> 2018-04-07T21:23:42.819295+08:00

# 把时替换会成22
xdatetime.get().replace(hour=22)
>> 2018-04-03T22:23:42.819295+08:00

# 把分替换会成21
xdatetime.get().replace(minute=21)
>> 2018-04-03T21:21:42.819295+08:00

# 把秒替换会成21
xdatetime.get().replace(second=21)
>> 2018-04-03T21:23:21.819295+08:00

时间扩展
两个时间的差值

计算二个时间的差值,返回值为秒数
传入的二个时间格式包括,时间字符串,datetime,时间戳等

xdatetime.get("2020-04-28 10:52:52", "1988-07-20 17:31:12").how
>> 1002648100

xdatetime.get("2020-04-28", "1988-07-20 17:31:12").how
>> 1002608928

xdatetime.get("1975-04-28 14:14:55", "1988-07-20 17:31:12").how
>> -417496577

开始与结束时间

返回 指定时间中,年,月,周的开始时间和结束时间
类型genre Y->年,M->月,W->周
第一个参数:年
第二个参数:年月类型中,代表月,周类型代表周数

# 年
xdatetime.get(2020, 8, genre="Y").begin_end
>> ['2020-01-01', '2020-12-01']

xdatetime.get(2021, 5, genre="Y").begin_end
>> ['2021-01-01', '2021-12-01']

# 月
xdatetime.get(2020, 8, genre="M").begin_end
>> ['2020-08-01', '2020-08-31']

xdatetime.get(2021, 5, genre="M").begin_end
>> ['2021-05-01', '2021-05-31']

# 周
xdatetime.get(2020, 35, genre="W").begin_end
>> ['2020-08-24', '2020-08-30']

xdatetime.get(2021, 45, genre="W").begin_end
>> ['2021-11-08', '2021-11-14']

时间是否在指定区间中

计算时间是否在指定的时间区间内,返回值为bool型
需要传入二个参数,第一个为需要验证的字符串,第二个是一个时间列表,里面包含二个时间,开始时间和结束时间

xdatetime.get("2027-04-01", ["1988-04-14", "2020-05-14"]).middle
>> False
>
xdatetime.get("2020-04-15", ["2020-04-14", "2020-05-14 12:12:14"]).middle
>> True

字符串模块-xstring

导入

from xToolkit import xstring # 字符串模块

字符串格式校验

进行字符串格式效验,包括车牌格式,身份证号码,整形或浮点型,时间字符串,URL地址,手机号,银行卡,用户姓名,密码,邮箱。

# 车牌号
xstring.check("鄂A96288").is_car_number
>>True

# 身份证号码
# 提供中国大陆身份证验证,暂时只支持效验18位身份证
xstring.check("110101199003072316").is_identity_card
>>True

# 整形或浮点型
xstring.check("12.5").is_int_or_float
>>True


# 时间字符串
xstring.check("1988-07-20").is_datetime_string
>>True

# URL地址
xstring.check("https://wwww.baidu.com").is_url
>>True

# 手机号
xstring.check("15172383635").is_phone
>>True

# 银行卡
xstring.check("6222600260001072444").is_bank_number
>>True

# 用户姓名
# 姓名要求为2-4个中文
xstring.check("熊利宏").is_user_name
>>True

# 密码
# 包含6-18位字符,必须包含字母与数字,可以包含特殊字符
xstring.check("xlh123456").is_user_password
>>True

# 邮箱
# 第一种:只允许英文字母、数字、下划线、英文句号、以及中划线组成
# 第二种:名称允许汉字、字母、数字,域名只允许英文域名
xstring.check("[email protected]").is_mailbox
>>True

多种姿势的字符串处理

进行字符串处理,比如从身份证提取生日号码,性别等操作

xstring.dispose("11010119900307053X").get_identity_card(True)
>>{'code': '0000', 'msg': '身份证格式正确', 'data': {'birthday': '1990-03-07', 'gender': '男'}}

split 多标签分割

# 主要解决了系统模块split只能用一个分隔符
xstring.dispose("abc,我的-他的,1245*ss").split([",", "-", "*"])
>>['abc', '我的', '他的', '1245', 'ss']

主要解决了系统模块strip只过滤首尾空格

# 如果不传过滤参数,默认去掉所有空格
xstring.dispose("          鄂 A9 62 --8 8---__  ").strip()
>>鄂A962--88---__


xstring.dispose("          鄂 A9 62 --8 8---__  ").strip([" ", "-", "_"])
>> 鄂A96288

把字符串转换为emoji表情

emoji表情对应字符串:https://blog.csdn.net/wandugu/article/details/122102481

xstring.dispose('Python is :thumbs_up:').string_to_emoji()
>>Python is 

emoji表情转字符串

xstring.dispose('Python is ').emoji_to_string()
>>Python is :thumbs_up:

中文分词

# 分词对象 中国人民解放军海军工程大学

# 全模式:把文本中所有可能的词语都扫描出来,有冗余 cut_all=True
# ['中国', '中国人民解放军', '中国人民解放军海军', '国人', '人民', '人民解放军', '解放', '解放军', '海军', '海军工程大学', '军工', '工程', '大学']
# 精确模式:把文本精确的切分开,不存在冗余单词 cut_all=False
# ['中国人民解放军', '海军工程大学']
# 默认为精确模式

# 精确模式
xstring.dispose('中国人民解放军海军工程大学').part(cut_all=False)
>>['中国人民解放军', '海军工程大学']

# 全模式
xstring.dispose('中国人民解放军海军工程大学').part(cut_all=True)
>>['中国', '中国人民解放军', '中国人民解放军海军', '国人', '人民', '人民解放军', '解放', '解放军', '海军', '海军工程大学', '军工', '工程', '大学']

多线程模块-xthread

# 函数一
def function_1(a, b, c):
    time.sleep(1)
    return a * 2, b * 2, c * 2

# 函数二
def function_2(a, b):
    time.sleep(1)
    return a * 2, b * 2

# 函数三
def function_3(a):
    time.sleep(1)
    return a * 2

# 函数四
def function_4():
    time.sleep(1)
    return 0


st = time.time()
result = xthreading([function_1, 1, 1, 1], [function_2, 2, 2], [function_3, 2], [function_4])
print(result[0])
print(result[1])
print(result[2])
print(result[3])
et = time.time()
print("运行时间:{}".format(et - st))

>> (2, 2, 2)
>> (4, 4)
>> 4
>> 0
>> 运行时间:1.0010571479797363

# 从上面的运行时间可以看出,如果单线程执行应该是4秒以上,结果为1秒,说明运行时是多线程运行

列表模块-xlist

值的频率

from xToolkit import xlise # 列表模块
# 计算列表中值的频率
xlise.basics(["武汉", "武昌", "武汉", "无聊", "五菱", "武昌"]).values_count()
>>{'武昌': 2, '武汉': 2, '五菱': 1, '无聊': 1}

字典值替换

# 1.字典型列表的值整体替换
# 2.要求传入参数格式为 [{"id": None, "name": "wuhan"}, {"id": 5, "name": "中国"}, {"id": 25, "name": "上号"}, {"id": 5, "name": "测试"}]
# 3.这种形状的参数即可,比如可以传入 django 的 QuerySet 等
# 4.参数:
#    1.需要替换的对象
#    2.kwargs["rules"] 要求元祖,比如 ((None, ''), (45, 47)))

value = [{"id": None, "name": "wuhan"}, {"id": 5, "name": "中国"}, {"id": 25, "name": "上号"}, {"id": 5, "name": "测试"}]
xlise.basics(value).dict_to_value(rules=((None, ''), ("中国", "china")))
>>[{'id': '', 'name': 'wuhan'}, {'id': 5.0, 'name': 'china'}, {'id': 25.0, 'name': '上号'}, {'id': 5.0, 'name': '测试'}]

文件模块-xfile

导入:

from xToolkit import xfile

导入的excel表格的格式是这样的:
Python之xToolkit库_第1张图片

# excel转dict

# 1.传来的文件可以是文件路径,也可以是二进制文件
# 2.传来的可以是二进制文件,这里以django接收前端传来文件为例:
#     接收用 request.FILES.get("fileName", None) 传入 my_file 即可

# kwargs接收的参数有:
    # sheet索引,0代表第一个表,1代表第二个表,默认0
    # max表格最大的行数,默认2000行
    # min表格最小的行数,默认1行
    # title 表头(用于表头校验)

# 表头为选填,如果不填,不进行表头校验
xfile.read("./result/t_excel.xls").excel_to_dict()
>> [{'编号': 1, '时间': '1988-07-21 00:00:00', '年龄': 1, '分数': 63.2, '总分': 1},
    {'编号': 2, '时间': '1988-07-21 00:00:00', '年龄': 1, '分数': 63.2, '总分': 1},
    {'编号': 3, '时间': '1988-07-21 00:00:00', '年龄': 1, '分数': 63.2, '总分': 1},
    {'编号': 4, '时间': '1988-07-21 00:00:00', '年龄': 1, '分数': 63.2, '总分': 1},
    {'编号': 5, '时间': '1988-07-21 00:00:00', '年龄': 1, '分数': 63.2, '总分': 1},
    {'编号': 6, '时间': '1988-07-21 00:00:00', '年龄': 1, '分数': 63.2, '总分': 1}
    ]

校验表头再进行读取

Python之xToolkit库_第2张图片

xfile.read("./result/t_excel.xls").excel_to_dict(title=["编号", "时间", "年龄", "分数", "总分"])
>> {'code': '0001', 'data': {'data': None}, 'msg': '表头第 3 列错误,错误值为 年龄1 应该为 年龄'}

你可能感兴趣的:(python,开发语言)