github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/server/block/util/convert.py (about) 1 # coding:utf-8 2 3 from datetime import datetime, date 4 from datetime import timedelta 5 import re 6 import time 7 import struct 8 from urlparse import urlparse 9 from types import GeneratorType, NoneType 10 import collections 11 import six 12 13 14 TS_UNITS = ('year', 'month', 'day', 'hour', 'minute', 'sec') 15 _reg_val_code_pat = re.compile(r'^[\d]{8}$') 16 _num_pat = re.compile(r'^-?[a-f\d]+?$', re.I) 17 _email_pat = re.compile(r'^(?P<name>[a-zA-Z\d][_\.a-zA-Z\d]*)@(?P<domain>[a-zA-Z\d.]+\.[a-z]{2,4})$') 18 _uuid_pat = re.compile(r'^[a-f\d]{32}$') 19 _sha1_pat = re.compile(r'^[a-f\d]{40}$') 20 # 能用到203x年已经太牛叉了 21 _date_pat = re.compile( 22 r'^(?P<year>[12]\d{3})-(?P<month>0[1-9]|1[012])-(?P<day>\d{1,2})(?:T(?P<hour>[01][0-9]|2[0-3]):(?P<minute>[0-5][0-9]):(?P<sec>[0-5][0-9]))?$') 23 _date_pat_s = re.compile( 24 r'^(?P<year>[12]\d{3})-(?P<month>0[1-9]|1[012])-(?P<day>\d{1,2})(?:\s+(?P<hour>[01][0-9]|2[0-3]):(?P<minute>[0-5][0-9]):(?P<sec>[0-5][0-9]))?$') 25 _time_pat = re.compile(r'^(?P<hour>[01][0-9]|2[0-3]):(?P<minute>[0-5][0-9]):(?P<sec>[0-5][0-9])$') 26 _float_pat = re.compile(r'^-?\d+(\.\d+)?$') 27 _mac_pat = re.compile(r'^[a-f\d]{2}(?::[a-f\d]{2}){5}$', re.I) 28 _gsm_tid_pattern = re.compile(r'^[\d]{10,13}$', re.I) 29 _cdma_tid_pattern = re.compile(r'^[\da-f]{20}$', re.I) 30 _imei_pat = re.compile(r'^\d{15}$') 31 _imsi_pat = re.compile(r'^\d{15}$') 32 _ip_pat = re.compile(r'^(?:(?:\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$') 33 _port_pat = re.compile(r'^\d{2,5}$') 34 _ip_port_pat = re.compile( 35 ''.join(('^', '(?P<ip>', _ip_pat.pattern[1:-1], '|[-_\da-z.]+):(?P<port>', _port_pat.pattern[1:-1], ')$'))) 36 _cn_mobile_pat = re.compile(r'^\d{9,13}$') 37 _cn_mobile_cdma = re.compile(r'^(?:(?:(?:133|153|177|18[019])\d{1})|1700|173\d)\d{7}$') #电信 38 _user_name = re.compile(r'^[a-zA-Z][a-zA-Z\d\-_]{5,11}$') # 用户名 39 _password = re.compile(r'^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?![,\.#%\'\+\*\-:;^_`]+$)[,\.#%\'\+\*\-:;^_`0-9A-Za-z]{8,20}$') 40 _merchant_name = re.compile(r'^[A-Za-z0-9\u4e00-\u9fa5]{3,50}$') 41 42 43 def is_reg_val_code(s): 44 """ 45 帐号注册验证码 46 """ 47 if not s or not isinstance(s, str): 48 return False 49 return _reg_val_code_pat.search(s) 50 51 52 def is_mobile(s): 53 if not s or not isinstance(s, str): 54 return False 55 return _cn_mobile_pat.search(s) 56 57 58 def is_mobile_cdma(s): 59 """ 60 电信手机号码 61 :param s: 62 :return: 63 ^133\d{8}$ 64 ^153\d{8}$ 65 ^177\d{8}$ 66 ^18[0|1|9]\d{8}$ 67 ^1700\d{7}$ 68 """ 69 if not s or not isinstance(s,str): 70 return False 71 return _cn_mobile_cdma.search(s) 72 73 74 75 def is_gsm_tid(tid): 76 """移动终端号码""" 77 if not tid or not isinstance(tid, str): 78 return False 79 return _gsm_tid_pattern.search(tid) 80 81 82 def is_cdma_tid(tid): 83 """电信终端号码""" 84 if not tid or not isinstance(tid, str): 85 return False 86 return _cdma_tid_pattern.search(tid) 87 88 89 def is_time(v): 90 if not v or not isinstance(v, str): 91 return False 92 return _time_pat.search(v) 93 94 95 def is_date(v): 96 """ 97 20140424(T12:34:56) 98 year, month, day, hour, minute, sec 99 """ 100 if not v or not isinstance(v, str): 101 return False 102 m = _date_pat_s.search(v) 103 if not m: 104 return False 105 106 month = int(m.groupdict().get('month')) 107 day = int(m.groupdict().get('day')) 108 if 2 == month and day > 29: 109 return False 110 return m 111 112 def is_password(v): 113 """ 114 密码为8~20位数字,英文,符号至少两种组合的字符 115 :param v: 116 :return: 117 """ 118 if not v or not isinstance(v, str): 119 return False 120 return _password.search(v) 121 122 def is_float(v): 123 if not v or not isinstance(v, str): 124 return False 125 return _float_pat.search(v) 126 127 128 def is_num(v): 129 if not v or not isinstance(v, str): 130 return False 131 return _num_pat.search(v) 132 133 134 def is_email(v): 135 if not v or not isinstance(v, basestring): 136 return False 137 return _email_pat.search(v) 138 139 def is_user_name(v): 140 if not v or not isinstance(v, basestring): 141 return False 142 return _user_name.search(v) 143 144 145 def is_uuidhex(v): 146 if not v or not isinstance(v, str): 147 return False 148 return _uuid_pat.search(v) 149 150 151 def is_even(n): 152 """ 153 偶数判定 154 """ 155 return 0 == n % 2 156 157 158 def bs2unicode(bs): 159 """ 160 basestring转换为python unicode类型 161 """ 162 if not isinstance(bs, basestring): 163 return bs 164 if isinstance(bs, unicode): 165 return bs 166 return bs.decode('utf-8') 167 168 169 def bs2utf8(bs): 170 if isinstance(bs, basestring): 171 return bs.encode('utf-8') if isinstance(bs, unicode) else bs 172 return bs 173 174 175 def to_list(x, default=None): 176 if x is None: 177 return default 178 if not isinstance(x, collections.Iterable) or \ 179 isinstance(x, six.string_types): 180 return [x] 181 elif isinstance(x, list): 182 return x 183 else: 184 return list(x) 185 186