-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringutils.py
132 lines (109 loc) · 2.79 KB
/
stringutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__ = 'ziyan.yin'
import datetime
import string
def is_empty(arg):
"""
string is None or ''
>>> is_empty('a')
"""
if arg is None:
return True
if str(arg).strip() == '':
return True
return False
def is_number(arg: str):
"""
string is number like 1.0, -1
>>> is_number('1.5')
"""
if len(arg) > 1 and arg[0] == '0':
return False
if arg.startswith('-'):
arg = arg[1:]
if arg.isdigit():
return True
if arg.find('.') > 0:
args = arg.split('.')
if len(args) > 0:
if args[0].isdigit() and args[1].isdigit():
return True
return False
def is_digit(arg: str):
"""
isdigit() method
"""
return arg.isdigit()
def is_bool(arg: str):
"""
check if string is true or false
>>> is_bool('true')
"""
if arg in {'true', 'false'}:
return True
return False
def is_date(arg: str, base='%Y-%m-%d %H:%M:%S'):
"""
check if string is date-format
>>> is_date('2020-01-01 00:00:00')
"""
try:
datetime.datetime.strptime(arg, base)
return True
except TypeError:
return False
def is_chinese(arg: str):
"""
check if string is utf-8 chinese
>>> is_chinese('我')
"""
for ch in arg:
if u'\u4e00' <= ch <= u'\u9fa5':
return True
return False
def is_letter(arg: str):
"""
check if string is number or words
>>> is_letter('ab12123')
"""
for ch in arg:
if ch not in string.ascii_letters and ch not in string.digits:
return False
return True
def is_tag(arg: str):
"""
check if string is tag format
>>> is_tag('Abc_1234')
"""
for ch in arg:
if ch not in string.ascii_letters and ch not in string.digits and ch not in '_':
return False
return True
def is_label(arg: str):
"""
check if string is sql column format
>>> is_label('ab12123')
"""
if len(arg) > 0 and arg[0] in '0123456789':
return False
for ch in arg:
if ch not in string.ascii_letters and ch not in string.digits and ch not in '_':
return False
return True
def is_legal(arg: str):
"""
check if string has illegal word
>>> is_legal('ab12123')
"""
illegal_signal = '~|'
for ch in arg:
if ch in illegal_signal:
return False
return True
def words_standard(arg: str):
temp = arg
for ch in arg:
if ch not in string.ascii_letters and ch not in string.digits and not (u'\u4e00' <= ch <= u'\u9fa5'):
temp = temp.replace(ch, ' ')
return temp.lower()