fish_common 基本函数包

fish_common.conf_as_dict(conf_filename) 读入 ini 配置文件,返回根据配置文件内容生成的字典类型变量;
fish_common.get_uuid(kind) 获得不重复的 uuid,可以是包含时间戳的 uuid,也可以是完全随机的;基于 Python 的 uuid 类进行封装和扩展;
fish_common.GetMD5.string(s) 获取一个字符串的MD5值
fish_common.GetMD5.file(filename) 获取一个文件的 MD5 值
fish_common.GetMD5.big_file(filename) 获取一个大文件的 MD5 值
fish_common.if_json_contain(left_json, …) 判断一个 json 是否包含另外一个 json 的 key,并且 value 相等;
fish_common.SingleTon 申明一个单例类,可以作为需要单例类时候申明用的父类
fish_common.sorted_list_from_dict(p_dict[, …]) 根据字典的 value 进行排序,并以列表形式返回
fish_common.splice_url_params(dic) 根据传入的键值对,拼接 url 后面 ? 的参数,比如 ?key1=value1&key2=value2

fish_common 包含的是最常用用的一些函数和类。

class fish_common.GetMD5

计算普通字符串和一般的文件,对于大文件采取逐步读入的方式,也可以快速计算;基于 Python 的 hashlib.md5() 进行封装和扩展;

举例如下:

print('--- md5 demo ---')
print('string md5:', GetMD5.string('hello world!'))
print('file md5:', GetMD5.file(get_abs_filename_with_sub_path('test_conf', 'test_conf.ini')[1]))
print('big file md5:', GetMD5.big_file(get_abs_filename_with_sub_path('test_conf', 'test_conf.ini')[1]))
print('---')

执行结果:

string md5: fc3ff98e8c6a0d3087d515c0473f8677
file md5: fb7528c9778b2377e30b0f7e4c26fef0
big file md5: fb7528c9778b2377e30b0f7e4c26fef0
static big_file(filename)

获取一个大文件的 MD5 值

Param:
  • (string) filename 需要进行 hash 的大文件路径
Returns:

  • (string) result 32位小写 MD5 值

static file(filename)

获取一个文件的 MD5 值

Param:
  • (string) filename 需要进行 hash 的文件名
Returns:

  • (string) result 32位小写 MD5 值

static string(s)

获取一个字符串的MD5值

Param:
  • (string) str 需要进行 hash 的字符串
Returns:

  • (string) result 32位小写 MD5 值

class fish_common.SingleTon

申明一个单例类,可以作为需要单例类时候申明用的父类

Param:
Returns:

举例如下:

print('--- class singleton demo ---')
t1 = SingleTon()
t1.x = 2
print('t1.x:', t1.x)

t2 = SingleTon()

t1.x += 1

print('t1.x:', t1.x)
print('t2.x:', t2.x)
print('---')

执行结果:

--- class singleton demo ---
t1.x: 2
t1.x: 3
t2.x: 3
---
fish_common.conf_as_dict(conf_filename)

读入 ini 配置文件,返回根据配置文件内容生成的字典类型变量;

Param:
  • conf_filename: (string) 需要读入的 ini 配置文件长文件名
Returns:

  • flag: (bool) 读取配置文件是否正确,正确返回 True,错误返回 False
  • d: (dict) 如果读取配置文件正确返回的包含配置文件内容的字典
  • count: (int) 读取到的配置文件有多少个 key 的数量

举例如下:

print('--- conf_as_dict demo---')
# 定义配置文件名
conf_filename = 'test_conf.ini'
# 读取配置文件
ds = conf_as_dict(conf_filename)
# 显示是否成功,所有 dict 的内容,dict 的 key 数量
print('flag:', ds[0])
print('dict:', ds[1])
print('length:', ds[2])

d = ds[1]

# 显示一个 section 下的所有内容
print('section show_opt:', d['show_opt'])
# 显示一个 section 下面的 key 的 value 内容
print('section show_opt, key short_opt:', d['show_opt']['short_opt'])

# 读取一个复杂的section,先读出 key 中的 count 内容,再遍历每个 key 的 value
i = int(d['get_extra_rules']['erule_count'])
print('section get_extra_rules, key erule_count:', i)
for j in range(i):
    print('section get_extra_rules, key erule_type:', d['get_extra_rules']['erule_'+str(j)])
