Skip to content

Commit f813cfa

Browse files
committed
add WebSpiderSeed/common
1 parent 3802ae5 commit f813cfa

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'''
2+
Author: herongwei
3+
Date: 2021-06-26 11:19:03
4+
LastEditTime: 2021-06-26 11:48:19
5+
LastEditors: Please set LastEditors
6+
Description: 通用操作帮助类
7+
FilePath: /WebSpiderSeed/lib/common_helper.py
8+
'''
9+
#!/usr/bin/spython
10+
# -*- coding: utf-8 -*-
11+
12+
import time
13+
import hashlib
14+
import socket
15+
from functools import lru_cache
16+
import math
17+
18+
class CommonHelper(object):
19+
@staticmethod
20+
def isNum(value):
21+
try:
22+
value + 1
23+
except TypeError:
24+
return False
25+
else:
26+
if math.isnan(value):
27+
return False
28+
return True
29+
30+
@staticmethod
31+
@lru_cache(1)
32+
def get_hostname():
33+
return socket.gethostname()
34+
35+
@staticmethod
36+
def get_time_millis():
37+
return int(round(time.time() * 1000))
38+
39+
@staticmethod
40+
def get_time_str():
41+
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
42+
43+
@staticmethod
44+
def get_time_str_num():
45+
return time.strftime("%Y%m%d%H%M", time.localtime(time.time()))
46+
47+
@staticmethod
48+
def encode(info):
49+
info = info.encode('utf-8')
50+
md5 = hashlib.md5(info)
51+
return md5.hexdigest()
52+
53+
@staticmethod
54+
def dict_remove_item(dict_obj, key):
55+
if key in dict_obj:
56+
del dict_obj[key]
57+
58+
@staticmethod
59+
def array_to_dict(arr, dict_key):
60+
dict_all = {}
61+
for item in arr:
62+
curr_key = item[dict_key]
63+
del item[dict_key]
64+
dict_all[curr_key] = item
65+
return dict_all
66+
67+
@staticmethod
68+
def arr_remove_item(arr, item):
69+
if item in arr:
70+
arr.remove(item)
71+
72+
@staticmethod
73+
def arr_append_item(arr, item):
74+
if item in arr:
75+
return
76+
arr.append(item)
77+
78+
@staticmethod
79+
def enum(**enums):
80+
return type('Enum', (), enums)
81+
82+
@staticmethod
83+
def sys_arg_to_dict(arr):
84+
dict_param = {}
85+
for i in range(0, len(arr), 2):
86+
dict_param[arr[i]] = arr[i + 1]
87+
return dict_param
88+
89+
# print(CommonHelper.get_hostname())
90+
# print(CommonHelper.get_hostname())
91+
# print(CommonHelper.encode('xuluhui:rgb-unlock:old-train-data:black-data:blacklist_attack_20180321_ls'))
92+
# print(CommonHelper.encode('aaaa'))
93+
# testenum = CommonHelper.enum(test1 = -1,test2 = 0,test3 = 1)
94+
# print(testenum.test1)

0 commit comments

Comments
 (0)