63 lines
2.4 KiB
Python
63 lines
2.4 KiB
Python
import importlib
|
|
from threading import Thread, Lock
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
from loader import dmt
|
|
|
|
|
|
def calc_intervals(posts):
|
|
firstpost = dmt(posts).reduce(lambda acc, e: acc if acc < e['CreationDate'] else e['CreationDate'], lambda acc, e: acc if acc < e else e, lambda: posts[0]['CreationDate'],
|
|
"firstpost").getresults()
|
|
lastpost = dmt(posts).reduce(lambda acc, e: acc if acc > e['CreationDate'] else e['CreationDate'], lambda acc, e: acc if acc > e else e, lambda: posts[0]['CreationDate'], "lastpost").getresults()
|
|
|
|
# calc quarter beginning
|
|
firstpost = firstpost.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
|
if firstpost.month not in (1, 4, 7, 10):
|
|
firstpost = firstpost.replace(month={1: 1, 2: 1, 3: 1, 4: 4, 5: 4, 6: 4, 7: 7, 8: 7, 9: 7, 10: 10, 11: 10, 12: 10}[firstpost.month])
|
|
lastpost = lastpost.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
|
if lastpost.month not in (1, 4, 7, 10):
|
|
lastpost = lastpost.replace(month={1: 1, 2: 1, 3: 1, 4: 4, 5: 4, 6: 4, 7: 7, 8: 7, 9: 7, 10: 10, 11: 10, 12: 10}[lastpost.month])
|
|
# add 3 months to last post
|
|
if lastpost.month == 10:
|
|
lastpost = lastpost.replace(month=1, year=lastpost.year + 1)
|
|
else:
|
|
lastpost = lastpost.replace(month=lastpost.month + 3)
|
|
|
|
cdate = firstpost
|
|
intervals = []
|
|
while cdate < lastpost:
|
|
nextquarter = cdate.replace(month=(cdate.month + 3) % 12, year=cdate.year + (0 if cdate.month + 3 < 12 else 1))
|
|
print("adding interval: " + cdate.strftime("%d-%m-%Y") + " - " + nextquarter.strftime("%d-%m-%Y"))
|
|
intervals.append((cdate, nextquarter))
|
|
cdate = nextquarter
|
|
return intervals
|
|
|
|
|
|
def imprt(file):
|
|
spec = importlib.util.spec_from_file_location("module.name", file)
|
|
foo = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(foo)
|
|
return foo
|
|
|
|
|
|
class FigSaver():
|
|
def __init__(self):
|
|
self.__lock = Lock()
|
|
self.__threads = []
|
|
|
|
def save(self, fig, path, **kwargs):
|
|
thread = Thread(target=self.__dosave, args=(fig, path, kwargs))
|
|
with self.__lock:
|
|
self.__threads.append(thread)
|
|
thread.start()
|
|
|
|
def __dosave(self, fig, path, kwargs):
|
|
fig.savefig(path, **kwargs)
|
|
plt.close(fig)
|
|
|
|
def join(self):
|
|
with self.__lock:
|
|
for thread in self.__threads:
|
|
thread.join()
|