How Do I Keep Total Time with Multiple Duplicate Entries? - python

I will attach my code, but basically I am importing a csv file with start times/end times for picking cases of a particular item. All the cases go to a "cart", which is identified by an ID number. I want to find the total time to pick all the cases. The format of the time is hh:mm:ss and, initially, I was using the datetime module but I could not figure out the documentation, so I ended up just converting all the times to seconds, subtracting end/start for each case, and adding that duration to the total time. In the end, converted total time to hours. Already had number of cases picked total, and divided by total time in hrs to get cases picked per hr. Is this correct logic? I got a number that was very, very low: 7.99 cases/hr, which leads me to believe my timing/duration code is incorrect (already checked that quantity was correct).
#instantiate totalTime to zero
totalTime = 0
#every line/row in file; assume already opened above
for line in lines:
#if there is a different case to pick, find the start time
if taskId != entryList[0]: #this is so it doesnt duplicate times
timestart = entryList[7]
colonStartIndex = timestart.find(":")
hourstart = int(timestart[0:colonStartIndex])
minutestart = int(timestart[colonStartIndex+1:colonStartIndex+3])
colonStartIndex2 = timestart.find(":", colonStartIndex+1)
secondstart = int(timestart[colonStartIndex2 +1:colonStartIndex2 +3])
start = hourstart*3600 + minutestart*60 + secondstart
#start = datetime(year=1, month=1, day=1,hour=hourstart,minute=minutestart,second=secondstart)
#start = datetime.time(start)
timeend = entryList[9]
colonEndIndex = timeend.find(":")
hourend = int(timeend[0:colonEndIndex])
minuteend = int(timeend[colonEndIndex+1:colonEndIndex+3])
colonEndIndex2 = timeend.find(":", colonEndIndex+1)
secondend = int(timeend[colonEndIndex2+1:colonEndIndex2+3])
end = hourend*3600 + minuteend*60 + secondend
#end = datetime(year=1,month=1,day=1,hour=hourend,minute=minuteend,second=secondend)
#end = datetime.time(end)
#duration = datetime.combine(date.today(), end) - datetime.combine(date.today(), start)
duration = end - start
if duration >= 0:
duration = duration
elif duration < 0:
duration = -1*duration
totalTime = totalTime + duration
taskId = entryList[0] #first entry in csv file of each line is cartID
totalTime = totalTime/3600
print(totalTime)
print(quantityCount)
avgNumCases = quantityCount/totalTime
print(avgNumCases)
Thank you so much for any help!! Also, I included the datetime stuff, commented out, so if you could suggest a solution based on that, I am open to it:) I was just frustrated because I spent a good bit of time trying to figure it out, but I'm not super familiar w it and the documentation is pretty hard to understand (esp b/c datetime objects, blah blah)

There is an obvious problem in this section:
duration = end - start
if duration >= 0:
duration = duration
elif duration < 0:
duration = -1*duration
If your start point is 22:00:00 and end point is 21:00:00 your duration will be 1 hour instead of 23 hours.

Related

How to find the difference between two times

