Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Retry After Exception
#1
Hey! Friends I coded a program with a external module called tenacity
Problem: I want to retry After Exception
Here Is The Code:
from tenacity import *
from ast import literal_eval
import time
print("Welcome To Decimal To Hex Or Binary Converter")
def line():
    for i in range(50):
        print("-",end='')
print("\nPlease Type The 'h' for hex input")
line()
print("\nPlease Type The 'd' for decimal input")
line()
print("\nPlease Type The 'b' for binary input")
line()
temp = input("\nType 'h' or 'd' or 'b':")
line()
time.sleep(1)
print(f"\nSo You type '{temp}'")
time.sleep(0.1)
@retry(stop=stop_after_attempt(2),retry=retry_any())
def starting():
    for i in temp:
        if i == 'h':
            try:
                time.sleep(1)
                hexa = input("\nPlease Type The Hex Input To Convert Into Deciaml And Binary:")
                time.sleep(1)
                print(f"In The Decimal Form: %s" %(literal_eval(hexa)))
                print(f"In The Binary Form: %s" %(bin(int(hexa,16))))
                time.sleep(5)
            except ValueError:
                print("Only Hex Numbers!")
                print("Hex Numbers Look Like 0x1,0x3, etc.")
                time.sleep(5)
                line()
        elif i == 'd':
            line()
            time.sleep(1)
            print("\nEntering Decimal Mode")
            time.sleep(1)
            line()
            time.sleep(1)
            deci = input("\nPlease Type The Decimal Input To Convert Into Hex And Binary:")
            try:
                time.sleep(1)
                print(f"In The Hexadecimal Form: %s" %(int(deci)))
                print(f"In The Binary Form: %s" %(bin(int(deci,16))))
                time.sleep(5)
            except ValueError:
                print("Only Decimal And Integers Accepted!")
                print("Type Some Simple Numbers like 1,2,3, etc.")
                line()
                time.sleep(5)
        elif i == 'b':
            line()
            time.sleep(1)
            print("\nEntering Binary Form")
            line()
            time.sleep(1)
            bina = input("\nType The Binary Input To Convert In Hexadecimal And Decimal Form:")
            line()
            try:
                time.sleep(1)
                print("\nIn The HexaDecimal Form: %s" %(hex(int(bina,2))))

            except ValueError:
                print("\nOnly Binary Numbers:")
        else:
            print("Please Only Select From 'h' or 'd' or 'b' ")
            time.sleep(5)
starting()
Reply
#2
I think you just want to do this:
@retry(stop=stop_after_attempt(2))
def starting()
There is a lot wrong going on in this bit of code.
print(f"In The Hexadecimal Form: %s" %(int(deci)))
print(f"In The Binary Form: %s" %(bin(int(deci,16))))
Your print format is all messed up. Read this:

https://docs.python.org/3/tutorial/inputoutput.html

Why are you using ast to convert a string to an integer? Immediately after you use int() which will generate an exception, which is what you installed tenacity to handle. Why not do this:
intval = int(inputstr, base)
print(f'In The Hexadecimal Form: {hex(intval)}')
print(f'In The Binary Form: {bin(intval)}')
Where base is 16 for hexicecimal, 8 for octal and 2 for binary.
Reply
#3
(Aug-09-2020, 03:20 PM)deanhystad Wrote: I think you just want to do this:
@retry(stop=stop_after_attempt(2))
def starting()
There is a lot wrong going on in this bit of code.
print(f"In The Hexadecimal Form: %s" %(int(deci)))
print(f"In The Binary Form: %s" %(bin(int(deci,16))))
Your print format is all messed up. Read this:

https://docs.python.org/3/tutorial/inputoutput.html

Why are you using ast to convert a string to an integer? Immediately after you use int() which will generate an exception, which is what you installed tenacity to handle. Why not do this:
intval = int(inputstr, base)
print(f'In The Hexadecimal Form: {hex(intval)}')
print(f'In The Binary Form: {bin(intval)}')
Where base is 16 for hexicecimal, 8 for octal and 2 for binary.

Thanks For Telling I Make Changes Now!

