def query_list_to_table(queries, maxqlen=120, output=False, normalise_numbers=True, **outputoptions): """Convert a django query list (list of dict with keys time and sql) into a table3 If output is non-False, output the table with the given options Specify print, "print", or a stream for output to be printed immediately """ time = collections.defaultdict(list) for q in queries: query = q["sql"] if normalise_numbers: query = re.sub(r"\d+", "#", query) # print(query) time[query].append(float(q["time"])) t = ObjectTable(rows=time.items()) t.add_column(lambda kv: len(kv[1]), "N") t.add_column(lambda kv: kv[0][:maxqlen], "Query") t.add_column(lambda kv: "%1.4f" % sum(kv[1]), "Cum.") t.add_column(lambda kv: "%1.4f" % (sum(kv[1]) / len(kv[1])), "Avg.") t = SortedTable(t, key=lambda row: row[2]) if output: if "stream" not in outputoptions and output is not True: if output in (print, "print"): import sys outputoptions["stream"] = sys.stdout else: outputoptions["stream"] = output t.output(**outputoptions) return t
def position_continuity(request): time = {} i = 0 positions = Position.objects.filter( date__gte="2010-06-01").order_by("date") while i < len(positions) - 1: if positions[i].club_1 == positions[i + 1].club_1: if positions[i].club_1 not in time: time[positions[i]. club_1] = positions[i + 1].date - positions[i].date else: time[positions[i]. club_1] += positions[i + 1].date - positions[i].date else: if positions[i].club_1 not in time: time[positions[i].club_1] = positions[ i + 1].date - positions[i].date - datetime.timedelta(days=1) else: time[positions[i].club_1] += positions[ i + 1].date - positions[i].date - datetime.timedelta(days=1) i += 1 time_sorted = sorted(time.items(), key=lambda kv: -kv[1]) return render(request, "EloMain/old/top_places.html", locals())
def get_ave(time, ave_time): for k, v in time.items(): length2 = len(v) sum = 0 #print("length2:%d" % length2) #print("v:%s" % v) #v = [int(x) for x in v] for a in v: sum = sum + int(a[0]) * 600 + int(a[1]) * 60 + int( a[2]) * 10 + int(a[3]) #print("sum:%d" % sum) ave = int(sum / length2) n1 = int(ave / 600) #print("n1:%d" % n1) s1 = ave % 600 #print("s1:%d" % s1) n2 = int(s1 / 60) #print("n2:%d" % n2) s2 = s1 % 60 #print("s2:%d" % s2) n3 = int(s2 / 10) #print("n3:%d" % n3) s3 = s2 % 10 #print("s3:%d" % s3) last = str(n1) + str(n2) + str(n3) + str(s3) #print("last:%s" % last) ave_time[k] = last
def postreqtimejson(time): for (k,v) in time.items(): tags = 'lua_reqt>1s:domain=' + k value = v counterType = 'GAUGE' outdictdata.append(simpleMetric(ip,metric,ts,step,value,counterType,tags)) return outreqtimedata
def query_list_to_table(queries, maxqlen=120, output=False, normalise_numbers=True, **outputoptions): """Convert a django query list (list of dict with keys time and sql) into a table3 If output is non-False, output the table with the given options Specify print, "print", or a stream for output to be printed immediately """ time = collections.defaultdict(list) for q in queries: query = q["sql"] if normalise_numbers: query = re.sub(r"\d+", "#", query) # print(query) time[query].append(float(q["time"])) t = ObjectTable(rows=time.items()) t.add_column(lambda kv: len(kv[1]), "N") t.add_column(lambda kv: kv[0][:maxqlen], "Query") t.add_column(lambda kv: "%1.4f" % sum(kv[1]), "Cum.") t.add_column(lambda kv: "%1.4f" % (sum(kv[1]) / len(kv[1])), "Avg.") t = SortedTable(t, key=lambda row: row[2]) if output: if "stream" not in outputoptions and output is not True: if output in (print, "print"): import sys outputoptions["stream"] = sys.stdout else: outputoptions["stream"] = output t.output(**outputoptions) return t
def get_continue_launch_count(strs, parm): time = strs.split(":") time = dict(Counter(time)) time = sorted(time.items(), key=lambda x: x[0], reverse=False) key_list = [] value_list = [] if len(time) == 1: return -2 for key, value in dict(time).items(): key_list.append(int(key)) value_list.append(int(value)) if np.mean(np.diff(key_list, 1)) == 1: if parm == '1': return np.mean(value_list) elif parm == '2': return np.max(value_list) elif parm == '3': return np.min(value_list) elif parm == '4': return np.sum(value_list) elif parm == '5': return np.std(value_list) else: return -1
def cur_day_repeat_count(strs): time = strs.split(":") time = dict(Counter(time)) time = sorted(time.items(), key=lambda x: x[1], reverse=False) # 一天一次启动 if (len(time) == 1) & (time[0][1] == 1): return 0 # 一天多次启动 elif (len(time) == 1) & (time[0][1] > 1): return 1 # 多天多次启动 elif (len(time) > 1) & (time[0][1] >= 2): return 2 else: return 3
def Duration(duration): time = re.compile('P' '(?:(?P<years>\d+)Y)?' '(?:(?P<months>\d+)M)?' '(?:(?P<weeks>\d+)W)?' '(?:(?P<days>\d+)D)?' '(?:T' '(?:(?P<hours>\d+)H)?' '(?:(?P<minutes>\d+)M)?' '(?:(?P<seconds>\d+)S)?' ')?').match(duration).groupdict() for key in time.items(): time[key[0]] = 0 if key[1] is None else time[key[0]] return (int(time["weeks"]) * 7 * 24 * 60 * 60) + (int( time["days"]) * 24 * 60 * 60) + (int(time["hours"]) * 60 * 60) + ( int(time["minutes"]) * 60) + (int(time["seconds"]) - 1)
def read_from_str(): from web_flask.caculate.fetchduration import fetch str_file = '/home/bf/Documents/Projects/helpplay/wait_time.data' write_file = 'origin_data/disney_data_2' with open(str_file,'r') as f: with open(write_file,'a') as fw: for line in f: # python解析json的时候,只能解析双引号,false,true,null,必须注意json字符串的格式。 line = line.replace("'",'"').replace("False","false").replace("True","true").replace("None","null") line_dict = json.loads(line) time = fetch.get_now_data(raw_json = line_dict) for key,value in time.items(): fw.write(key+','+str(value)+'\n')
def gettime_with_folders(): time = {} res = all_comp_folders_from_db() other_folders = [] for folder in res: all_files=get_all_files(folder) time[folder] = gettime(all_files) tmp = 0 result_folder = '' for item in time.items(): if item[1] > tmp: tmp = item[1] result_folder = item[0] for item in time.keys(): if item != result_folder: other_folders.append(item) return (result_folder, tmp, other_folders)
def calculate_time(time): ''' calculates average, maximum and minimum time elapsed. Sends all these data to generate_report function :param time: dictionary of commands as keys and elapsed time as values. ''' total_time = 0 for k, v in time.items(): print(k, v) total_time = total_time + v # calculate total time elapsed key_max = max(time.keys(), key=(lambda k: time[ k])) # find the command which took maximum amount of time to execute key_min = min(time.keys(), key=(lambda k: time[ k])) # find the command which took minimum amount of time to execute avg = total_time / 8 # Calculate average amount of time generate_report(time, key_max, key_min, avg, total_time)
def get_time(S, S2, squence): time = {} length = len(squence) #print("squence:%s" % squence) #print("length:%d" % length) for i in range(length): time[i] = [] #print("time:%s" % time) for s1 in S: #print("s1:%s" % s1) count = 0 temp_time = {} for s2 in s1: #print("s2:%s" % s2) #print("s2[0][0]:%s" % s2[0][0]) '''if s2[0][0] == squence[count][0]:####s2[0][0]解决原序列中有多个 temp_time[count]= s2[0][1:] count += 1''' for s3 in s2: if s3[0] == squence[count][0]: temp_time[count] = s3[1:] count += 1 #print("count:%d" % count) break if count == length: ###全部符合就记录 for k, v in temp_time.items(): time[k].append(v) #print("done") #print("time:%s" % time) break #print("temp_time:%s" % temp_time) for k, v in time.items(): time[k].sort() ##########排序 #print("time:%s" % time) #S2.write("time:%s\n" % time) ave_time = {} get_ave(time, ave_time) # print("ave_time:%s" % ave_time) npprefix.prefix2.append(ave_time) #S2.write("ave_time:%s\n" % ave_time) recommend_time(time, ave_time) #np4.5 #print("time:%s" % time) #S2.write("time:%s\n" % time) ###########################新时间 new_time = {} for i in range(len(time)): if (len(time[i]) >= 2): #print((time[i][0])) new_time[i] = [] new_time[i].append(time[i][0]) if (time[i][-1] != time[i][0]): new_time[i].append(time[i][-1]) elif (len(time[i]) == 1): new_time[i] = [] new_time[i].append(time[i][0][0] + time[i][0][1] + ':' + time[i][0][2] + time[i][0][3]) else: new_time[i] = ['无合适时间'] #############################新时间2 new_time2 = {} for i in range(len(new_time)): if (len(new_time[i]) == 2): new_time2[i] = [] new_time2[i].append(new_time[i][0][0] + new_time[i][0][1] + ':' + new_time[i][0][2] + new_time[i][0][3] + '-' + new_time[i][1][0] + new_time[i][1][1] + ':' + new_time[i][1][2] + new_time[i][1][3]) else: new_time2[i] = new_time[i] #new_time2[i] = [] #new_time2[i].append(new_time[i][0][0]+new_time[i][0][1]+':'+new_time[i][0][2]+new_time[i][0][3]) #print("推荐时间:%s" % new_time2) S2.write("推荐时间:%s\n" % new_time2) ################################### #print("____________________________________________") S2.write("____________________________________________\n")
def rest_overtime(contract, datetime_day): weekday = datetime_day.strftime("%w") if weekday == "0": weekday = str(6) else: weekday = str(int(weekday) - 1) res = 0 active = 0 date_day = datetime.strftime(datetime_day, DEFAULT_SERVER_DATE_FORMAT) day = datetime_day.strftime(DEFAULT_SERVER_DATE_FORMAT) holiday_ids = self.env['hr.holidays'].search([ ('state', '=', 'validate'), ('employee_id', '=', contract.employee_id.id), ('type', '=', 'remove'), ('date_from', '<=', day), ('date_to', '>=', day) ]) if not holiday_ids: sday = datetime.strftime(datetime_day, "%Y-%m-%d 00:00:00") eday = datetime.strftime(datetime_day, "%Y-%m-%d 23:59:59") attendance_ids = self.env['hr.attendance'].search( [('employee_id', '=', contract.employee_id.id), ('check_in', '>=', sday), ('check_out', '<=', eday)], order='check_in ASC') time = {} for work in contract.resource_calendar_id.attendance_ids: if work.dayofweek == weekday and ( work.date_from == False or work.date_from < date_day) and (work.date_to == False or work.date_to > date_day): if str(datetime_day + timedelta(hours=work.hour_from)) not in time: time[str(datetime_day + timedelta(hours=work.hour_from))] = { 'start': (datetime_day + timedelta(hours=work.hour_from)), 'end': (datetime_day + timedelta(hours=work.hour_to)) } if time: for attendance_id in attendance_ids: attend = attendance_id if attend.check_out: y = 0.0 now = datetime.strptime( attend.check_out, DEFAULT_SERVER_DATETIME_FORMAT) + timedelta( hours=3) for key, value in sorted(time.items()): start = value['start'] end = value['end'] for policy in contract.policy_id.line_ids: starttime = ( day + " " + str(policy.starttime) ).strftime(DEFAULT_SERVER_DATETIME_FORMAT) endtime = ( day + " " + str(policy.endtime) ).strftime(DEFAULT_SERVER_DATETIME_FORMAT) if policy.type == 'restday': active = policy.active_after overtime = True worked_hours = 0 if attend.check_in >= starttime and attend.check_in < start and attend.check_in <= endtime: worked_hours += ( start - attend.check_in).seconds / 60 if attend.check_out <= endtime and attend.check_out >= starttime and attend.check_out > end: worked_hours += (attend.check_out - end).seconds / 60 if worked_hours >= active: res += worked_hours return res
def execute(self, context): time = readFile(self.filepath, self.dump_textures) for k, v in time.items(): self.report({'INFO'}, 'Time for ' + k + ': ' + str(v) + ' seconds.') return {'FINISHED'}
def cvtDataForAPCA(sns_dict, disp_sns_num, trainig_range, tmp_sns_num, anomal_occur=0, anomal_disp=0): """ Args : sns_dict (dictionary) : disp_sns_num (string or list) : sensor num where displacement data to be imported training_range (int or float) : num of data to be used for training. Ininitial count starts from the first idx tmp_sns_num (string) : sensor num where temperature data to be imported anomal_occur (int or float) : datetime when anomaly occurs if not occur, it is set to zero. anomal_disp (int or float) : displacement amount to be shifted from the original displacement if it is set to None, the displacement will be fixed at anomal_occur Returns : op (dictionary) : Python data dictionary prepared for APCA """ disp_data_list = [] temp_data_list = [] ite = 0 for date, time in sns_dict[tmp_sns_num]['data'].items(): for time_, dir_ in time.items(): if ite > anomal_occur: anomal_disp_ = anomal_disp else: anomal_disp_ = 0 try: temp = dir_['temp'] except: temp = None try: disp_list = [] for idx, sns_num in enumerate(disp_sns_num): if sns_dict[sns_num]['disp_dir'] == 'Forward': disp_list.append(-( sns_dict[sns_num]['data'][date][time_]['x_disp'] + anomal_disp_)) else: disp_list.append( sns_dict[sns_num]['data'][date][time_]['x_disp'] + anomal_disp_) except: disp_list = None if temp and disp_list: ite += 1 temp_data_list.append(temp) disp_data_list.append(disp_list) op = {} op['t'] = np.array(temp_data_list, dtype=np.float32).squeeze()[:, np.newaxis].T op['f'] = np.array(disp_data_list, dtype=np.float32).squeeze() op['IND_x0'] = np.arange(0, trainig_range) op['IND_x1'] = np.arange(trainig_range, len(disp_data_list)) return op
def get_time_with_zeros(time): for key, value in time.items(): if value < 10: time[key] = '0' + str(value) return time