I'm trying to figure out a way to take two times from the same day and figure out the difference between them. So far shown in the code below I have converted both of the given times into Int Vars and split the strings to retrieve the information. This works well but when the clock in values minute is higher than the clock out value it proceeds to give a negative value in minute slot of the output.
My current code is:
from datetime import datetime
now = datetime.now()
clocked_in = now.strftime("%H:%M")
clocked_out = '18:10'
def calc_total_hours(clockedin, clockedout):
in_hh, in_mm = map(int, clockedin.split(':'))
out_hh, out_mm = map(int, clockedout.split(':'))
hours = out_hh - in_hh
mins = out_mm - in_mm
return f"{hours}:{mins}"
print(calc_total_hours(clocked_in, clocked_out))
if the clocked in value is 12:30 and the clocked out value is 18:10
the output is:
6:-20
the output needs to be converted back into a stand time format when everything is done H:M:S
Thanks for you assistance and sorry for the lack of quality code. Im still learning! :D
First, in order to fix your code, you need to convert both time to minutes, compute the difference and then convert it back to hours and minutes:
clocked_in = '12:30'
clocked_out = '18:10'
def calc_total_hours(clockedin, clockedout):
in_hh, in_mm = map(int, clockedin.split(':'))
out_hh, out_mm = map(int, clockedout.split(':'))
diff = (in_hh * 60 + in_mm) - (out_hh * 60 + out_mm)
hours, mins = divmod(abs(diff) ,60)
return f"{hours}:{mins}"
print(calc_total_hours(clocked_in, clocked_out))
# 5: 40
Better way to implement the time difference:
import time
import datetime
t1 = datetime.datetime.now()
time.sleep(5)
t2 = datetime.datetime.now()
diff = t2 - t1
print(str(diff))
Output:
#h:mm:ss
0:00:05.013823
Probably the most reliable way is to represent the times a datetime objects, and then take one from the other which will give you a timedelta.
from datetime import datetime
clock_in = datetime.now()
clock_out = clock_in.replace(hour=18, minute=10)
seconds_diff = abs((clock_out - clock_in).total_seconds())
hours, minutes = seconds_diff // 3600, (seconds_diff // 60) % 60
print(f"{hours}:{minutes}")

why is not the result 00:00:XX?

i expected like 00:00:0X but 09:00:0X came out how can i do to make 00:00:0X
import time
start = input("Enter를 누르면 타이머를 시작합니다.")
begin = time.time()
while True:
time.sleep(1)
count = time.time()
result = time.localtime(count - begin)
print(count - begin)
print(time.strftime('%I:%M:%S', result))
result:
1.0102884769439697
09:00:01
2.0233511924743652
09:00:02
3.0368154048919678
time.time() will give you the number of seconds since 1.1.1970 in UTC.
So begin is a huge number and count will also be a huge number + about 1. Subtracting those will give about 1.
If you pass this to time.time() you'll get 1.1.1970 plus 1 second. Converting to local time (time.localtime()) will give you whatever timezone offset you are. Obviously +9 hours.
What you probably wanted is time.gmtime() and output in 24 hour format. This will work...
import time
start = input("Enter를 누르면 타이머를 시작합니다.")
begin = time.time()
while True:
time.sleep(1)
count = time.time()
result = time.gmtime(count - begin)
print(count - begin)
print(time.strftime('%H:%M:%S', result))
but it is semantically incorrect. If you subtract 2 dates, the result is a timespan, not a date. What is the difference?
If someone asks, how old you are, you have a look at the current year and you subtract the year of your birth. Then you say "I'm 25 years old". You don't add 1.1.1970 and say "I'm 1995 years old".
So the following is semantically much better:
import time
from datetime import timedelta
start = input("Enter를 누르면 타이머를 시작합니다.")
begin = time.time()
while True:
time.sleep(1)
count = time.time()
timespan = timedelta(seconds=count - begin)
print(timespan)
It shows 09:00:00 because you're in the UTC+9 timezone. For example, I'm in UTC+1 (France) and it shows 01:00:00 for me. Therefore, your code will have different outputs depending on where you run it.
To remove this timezone constraint, simply use datetime.timedelta:
begin = time.time()
while True:
time.sleep(1)
count = time.time()
print(datetime.timedelta(seconds=round(count - begin)))
Output:
0:00:01
0:00:02
0:00:03
0:00:04
0:00:05

How can I iterate through list data faster using multiprocessing?