I Made Changes Please Tell Me if i can small the code
from tenacity import *
from ast import literal_eval
#def
import time
def bins(n):
    b_num = list(n)
    value = 0

    for i in range(len(b_num)):
        digit = b_num.pop()
        if digit == '1':
            value = value + pow(2, i)
    print(f"The Decimal Form is: {value}")
    time.sleep(3)
print("Welcome To Decimal To Hex Or Binary Converter")
def line():
    for i in range(50):
        print("-",end='')
print("\nPlease Type The 'h' for hex input")
line()
print("\nPlease Type The 'd' for decimal input")
line()
print("\nPlease Type The 'b' for binary input")
line()
temp = input("\nType 'h' or 'd' or 'b':")
line()
time.sleep(0.1)
print(f"\nSo You type '{temp}'")
time.sleep(0.1)
@retry(stop=stop_after_attempt(2),retry=retry_any())
def starting():
    for i in temp:
        if i == 'h':
            try:
                time.sleep(0.1)
                hexa = input("If You Wish Don't Run The Program Just Type! 'q'\nPlease Type The Hex Input To Convert Into Deciaml And Binary:")
                if hexa == 'q':
                    sys.exit("You Don't To Run The Program")
                else:
                    time.sleep(0.1)
                    print(f"In The Decimal Form: {literal_eval(hexa)}")
                    print(f"In The Binary Form: {(bin(int(hexa,16)))}")
                    time.sleep(3)
            except ValueError:
                print("Only Hex Numbers!")
                print("Hex Numbers Look Like 0x1,0x3, etc.")
                time.sleep(5)
                line()
        elif i == 'd':
            line()
            time.sleep(0.1)
            print("\nEntering Decimal Mode")
            time.sleep(0.1)
            line()
            time.sleep(0.1)
            deci = input("\nIf You Wish Don't Run The Program Just Type! 'q'\nPlease Type The Decimal Input To Convert Into Hex And Binary:")
            try:
                time.sleep(0.1)
                if deci == 'q':
                    sys.exit("")
                else:
                    print(f"In The Hexadecimal Form: {(int(deci))}")
                    print(f"In The Binary Form: {(bin(int(deci,16)))}")
                time.sleep(5)
            except ValueError:
                print("Only Decimal And Integers Accepted!")
                print("Type Some Simple Numbers like 1,2,3, etc.")
                line()
                time.sleep(3)
        elif i == 'b':
            line()
            time.sleep(0.1)
            print("\nEntering Binary Form")

            time.sleep(0.1)
            bina = input("\nIf You Wish Don't Run The Program Just Type! 'q'\nType The Binary Input To Convert In Hexadecimal And Decimal Form:")

            try:
                time.sleep(0.1)
                if bina == 'q':
                    sys.exit()
                else:
                    print(f"\nIn The HexaDecimal Form: {(hex(int(bina,2)))}")
                    print(bins(bina))
            except ValueError:
                print("\nOnly Binary Numbers:")
        else:
            print("Please Only Select From 'h' or 'd' or 'b' ")
            time.sleep(3)
for i in range(0,3):
    while True:
        try:
            starting()
        except ValueError:
            continue
        break
""" 
for i in range(0,100):
    while True:
        try:
            # do stuff
        except SomeSpecificException:
            continue
        break"""
Hey Buddy Due To Your Help I Learnt It!
Thanks For The Help
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Use function, retry until valid Ashcora 8 1,407 Jan-06-2023, 10:14 AM
Last Post: Ashcora
  How to ignore "Retrying (Retry(total=2, connect=2, read=5, redirect=5, status=None))" const 3 2,639 Mar-26-2022, 08:55 AM
Last Post: ndc85430
  requests_futures.sessions retry list of HTTP 429 Johanoosterwaal 0 1,534 Nov-20-2020, 09:23 PM
Last Post: Johanoosterwaal
  How can I fix this code so if i want to retry, it will go through the same loop SteampunkMaverick12 1 1,866 Apr-15-2019, 07:36 PM
Last Post: ichabod801
  During handling of the above exception, another exception occurred Skaperen 7 26,728 Dec-21-2018, 10:58 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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