utils_string.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. """
  4. 字符处理的工具类
  5. """
  6. import hashlib
  7. import random
  8. import string
  9. # 错误信息重定向
  10. from typing import Iterable
  11. from apilib.utils_sys import PY3
  12. def cn(_): return unicode(_).encode('utf-8')
  13. def get_random_string(length = 8):
  14. src_digits = string.digits
  15. src_uppercase = string.ascii_uppercase
  16. src_lowercase = string.ascii_lowercase
  17. digits_num = random.randint(1, 6)
  18. uppercase_num = random.randint(1, length - digits_num - 1)
  19. lowercase_num = length - (digits_num + uppercase_num)
  20. random_string = random.sample(src_digits, digits_num) + random.sample(src_uppercase, uppercase_num) + random.sample(
  21. src_lowercase, lowercase_num)
  22. random.shuffle(random_string)
  23. return ''.join(random_string)
  24. def get_random_str(num, seq=string.ascii_letters):
  25. ret_list = []
  26. for i in range(1, num + 1):
  27. ret_list.append("".join(random.sample(seq, 1)))
  28. return "".join(ret_list)
  29. def generate_random_seq(size = 32):
  30. char = string.ascii_letters + string.digits
  31. return "".join(random.choice(char) for _ in range(size))
  32. if PY3:
  33. unicode_type = str
  34. basestring_type = (str, bytes)
  35. else:
  36. unicode_type = unicode
  37. basestring_type = basestring
  38. def encode(s):
  39. return s.encode('utf-8') if isinstance(s, unicode_type) else s
  40. def decode(s):
  41. return s.decode('utf-8') if isinstance(s, bytes) else s
  42. def md5(_): return hashlib.md5(_).hexdigest()
  43. # TODO: 暂时放这儿吧
  44. def encrypt_display(str): return '******'
  45. def split_str(s, startIndex=0, lens=None, toInt=False):
  46. """
  47. 分割字符串的函数,用于将协议返还的串口数据分割成指定的长度
  48. ex:
  49. s = "660000FFFF00A1"
  50. startIndex = 2
  51. lens = "4422"
  52. 返还 ["0000", "FFFF", "00", "A1"]
  53. :param s: 被分割的字符串
  54. :param startIndex: 被分割起始位置
  55. :param lens: 要被分割的长度
  56. :param toInt: 分割后是否转为 int 数值
  57. :return: list
  58. """
  59. if lens is None:
  60. lens = list()
  61. if not isinstance(lens, Iterable):
  62. raise TypeError("indexes mast be Iterable")
  63. result = list()
  64. for _len in lens:
  65. endIndex = int(startIndex) + int(_len)
  66. tempS = s[startIndex: endIndex]
  67. if toInt:
  68. tempS = int(tempS, 16)
  69. result.append(tempS)
  70. startIndex = endIndex
  71. return result
  72. def make_title_from_dict(valueDictList):
  73. result = ''
  74. keys = []
  75. for kd in valueDictList:
  76. keys.extend(kd.keys())
  77. maxLen = 0
  78. for k in keys:
  79. if len(k) > maxLen:
  80. maxLen = len(k)
  81. for valueDict in valueDictList:
  82. for k,v in valueDict.items():
  83. if k == '':
  84. result += u'{}\\n'.format(v)
  85. else:
  86. needTab = 2 + (maxLen - len(k))/2
  87. tabs = ''
  88. for ii in range(needTab):
  89. tabs += '\\t'
  90. result += '\\n\\n%s:%s%s' % (k,tabs,v)
  91. result += '\\n'
  92. return result
  93. def make_qr_code(url, logoUrl = None):
  94. from io import BytesIO
  95. import qrcode
  96. img = qrcode.make(url)
  97. bytesIo = BytesIO()
  98. img.save(bytesIo, format = 'PNG')
  99. import base64
  100. return base64.b64encode(bytesIo.getvalue())