Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I add an exception? if Appname is not found #11

Closed
Sahil481 opened this issue Mar 17, 2023 · 6 comments
Closed

How do I add an exception? if Appname is not found #11

Sahil481 opened this issue Mar 17, 2023 · 6 comments
Labels

Comments

@Sahil481
Copy link
Contributor

The app opener when using the open() function does not throw an error and rather prints to terminal. This makes it hard to do certain things such as speak something using speech recognition library when like the app is not found

@athrvvvv
Copy link
Owner

athrvvvv commented Mar 17, 2023

You could try like

from AppOpener import open

try:
    open("XYZ", output=True)
except Exception as e:
    print(str(e))  # prints "XYZ was not found"
    print("HELLO")

@athrvvvv athrvvvv added waiting for reply Awaiting for the reply from issue commiter. Next version fixes this The next version of AppOpener is going to fix this issue. and removed waiting for reply Awaiting for the reply from issue commiter. labels Mar 24, 2023
@Sahil481 Sahil481 reopened this Mar 26, 2023
@athrvvvv athrvvvv reopened this Mar 28, 2023
@Sahil481 Sahil481 reopened this Mar 28, 2023
@Sahil481
Copy link
Contributor Author

Actually the output=True, just prints when the app is not found or when a app is not running. But if it threw an error then I can do something like:

try:
    open("XYZ", throw_error=True)
except:
    # Now i can do whatever I want here like
    # using the pyttsx3 library I can make it speak something 
    engine = pyttsx3.init()
    engine.say("The App you want to open was not found")
    engine.runAndWait()

It just expands the usability of the module. Previously we couldn't do that because it was just a print statement.
I know it sounds weird becuase I want it to throw an error 😅 but in some cases it's better like the above example that's why I did the PR.

@athrvvvv
Copy link
Owner

You seem beginner in python, here how you may do this without throw_error attribute:

from AppOpener import open

try:
    open("XYZ", output=True)
except Exception as e:
    print(str(e))  # prints "XYZ was not found"
    print("DO NOW WHATEVER YOU WANNA DO")

I would pull back the changes :) Btw, thanks for the pull request ✨🤗

@athrvvvv athrvvvv added Fixed and removed Next version fixes this The next version of AppOpener is going to fix this issue. labels Mar 28, 2023
@athrvvvv athrvvvv pinned this issue Mar 28, 2023
@athrvvvv athrvvvv changed the title Not throwing an error How do I add an exception? if Appname is not found Mar 28, 2023
@Sahil481
Copy link
Contributor Author

Actually your code dosen't even throw an exception error.
Try this code first:

from AppOpener import *

try:
    open("XYZ", output=True)
except Exception as e:
    pass

Even if you don't print the exception it'll still print his line "XYZ" was not found. Explained later why

then try this:

from AppOpener import *

try:
    open("XYZ", output=True)
except Exception as e:
    print("DID IT THROW AN ERROR?")

Then check the output. In the output you won't see this "DID IT THROW AN ERROR?"
You can put anything in the except block it won't be executed even if the app is not found. Yes this "XYZ was not found" will be printed because this is being done through your code in this line. Even if you do not print the exception it will still print it, because there is no exception and the exception is not even a thing.

if output:
     ''' Instead of printing this line it should throw an error '''
     print(f"{self.upper()} NOT FOUND... TYPE (LS) for list of applications.")

Because printing to the console is not the same as raising an error like in my pr. Please do try this in your version of the code and then tell me if it worked because it shouldn't work and I'm pretty sure it's not working for anyone. Try and put anything in the except block it won't get executed in your version of the code, and then try the same in my version of the code.
And I am actually not a beginner in python I'm pretty advanced building APIs and almost a data analyst. Please do do these run's and then tell me what you see.

@athrvvvv
Copy link
Owner

import io
from contextlib import redirect_stdout
from AppOpener import open

output = io.StringIO()
with redirect_stdout(output):
    open("XYZ", output=True)

if " NOT FOUND" in output.getvalue():
    print("DO WHATEVER YOU WANNA DO")

but thats the long route... I will keep the throw_error attribute 💁‍♂️⚡🤝

@Sahil481
Copy link
Contributor Author

Yeah, that is a solution but it's not really developer friendly and it's pretty expected by developers nowdays that the code will throw an error. That is why throw_error attribute is necessary. Thanks for making this module btw love it 💖

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants