-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneralfunctions.py
75 lines (60 loc) · 1.68 KB
/
generalfunctions.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
#!/usr/bin/env python3
import logging
import os
import requests
import json
import pathlib
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from dateutil import parser as DP
def log(path, message, isError, isDebug):
try:
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", handlers=[logging.FileHandler(path)])
if isError:
logging.error(message)
else:
if isDebug:
logging.debug(message)
else:
logging.info(message)
except Exception as e:
logging.error(str(e))
#print(e)
def create_directory(path):
if not os.path.isdir(path):
os.makedirs(path)
def read_json(path, log_path):
try:
json_text = ''
with open(path, 'r', encoding="utf-8-sig") as openfile:
text = openfile.read()
openfile.close()
# strip BOM character
json_text = text.lstrip('\ufeff')
json_text = json.loads(text)
except Exception as e:
log(log_path, str(e), True, False)
print(e)
return json_text
def read_file(path):
with open(path, "r") as infile:
text = infile.read()
infile.close()
text = text.strip('\n')
return text
def now():
date = datetime.now()
return date
def format_dateYYYMMDDHHMM(date):
format = "%Y%m%d%H%M"
formatted = date.strftime(format)
return formatted
def format_dateYYYMMDDHHMMSS(date):
format = "%Y%m%d%H%M%S"
formatted = date.strftime(format)
return formatted
def to_boolean(text):
status = False
if text.lower() == "true":
status = True
return status