On 2022-12-15 18:14, Gronicus@SGA.Ninja wrote:
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)):

You don't need the parentheses, and certainly 2 pairs of them!

            ItemValue = lineEQN[30:60].strip()   # Just the Data
     return(ItemValue)

You're returning a _string_.

I suggest using 'literal_eval' from the 'ast' module to convert the string safely into a tuple.

However, if the 'for' loop fails to match a line, the function will return "--", which won't be of any use later on unless you check for it specifically and, say, report an error to the user.


"""
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

'Startt' and 'Stopp' here are tuples.

else:
    print("     Running Test B:")
    Startt = GetSpecByItem("HRG")
    Stopp =  GetSpecByItem("HRB")

'Startt' and 'Stopp' here are _strings_.

    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)

These will work if 'Startt' and 'Stopp' are tuples, but not if they're strings. In the latter case, you're effectively passing multiple single-characters strings into 'datetime'.

#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> On
Behalf Of Thomas Passin
Sent: Tuesday, December 13, 2022 11:20 PM
To: 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 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

Reply via email to