Python Forum
Retry After Exception - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Retry After Exception (/thread-28909.html)



Retry After Exception - Harshil - Aug-09-2020

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()



RE: Retry After Exception - deanhystad - Aug-09-2020

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.


RE: Retry After Exception - Harshil - Aug-09-2020

(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