I'm trying to determine the amount of time worked by a list of employees during their shift - this data is given to me in the form of a CSV file.
I populate a matrix with this data and iterate through it using a while loop applying the necessary conditionals (for example, deducting 30 minute for lunch). This is then put into a new list, which is used to make an Excel worksheet.
My script does what it is meant to do, but takes a very long time when having to loop through a lot of data (it needs to loop through approximately 26 000 rows).
My idea is to use multiprocessing to do the following three loops in parallel:
Convert the time from hh:mm:ss to minutes.
Loop through and apply conditionals.
Round values and convert back to hours, so that this is not done within the big while loop.
Is this a good idea?
If so, how would I have the loops run in parallel when I need data from one loop to be used in the next? My first thought is to use the time function to give a delay, but then I'm concerned that my loops may "catch up" with one another and spit out that the list index being called does not exist.
Any more experienced opinions would be amazing, thanks!
My script:
import pandas as pd
# Function: To round down the time to the next lowest ten minutes --> 77 = 70 ; 32 = 30:
def floor_time(n, decimals=0):
multiplier = 10 ** decimals
return int(n * multiplier) / multiplier
# Function: Get data from excel spreadsheet:
def get_data():
df = pd.read_csv('/Users/Chadd/Desktop/dd.csv', sep = ',')
list_of_rows = [list(row) for row in df.values]
data = []
i = 0
while i < len(list_of_rows):
data.append(list_of_rows[i][0].split(';'))
data[i].pop()
i += 1
return data
# Function: Convert time index in data to 24 hour scale:
def get_time(time_data):
return int(time_data.split(':')[0])*60 + int(time_data.split(':')[1])
# Function: Loop through data in CSV applying conditionals:
def get_time_worked():
i = 0 # Looping through entry data
j = 1 # Looping through departure data
list_of_times = []
while j < len(get_data()):
start_time = get_time(get_data()[i][3])
end_time = get_time(get_data()[j][3])
# Morning shift - start time < end time
if start_time < end_time:
time_worked = end_time - start_time # end time - start time (minutes)
# Need to deduct 15 minutes if late:
if start_time > 6*60: # Late
time_worked = time_worked - 15
# Need to set the start time to 06:00:00:
if start_time < 6*60: # Early
time_worked = end_time - 6*60
# Afternoon shift - start time > end time
elif start_time > end_time:
time_worked = 24*60 - start_time + end_time # 24*60 - start time + end time (minutes)
# Need to deduct 15 minutes if late:
if start_time > 18*60: # Late
time_worked = time_worked - 15
# Need to set the start time to 18:00:00:
if start_time > 18*60: # Early
time_worked = 24*60 - 18*60 + end_time
# If time worked exceeds 5 hours, deduct 30 minutes for lunch:
if time_worked >= 5*60:
time_worked = time_worked - 30
# Set max time worked to 11.5 hours:
if time_worked > 11.5*60:
time_worked = 11.5*60
list_of_times.append([get_data()[i][1], get_data()[i][2], round(floor_time(time_worked, decimals = -1)/60, 2)])
i += 2
j += 2
return list_of_times
# Save the data into Excel worksheet:
def save_data():
file_heading = '{} to {}'.format(get_data()[0][2], get_data()[len(get_data())-1][2])
file_heading_2 = file_heading.replace('/', '_')
df = pd.DataFrame(get_time_worked())
writer = pd.ExcelWriter('/Users/Chadd/Desktop/{}.xlsx'.format(file_heading_2), engine='xlsxwriter')
df.to_excel(writer, sheet_name='Hours Worked', index=False)
writer.save()
save_data()
You can look at multiprocessing.Pool which allows executing a function multiple times with different input variables. From the docs
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))
Then, it's a matter of splitting up your data into chunks (instead of the [1, 2, 3] in the example).
But, my personal preference, is to take the time and learn something that is distributed by default. Such as Spark and pyspark. It'll help you in the long run immensely.

Output text when between two datetime values