print('---')

执行结果:

--- conf_as_dict demo---
flag: True
dict: (omit)
length: 7
section show_opt: {'short_opt': 'b:d:v:p:f:', 'long_opt': 'region=,prov=,mer_id=,mer_short_name=,web_status='}
section show_opt, key short_opt: b:d:v:p:f:
section get_extra_rules, key erule_count: 2
section get_extra_rules, key erule_type: extra_rule_1
section get_extra_rules, key erule_type: extra_rule_2
---
fish_common.get_uuid(kind)

获得不重复的 uuid,可以是包含时间戳的 uuid,也可以是完全随机的;基于 Python 的 uuid 类进行封装和扩展;

支持 get_time_uuid() 这样的写法,不需要参数,也可以表示生成包含时间戳的 uuid,兼容 v1.0.12 以及之前版本;

Param:
  • kind: (int) uuid 类型,整形常量 udTime 表示基于时间戳, udRandom 表示完全随机
Returns:

  • result: (string) 返回类似 66b438e3-200d-4fe3-8c9e-2bc431bb3000 的 uuid

举例如下:

print('--- uuid demo ---')
# 获得带时间戳的uuid
for i in range(2):
    print(get_uuid(udTime))

print('---')

# 时间戳 uuid 的简单写法,兼容之前版本
for i in range(2):
    print(get_time_uuid())

print('---')

# 获得随机的uuid
for i in range(2):
    print(get_uuid(udRandom))

print('---')

执行结果:

--- uuid demo ---
c8aa92cc-60ef-11e8-aa87-acbf52d15413
c8ab7194-60ef-11e8-b7bd-acbf52d15413
---
c8ab7368-60ef-11e8-996c-acbf52d15413
c8ab741e-60ef-11e8-959d-acbf52d15413
---
8e108777-26a1-42d6-9c4c-a0c029423eb0
8175a81a-f346-46af-9659-077ad52e3e8f
---
fish_common.if_json_contain(left_json, right_json, op='strict')

判断一个 json 是否包含另外一个 json 的 key,并且 value 相等;

Param:
  • left_json: (dict) 需要判断的 json,我们称之为 left
  • right_json: (dict) 需要判断的 json,我们称之为 right,目前是判断 left 是否包含在 right 中
  • op: (string) 判断操作符,目前只有一种,默认为 strict,向后兼容
Returns:

  • result: (bool) right json 包含 left json 的 key,并且 value 一样,返回 True,否则都返回 False

举例如下:

print('--- json contain demo ---')
json1 = {"id": "0001"}
json2 = {"id": "0001", "value": "File"}
print(if_json_contain(json1, json2))
print('---')

执行结果:

True
fish_common.sorted_list_from_dict(p_dict, order=10011)

根据字典的 value 进行排序,并以列表形式返回

Param:
  • p_dict: (dict) 需要排序的字典
  • order: (int) 排序规则,odASC 升序,odDES 降序,默认为升序
Returns:

  • o_list: (list) 排序后的 list

举例如下:

# 定义待处理字典
dict1 = {'a_key': 'a_value', '1_key': '1_value', 'A_key': 'A_value', 'z_key': 'z_value'}
print(dict1)
# 升序结果
list1 = sorted_list_from_dict(dict1, odASC)
print('ascending order result is:', list1)
# 降序结果
list1 = sorted_list_from_dict(dict1, odDES)
print('descending order result is:', list1)

执行结果:

{'a_key': 'a_value', 'A_key': 'A_value', '1_key': '1_value', 'z_key': 'z_value'}
ascending order result is: ['1_value', 'A_value', 'a_value', 'z_value']
descending order result is: ['z_value', 'a_value', 'A_value', '1_value']
fish_common.splice_url_params(dic)

根据传入的键值对,拼接 url 后面 ? 的参数,比如 ?key1=value1&key2=value2

Param:
  • dic: (dict) 参数键值对
Returns:

  • result: (string) 拼接好的参数

举例如下:

dic1 = {'key1': 'value1', 'key2': 'value2'}
print(splice_url_params(dic1))

执行结果:

?key1=value1&key2=value2