Yes, it works like a charm. On the tupility of it all.
Special thanks for the explanation too…..

 

(Originally asked but I found the errors. All is working)

Now that the code no longer produces the errors, I see that the year and month 
not included in the calculation? How do I fix this?

 

From: anthony.flury <anthony.fl...@btinternet.com> 
Sent: Thursday, December 15, 2022 1:47 PM
To: Gronicus@SGA.Ninja
Subject: RE: Subtracting dates to get hours and minutes

 

What is likely happening is that when you read the data from the file you are 
not reading a tuple, you are reading a 26 charcter string.

You have to convert that string into a tuple - the easiest way will be somthing 
like this : 

 

timet = tuple(int(x.strip()) for x in timestring[1:-1].split(','))

 

where timestring is the data you get from the file

The [1:-1] removes the () from the data

The .split(",") method creates a temporary list from the remaining string 
breaking the string where there are commas

The x.strip() removes spaces from each item in the temporary list.

 

Note that the * unpack operator doesn't just unpack tuples, it works on an 
iterable, so when you read the data from the file currently, and then use * on 
it, it will pass 26 separate characters to the function.

 



------ Original Message ------
From: Gronicus@SGA.Ninja <mailto:Gronicus@SGA.Ninja> 
To: "'Thomas Passin'" <li...@tompassin.net <mailto:li...@tompassin.net> >; 
python-list@python.org <mailto:python-list@python.org> 
Sent: Thursday, 15 Dec, 22 At 18:14
Subject: RE: Subtracting dates to get hours and minutes

So far so good , I can now use a variable in datetime.datetime but it only
works if I hard-code the time/date information. Now I want to have the code
read from a file but I get: TypeError: function takes at most 9 arguments
(26 given)

I figure that the structure in the file is incorrect. What should it be? The
entry in the file is (2022, 12, 13, 5, 3, 30) but when my program tries to
use it I get the error.

The program is a bit more sophisticated now but here is the update with a
sample of the SPECIFICATIONS.txt file:
=====================================================================

# This program compares two Timedate values, subtracts the two and
# converts the difference to seconds and hours.
#

# %A Monday # %a Mon # %B January # %b Jan
# %d 05 day # %m month as 01 # %Y 2020 # %y 20
# %H 24 # %I 12 # %M 30 min # %S Seconds

import time
import datetime
from time import gmtime, strftime ##define strftime as time/date right now
# ======================================================

def GetSpecByItem(GetThisOne): #get line by item in column 4 - 7
ItemValue = "--"

with open("SPECIFICATIONS.txt" , 'r') as infile:
for lineEQN in infile: # loop to find each line in the file for that
dose
if ((lineEQN[4:7]== GetThisOne)):
ItemValue = lineEQN[30:60].strip() # Just the Data
return(ItemValue)

"""
SPECIFICATIONS.txt

IYf HRB Humalog R Date (2018, 12, 4, 10, 7, 00) ##
IYf HRG Humulin R Date (2022, 12, 13, 5, 3, 30) ##
"""
# ====================== Main() ======================================
print()
Startt = "404"
Stopp = "404"

Answer = "Y"
Answer = input("Run test A? (" + Answer + ")" )

if Answer == "Y" or Answer == "y" or Answer == "":
print()
print(" Running Test A:")
# Year Mth Day Hour Min Sec
Startt = 2018, 12, 4, 10, 7, 00 
Stopp = 2022, 12, 12, 1, 15, 30
NowTime = 2022, 12, 14, 21, 15, 30
else:
print(" Running Test B:")
Startt = GetSpecByItem("HRG")
Stopp = GetSpecByItem("HRB")
NowTime = strftime("(%Y, %m, %d, %H, %M, %S)")

print()
print("55 NowTime = " + str(NowTime))
print("56 Startt = " + str(Startt))
print("57 Stopp = " + str(Stopp))
print()

NowTime = datetime.datetime(*NowTime)
Startt = datetime.datetime(*Startt)
Stopp = datetime.datetime(*Stopp)

#Start == Startt # True" 
#print("Startt test = " + Start)
# =================================================
print()
c = NowTime - Stopp 
minutes = c.total_seconds() / 60 
minutes = c.seconds / 60
hours = 0

while (minutes > 59):
minutes = minutes - 60
hours += 1
minutes = round(minutes)
print ("77 Hours = <" + str(hours) + ">")
print ("78 Minutes = <" + str(minutes) + ">")
if hours > 7:
print(" Time to inject Humulin R u500.")

pause = input("Pause") 
# ======================================================


-----Original Message-----
From: Python-list <python-list-bounces+gronicus=sga.ni...@python.org 
<mailto:python-list-bounces+gronicus=sga.ni...@python.org> > On
Behalf Of Thomas Passin
Sent: Tuesday, December 13, 2022 11:20 PM
To: python-list@python.org <mailto:python-list@python.org> 
Subject: Re: Subtracting dates to get hours and minutes

Your problem is that datetime.datetime does not accept a tuple as an
argument. It expects an integer value for the first argument, but you
supplied a tuple. In Python, you can use a sequence (e.g., tuple or
list) the way you want by prefixing it with an asterisk. This causes the
sequence of items to be treated as individual arguments. So:

Startt = datetime.datetime(2022, 12, 13, 5, 3, 30)
st1 = (2022, 12, 13, 5, 3, 30)
dts1 = datetime.datetime(*st1) # NOT datetime.datetime(st1)
dts1 == Startt # True

On 12/13/2022 10:43 PM, Gronicus@SGA.Ninja <mailto:Gronicus@SGA.Ninja>  wrote:

                        
As is, Test A works.
Comment out Test A and uncomment Test B it fails.
In Test B, I move the data into a variable resulting with the report:
"TypeError: an integer is required (got type tuple)

How do I fix this?

#---------------------------------------------------------------------
--------
import datetime
#=================================================
# Test A Hard coded Date/Time
Startt = datetime.datetime(2022, 12, 13, 5, 3, 30) Stopp = 
datetime.datetime(2022, 12, 12, 21, 15, 30)

# =================================================
# Test B Date/Time data as a variable
#Startt = (2022, 12, 13, 5, 3, 30)
#Stopp = (2022, 12, 12, 21, 15, 30)

#Startt = datetime.datetime(Startt)
#Stopp = datetime.datetime(Stopp)

# =================================================
c = Startt - Stopp
minutes = c.total_seconds() / 60
minutes = c.seconds / 60
hours = 0

while (minutes > 59):
minutes = minutes - 60
hours += 1
minutes = round(minutes)
print()
print (" Hours = <" + str(hours) + ">")
print (" Minutes = <" + str(minutes) + ">")

# 
----------------------------------------------------------------------
-------


--
https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to