-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKhadang.py
138 lines (118 loc) · 3.48 KB
/
Khadang.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
133
134
135
136
137
138
import datetime
import sys
import numbers
import socket
from StaticsBase import *
def getIpByHost(address):
ip=UNIN
try:
ip=socket.gethostbyname(address)
print(address,ip)
except Exception as e:
print(e)
return ip
def isLive(address):
return not getIpByHost(address)=='127.0.0.1'
def is_valid_ipv4_address(address):
try:
socket.inet_pton(socket.AF_INET, address)
except AttributeError: # no inet_pton here, sorry
try:
socket.inet_aton(address)
except socket.error:
return False
return address.count('.') == 3
except socket.error: # not a valid address
return False
return True
def is_valid_ipv6_address(address):
try:
socket.inet_pton(socket.AF_INET6, address)
except socket.error: # not a valid address
return False
return True
def isValidHostStr(hostStr):
if is_valid_ipv4_address(hostStr):return True
if is_valid_ipv6_address(hostStr):return True
return False
### Math Helpers
def ffloat(f):
return float(f.replace(',', ''))
def mySQLTypeGen(element):
# print(element)
if isinstance(element, numbers.Number):
return 'float'
else:
return 'VARCHAR(256)'
def mySQLTypedFormat(e):
if isinstance(e, numbers.Number):
return e
elif e=='*':
return '{}'.format('*')
else:
return '`{}`'.format(e)
"""
Random Text helper
"""
""" t$ for timestamp"""
def berin(n=1,sign='-',indent=0,v=1):
indented=''
if indent>0:
indented = berin(n=indent, sign=' ', indent=0, v=0)
else:
if sign=='t$':
sign=datetime.datetime.now()
ret= ''
for i in range(n):
ret= '{} {}'.format(sign , ret)
ret= '{}{}'.format(indented, ret)
if v:
print(ret)
return ret
###########
def braces(boundBy):
boundByL=''
boundByR=''
if not boundBy == '':
if boundBy == '(' or boundBy == ')':
boundByL = '('
boundByR = ')'
elif boundBy == '{' or boundBy == '}':
boundByL = '{'
boundByR = '}'
elif boundBy == '{' or boundBy == '}':
boundByL = '{'
boundByR = '}'
elif boundBy == '<' or boundBy == '>':
boundByL = '<'
boundByR = '>'
elif boundBy == '[' or boundBy == ']':
boundByL = '['
boundByR = ']'
else:
boundByL=boundBy
boundByR=boundBy
return boundByL,boundByR
def embrace(content,boundBy,boundBy2=UNIN):
ret = '{boundByL}{content}{boundByR}'
if boundBy2==unIn:
boundByL, boundByR=braces(boundBy)
else:
boundByL, boundByR = boundBy,boundBy2
ret = ret.format(boundByL=boundByL, boundByR=boundByR, content=content)
return ret
def virgool(inList,quotation='',sym=',',boundBy='',sql=0):
inlist2=[]
if sql==1:
for i in inList:
inlist2.append(mySQLTypedFormat(i))
inList=inlist2
boundByL, boundByR = braces(boundBy)
ret=''
for item in inList:
ret='{ret}{sym}{quotation}{item}{quotation}'.format(ret=ret,sym=sym,quotation=quotation,item=item)
ret='{boundByL}{ret}{boundByR}'.format(boundByL=boundByL,ret=ret[1:],boundByR=boundByR)
return ret
def sexyError(e):
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
return 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e