-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
68 lines (56 loc) · 1.43 KB
/
utils.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
import sys, os, time, socket
import logging
import tzlocal
def getAppPath():
return os.path.dirname(os.path.realpath(__file__)) + os.sep
def getFileSize(filePath):
try:
return os.path.getsize(filePath)
except:
return 0
def getFilePath(filePath):
path, file = os.path.split(filePath)
return path
def getFileName(filePath):
path, file = os.path.split(filePath)
return file
def isFileExist(filePath):
return os.path.isfile(filePath)
def isRaspberryPi():
return os.name != "nt" and os.uname()[0] == "Linux"
def isWindows():
return os.name == "nt"
def getIPAddress():
try:
if isWindows():
hostname = socket.gethostname()
return socket.gethostbyname(hostname)
else:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
except:
return "-"
def getCPULoad():
try:
return int(psutil.cpu_percent())
except:
return 0
def getRAMUsage():
try:
return int(psutil.virtual_memory().percent)
except:
return 0
def dateAsLocalTZ(dt):
try:
tzName = tzlocal.get_localzone()
return dt.astimezone(tzName)
except BaseException as e:
return dt
def dateAsString(dt, format):
try:
tzName = tzlocal.get_localzone()
tmWithTz = dt.astimezone(tzName)
return tmWithTz.strftime(format)
except BaseException as e:
return str(e)