I am making a program that determines what period in school you are in; the schedules differ on certain days so you can just run the program instead of finding out what schedule you are on.
I am using the 'DateTime' import; but the problem is certain classes may start at let's say 7:45 and end at 9:50. The way I am programming the two times contradict each-other so the text wont be displayed.
here is the snippet of code:
if sch == "A":
if hour >= 7 and min >= 45:
if hour <= 9 and min <= 50:
print("It is period 1; Class ends at 9:50AM")
It was determined previously in the program that we are on schedule 'A' which starts at 7:45AM and ends at 9:50AM. In short I want it to display the message whilst in between those two times.
Use datetime for this task:
from datetime import datetime
begin = '07:45:00'
end = '09:50:00' # for example
current_time = '10:32:13'
FMT = '%H:%M:%S' # format time
if sch == "A":
if datetime.strptime(begin, FMT) < \
datetime.strptime(current_time, FMT) < \
datetime.strptime(end, FMT):
print("It is period 1; Class ends at 9:50AM")
you can try this:
from datetime import time
if sch == "A":
if time(hour=7, minute=45) <= time(hour=hour, minute=minute) <= time(hour=9, minute=45):
print("It is period 1; Class ends at 9:50AM")
As I commented your logic is off because you don't allow for minutes other than anything in the 45-50 range, your are using datetimes so stick to comparing datetimes.time's seeing if the hour and minute are within the range 7:45-9:50.
from datetime import time
# cretate a start and end time
start, end = time(7, 45, 0), time(9, 50, 0)
# pass whatever hour and min are in your code to time
hour_min = time(7, 46)
# check the times falls in the range 7:45-9:50
if start <= hour_min <= end:
print("It is period 1; Class ends at 9:50AM")

How to add 24 to the %H part of the enter variable, then store it as the same variable? [duplicate]

This question already has answers here:
What is the standard way to add N seconds to datetime.time in Python?
(11 answers)
Closed 6 years ago.
from datetime import datetime
run = 1
list1 = []
for i in range(2):
while run > 0:
print("")
reg_num = input('reg num')
enter = input('time enter in the form "HH:MM:SS" in 24 hours format')
ext = input('time exit in the form "HH:MM:SS" in 24 hours format')
total_time = '%H:%M:%S'
if int(enter[0:1]) > int(ext[0:1]):
total_time = enter[0:1] + 24
t_diff = datetime.strptime(ext, total_time)- datetime.strptime(enter, total_time)
t_diff1 = str(t_diff)
print(t_diff)
t_diff2 = int(t_diff1[4:5])
t_diff3 = int(t_diff1[7:8])
time = ((t_diff2/60)+(t_diff3/60/60))
speed = run/time
print("{:.2f}".format(speed), "mph")
if speed > 70:
list1.append(reg_num)
break
for item in list1:
print(item, "is over speeding")
This is code to work out the speed of something as it enters and exits a checkpoint that is set a mile apart.
What I am trying to do here is to find the difference in time when the time entered hour is greater than time exited hour. i.e. enter = 23:59:00 and ext = 00:00:00. I know that I have to add 24 to the %H but I don’t know how to do it and when I try to run it in the shell the message is:
total_time = enter[0:1] + 24
TypeError: Can't convert 'int' object to str implicitly
The problem I think comes from this part of the code:
if int(enter[0:1]) > int(ext[0:1]):
total_time = enter[0:1] + 24
Can you help me find out a way to add 24 to the hour section and then store it into the same variable.
In the end you want the speed. This approach calculates it.
The best is to use datetime.datetime objects for all time calculations.
Therefore, convert the strings first.
You can use datetime.timedelta(hours=24) to create a timedelta object.
You can add this to a datetime.datetime object:
from __future__ import division # if you still work with Python 2
import datetime
run = 1
enter = '23:59:20'
ext = '00:00:00'
time_format = '%H:%M:%S'
enter_time = datetime.datetime.strptime(enter, time_format)
ext_time = datetime.datetime.strptime(ext, time_format)
if enter_time > ext_time:
ext_time += datetime.timedelta(hours=24)
t_diff = ext_time - enter_time
elapsed_time = t_diff.total_seconds() / 3600
speed = run / elapsed_time
print(speed)
Output:
90.0

Categories

Resources