time_recorder_format_main.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from tool_lib.read_table import Read_Table
  2. from fast_excel_to_bill.transform_excel_data_to_bill_data.table_config import TABLE_PATH
  3. def add_sum_to_dict(work_time_dict, income_per_hour):
  4. for key in work_time_dict.keys():
  5. work_time_dict[key]['Betrag'] = int(work_time_dict[key]['Arbeitszeit']*income_per_hour)
  6. return work_time_dict
  7. def round_worktime_for_week(work_time_dict, round_time = 300):
  8. for key in work_time_dict.keys():
  9. over_time = work_time_dict[key]['Arbeitszeit'] % round_time # default round to 5 hours
  10. work_time_dict[key]['Arbeitszeit'] = (work_time_dict[key]['Arbeitszeit']-over_time)/ 60
  11. return work_time_dict, over_time
  12. def get_task_out_of_tree(work_time_dict):
  13. for key in work_time_dict.keys():
  14. work_time_dict[key]['Task'] = ['- ' + last_branch[last_branch.rfind('/')+1:] for last_branch in work_time_dict[key]['Task']]
  15. return work_time_dict
  16. def compress_data_by_key_list(*compressable_lists, key_list):
  17. '''
  18. function: a list containing different Values, if a Value is more than one time
  19. in the list. Then this list an all list of the same size are compressed, for the values of the
  20. list_of_same_size they are put in tuple.
  21. '''
  22. if len(compressable_lists) > 1:
  23. for list_ in compressable_lists:
  24. past_values = []
  25. pos_bias = 0
  26. for pos, value in enumerate(key_list):
  27. if value in past_values:
  28. pos = pos - pos_bias
  29. if isinstance(list_[pos - pos_bias], int):
  30. list_[pos - 1] += list_[pos]
  31. elif isinstance(list_[pos - pos_bias], str):
  32. list_[pos - 1] = [list_[pos - 1], list_[pos]]
  33. elif isinstance(list_[pos - pos_bias], list):
  34. list_[pos - 1] = list_[pos - 1].append(list_[pos])
  35. list_.remove(list_[pos])
  36. pos_bias += 1
  37. else:
  38. past_values += [value]
  39. else:
  40. compressable_lists = compressable_lists[0]
  41. if len(compressable_lists) > len(key_list):
  42. compressable_lists = compressable_lists[:len(key_list)]
  43. elif len(compressable_lists) < len(key_list):
  44. assert (False, 'Compressable list length should be greater equal then the length of the key_list')
  45. past_values = []
  46. pos_bias = 0
  47. for pos, value in enumerate(key_list):
  48. if value in past_values:
  49. pos = pos - pos_bias
  50. if isinstance(compressable_lists[pos - 1], int):
  51. compressable_lists[pos - 1] += compressable_lists[pos]
  52. elif isinstance(compressable_lists[pos - 1], str):
  53. compressable_lists[pos - 1] = [compressable_lists[pos - 1], compressable_lists[pos]]
  54. elif isinstance(compressable_lists[pos - 1], list):
  55. compressable_lists[pos - 1] += [compressable_lists[pos]]
  56. compressable_lists.remove(compressable_lists[pos])
  57. pos_bias += 1
  58. else:
  59. past_values += [value]
  60. return compressable_lists
  61. def main_data_collection(pathes, list_of_keys=['Arbeitszeit', 'Task']):
  62. week_keys_to_list_of_keys_dict = {}
  63. if isinstance(pathes, list):
  64. for c, path in enumerate(pathes):
  65. # Read Data
  66. read_table_object = Read_Table(path)
  67. table_object = read_table_object.table_to_dict()
  68. # Calendar Week
  69. calendar_weeks_list = table_object['Kalenderwoche']
  70. # Add new Weeks to dict
  71. for week in list(set(calendar_weeks_list)):
  72. if week in week_keys_to_list_of_keys_dict:
  73. pass
  74. else:
  75. week_keys_to_list_of_keys_dict[week] = {}
  76. # Get variable key dict with values of table
  77. key_dict = {}
  78. for key in list_of_keys:
  79. key_dict[key] = table_object[key]
  80. # Compress Data by Calendar week
  81. for key in key_dict.keys():
  82. all_weeks = compress_data_by_key_list(key_dict[key], key_list=calendar_weeks_list)
  83. for week_indx, week in enumerate(list(set(calendar_weeks_list))):
  84. if c == 0:
  85. week_keys_to_list_of_keys_dict[week][key] = all_weeks[week_indx]
  86. else:
  87. if isinstance(all_weeks[week_indx], int):
  88. week_keys_to_list_of_keys_dict[week][key] += all_weeks[week_indx]
  89. elif isinstance(all_weeks[week_indx], list):
  90. week_keys_to_list_of_keys_dict[week][key] += all_weeks[week_indx] # Change to Append if needed
  91. else:
  92. # Read Data
  93. read_table_object = Read_Table(pathes)
  94. table_object = read_table_object.table_to_dict()
  95. # Calendar Week
  96. calendar_weeks_list = table_object['Kalenderwoche']
  97. # Add new Weeks to dict
  98. for week in list(set(calendar_weeks_list)):
  99. if week in week_keys_to_list_of_keys_dict:
  100. pass
  101. else:
  102. week_keys_to_list_of_keys_dict[week] = {}
  103. # Get variable key dict with values of table
  104. key_dict = {}
  105. for key in list_of_keys:
  106. key_dict[key] = table_object[key]
  107. # Compress Data by Calendar week
  108. compressed_lists = []
  109. for key in key_dict.keys():
  110. all_weeks = compress_data_by_key_list(key_dict[key], key_list=calendar_weeks_list)
  111. for week_indx, week in enumerate(list(set(calendar_weeks_list))):
  112. week_keys_to_list_of_keys_dict[week][key] = all_weeks[week_indx]
  113. return week_keys_to_list_of_keys_dict
  114. if __name__ == '__main__':
  115. table_object = main_data_collection([TABLE_PATH, TABLE_PATH])