Skip to content

Instantly share code, notes, and snippets.

@venturqx
Created October 12, 2022 08:51
Show Gist options
  • Save venturqx/df2fda77cacea7b5334bb61258069f14 to your computer and use it in GitHub Desktop.
Save venturqx/df2fda77cacea7b5334bb61258069f14 to your computer and use it in GitHub Desktop.
from math import comb
import os
from os import walk
import shutil
import itertools
import datetime
import csv
import sys
### list files in path
def list_file(path):
f = []
for (dirpath, dirnames, filenames) in walk(path):
f.extend(filenames)
break
return f
### return list from csv
def csv2list(csvfile):
with open(csvfile, newline='') as f:
reader = csv.reader(f)
csvlist = list(reader)
n = len(csvlist)
L = []
for i in range(1,n):
L.append([datetime.datetime.strptime(csvlist[i][0], '%Y-%m-%d %H:%M:%S+00:00'),datetime.datetime.strptime(csvlist[i][1], '%Y-%m-%d %H:%M:%S+00:00')])
return L
### return list of events (without consecutive events) from list of events
def clear_gap(L):
if len(L) >= 2:
i=0
debut = L[0][0]
fin = L[0][1]
while L[i][1] == L[i+1][0]:
fin = L[i+1][1]
i += 1
return [[debut,fin]]+clear_gap(L[i+1:])
elif len(L) == 1:
return L
elif len(L) == 0:
return []
# return list of free events from list of events (without consecutive events)
def free(L,i,n):
if i >= n - 1:
return []
else:
return [[L[i][1],L[i+1][0]]] + free(L,i+1,n)
# return merged
def fusion(L1,L2,i,j,n1,n2):
if i >= n1 - 1 and j >= n1 - 1:
return []
elif i >= n1 - 1:
return [L2[j:]]
elif j >= n2 - 1:
return [L1[i:]]
else: # soit gap, soit overlap
start1, start2, stop1, stop2 = L1[i][0], L2[i][0], L1[i][1], L2[i][1]
if stop1<start2: # gap -_
return fusion(L1,L2,i+1,j,n1,n2)
elif stop2<start1: # gap _-
return fusion(L1,L2,i,j+1,n1,n2)
else: # overlap
if start1 > start2:
debut = start1
else:
debut = start2
if stop1 > stop2:
fin = stop2
return [[debut,fin]]+fusion(L1,L2,i,j+1,n1,n2)
else:
fin = stop1
return [[debut,fin]]+fusion(L1,L2,i+1,j,n1,n2)
def merge(L):
lenL=len(L)
if lenL == 0:
return []
elif lenL == 1:
return L
else:
return fusion(L[0],L[1],0,0,len(L[0]),len(L[1]))+merge(L[2:])
# return list of (list[i] =: (i-arrangment of item) in list)
def combo_newton(L,inf,sup):
combo = []
for i in range(inf,sup+1):
icombo=list(itertools.combinations(names,i))[0]
combo.append(icombo)
return combo
path = "/home/venturqx/Prog/Calelt/"
input_path = path + "ics/"
output_path = path + "csv/"
tmp_path = path + "tmp/"
tmp2_path = path + "tmp2/"
names = []
B = []
F = []
os.chdir(path)
input_files=list_file(input_path)
if not os.path.exists(tmp_path):
os.mkdir(tmp_path)
if not os.path.exists(tmp2_path):
os.mkdir(tmp2_path)
for file in input_files:
name=file.split(".")[-2]
names.append(name)
os.system("python ical2csv.py "+input_path+file)
path1=tmp_path+name+".csv"
os.rename(input_path+name+".csv",path1)
print("Moved to "+path1+"\n")
path2=tmp2_path+name+".csv"
os.system("csvcut -c \"Start Time\",\"End Time\" "+path1+" > "+path2)
print("csvcut "+name+".csv to "+path2+"\n")
F.append(free(clear_gap(csv2list(path2))))
merge(F)
# combo_newton(names,2,len(names))
### CALC
def free(jointed_list):
return 0
def merge(free0,free1):
return 0
def Lmerge(Lfree,fuse): # free =: [ [ [bla,bla],[ble,ble].. ], [ [bli,bli],[blo,blo].. ] ] ; fuse =: 0
lenf=len(Lfree)
if lenf == 0:
return []
if lenf == 1:
if fuse == 0:
merge(Lfree[0],Lfree[0])
fuse = 1
else:
return Lfree[0]
else:
return merge(Lfree[0],Lfree[1])+Lmerge(Lfree[2:])
# for icombo in combo:
# for arrangement in icombo:
# file3=name1+"_"+name2+".csv"
#M1=clear_gap(csv2list(input_path+"ELT4A_G1.ics"))
#M2=clear_gap(csv2list(input_path+"ELT4A_G2.ics"))
#print(M1)
# Mlen0=len(M[0])
# Mlen1=len(M[1])
# free=freed(M,0,0,Mlen0,Mlen1)
# with open(file3, 'w') as f:
# write = csv.writer(f)
# write.writerow(["Start Time", "End Time"])
# write.writerows(free)
### QUIT
for file in input_files:
if os.path.exists(tmp_path):
shutil.rmtree(tmp_path)
print("Deleting directory "+tmp_path+"\n")
if os.path.exists(tmp2_path):
shutil.rmtree(tmp2_path)
print("Deleting directory "+tmp2_path+"\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment