Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with sys.get_frame
#1
This is the code:

def date_parser(date):
    while True:
       try:
            checked_date = datetime.strptime(date,'%d %b %Y')
            caller = sys._getframe(1).f_code.co_name
            check = input(f"You have entered {checked_date}. Is this correct? y/n ")
            if check == 'y':
                print(caller)
                if caller == 'birth_date' or 'date_parser':
                    if checked_date <= datetime.today():
                        return checked_date

                    else:
                        z = input("Cannot have date after today. Please re-enter date. ")
                        return date_parser(z)
                        break
                else:
                     return checked_date
....

def birth_date(date):
   return date_parser(date)
def expiry_date(date):
   return date_parser(date)

birth_date = birth_date("16 May 2023")
expiry_date = expiry_date("26 Jun 2022")
When the code is run, birth_date correctly runs the else loop (can't enter a birth date in the future) but expiry_date also runs the else loop. I (obviously) need the code to accept an expiry date into the future.

The reason I decided to use the sys.get_frame method to check the caller is because the remaining branch of the def_parser function is quite long and it seems very repetitive to create two separate ,long, and almost identical, functions just because the caller is different.

What have I missed here?

Thanks in advance
Reply
#2
It's better and much easier to help if you could post code that we can run.
All imports,no cut of code and with Traceback or output that's wrong.
On line 9 you have a Common pitfall.
Reply
#3
Thanks for the reply. It was line 9 after all!

I will remember to post a functional code next time.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020