I'm trying to use warnings.simplefilter to display my warning once. I've created a subclass to DeprecationWarning. I tried putting the simplefilter in the same module as my warning, and in the package level init as far to the top I could but it will always display the warning on every call. Tested in python 3.4.
my warning:
class MyDeprecationWarning(DeprecationWarning):
pass
how I'm calling simplefilter:
warnings.simplefilter('once', MyDeprecationWarning)
how I'm calling warn:
warnings.warn("Warning!", MyDeprecationWarning)
What's the issue?
If your program is running multiple times or some code in running in a separate process, you may have not issued your commands in the right order. The following program works as expected.
import warnings
class MyDeprecationWarning(DeprecationWarning):
pass
def main():
print('Program Starting')
warnings.simplefilter('once', MyDeprecationWarning)
for _ in range(100):
warnings.warn('Warning!', MyDeprecationWarning)
print('Program Finished')
if __name__ == '__main__':
main()
Related
I am somewhat new to python and error handling. I am running a program that connects to a data source and continuously receives market data, every so often I encounter an "Exception in thread Thread" which I think happens because an issue with the data connection occurs, so the computation that the program should be doing can't be completed. I think I can resolve this problem by restarting the program from the beginning if the connection fails. Something like... if this type of error is encountered, go back to the beginning and try to reconnect, and if that doesn't fix the problem after a few attempts, stop trying. The thread is currently started in the App class, when the class is initialized, so going back to the first line would solve the problem, I'm just not sure how I would do this. My main code is fairly complex, but is structured like so:
if __name__ == '__main__':
my_app = App(args)
while my_app.market_open():
do some computation
do some more computation, etc.
my_app.disconnect()
Define a function def main():
and within it, do the (re)connection stuff.
Then you write the famous
if __name__ == '__main__' :
main()
To restart from anywhere within your program, call main() from wherever you are! In this case, you put everything under/after if __name__ == "__main__" into main()s definition and from inside main() call main()
You might also try putting your calculations into a try / except loop to catch the error and redirect the code to a new function call? You will need to specify the error:
try:
while my_app.market_open():
do some computation
do some more computation, etc.
except WhatEverYourErrorIsHere:
while my_app.market_open():
do some computation again?
Here is example running different functions in two threads every minute. Again, you will need to specify the Error to catch:
import schedule, threading, datetime
def someCallableFunction():
try:
print('running someCallableFunction\n')
except ConnectionError:
print(datetime.utcnow(), 'External Connection Error')
return
def someOtherCallableFunction():
try:
print('running someOtherCallableFunction\n')
except ConnectionError:
print(datetime.utcnow(), 'External Connection Error')
return
def run_threaded(job_func):
job_thread = threading.Thread(target=job_func)
job_thread.start()
schedule.every(1).minutes.at(':00').do(run_threaded, someCallableFunction)
schedule.every(1).minutes.at(':00').do(run_threaded,
someOtherCallableFunction)
while True:
schedule.run_pending()
I have the following structure in code.
In main.py:
def run(parameters):
# do something
In execution.py:
import main
if __name__ = "main":
main.run(parameters)
However, I get the following error - main has no 'run' member.
When I run that code there is no output, because the test in execution.py should be if __name__ == "__main__":. The if-test in your code is never true.
Buy when I fix that problem, main.run() executes as expected, but only if I fix another problem. The function body of run() must consist of more than a comment. You need pass at the very least.
So that is a syntax error, which means that the def statement doesn't actually define the function. Which explains the message.
I am doing a simple project on my Pycharm IDE.
My code is this:
import webbrowser
import time
socialMediaUrls = ["www.google.com","www.edureka.com"]
techUrls = ["www.udacity.com","www.dailymotion.com"]
def open_tabs(url_list):
for element in url_list:
webbrowser.open_new_tab(element)
def main():
webbrowser.open("www.youtube.com",new=0,autoraise=false)
time.sleep(1)
open.tab(socialMedialUrls)
open_tabs(techUrls)
main()
but after running I am getting this message:
C:\Users\adc\AppData\Local\Programs\Python\Python36-32\python.exe
C:/Users/adc/PycharmProjects/untitled1/ur.py
Process finished with exit code 0
And I am getting same message for all my projects. What should I do?
You should call main in that way:
def main():
webbrowser.open("www.youtube.com",new=0,autoraise=false)
time.sleep(1)
open.tab(socialMedialUrls)
open_tabs(techUrls)
if __name__ == "__main__":
main()
Also I see that your code contains some other errors. For example, in Python there is False keyword, not false. Lines with open.tab and open_tabs will not work too.
Currently, no instructions are reachable in your script (besides the import statements)
in:
def main():
webbrowser.open("www.youtube.com",new=0,autoraise=false)
time.sleep(1)
open.tab(socialMedialUrls)
open_tabs(techUrls)
main()
indentation suggests that you're performing a recursive call (which isn't what you want).
Unindent main() to make sure you execute something in your script.
Or put the instructions of main at zero-indent level outside any procedure (in that case, it is executed even when importing the module, probably not important here)
(note that python programs don't need a main(), this isn't C)
I use a list of different python scripts for various functions. To help facilitate this, I've organized all of my reusable functions into custom libraries. However, I found that many of these functions will error out for strange reasons, some known and some unknown. I designed the below function to at least let me see the error message before throwing a giant traceback at me. I have the below command in one library:
FooBar = trace_check(lambda: Foo(bar))
This is the error catching function in a separate library:
def trace_check(func):
try:
return func()
except:
TracebackString = traceback.format_exc() ###This gets the traceback as a string.
type, message, tracebacklocation = sys.exc_info() ###This gets the components, particularly the message.
print "An error has occurred with the following error message:"
print type ###Example is IOError
print message ###The message associated with the error
TracebackPrompt = ask("Do you want to see the entire traceback?") #Returns True/False value
if TracebackPrompt:
print TracebackString
print 'Exiting Python Script' ###This shows me it gets to this point.
sys.exit(0) ###This is the problem
print "Did it work?" ###This statement does not print, so exit worked...
When trace_check runs and I get an error, the sys.exit only quits the function back out to main() instead of ending main. if I use os._exit() instead, the main() function ends correctly, but the program running the script also dies. One command is not strong enough and the other is overkill... what could I do instead to ensure the main() function ends?
Note: I tried putting the meat of my trace_check function into the first library, but the same thing happens with the library call ending but not the main().
tl;dr - Python: main() calls function in library that calls a second function in separate library. Second function has a sys.exit() command that only exits to main() instead of ending main(). os._exit() kills shell and is overkill (requires restarting shell TT^TT). Is there another way to end the main() from a function library?
To directly answer your question, if you want to handle sys.exit() calls from main, then you should catch the SystemExit exception that is raised by sys.exit(). The sample code below illustrates how to do that.
import sys
def func():
sys.exit(1)
def main():
try:
func()
except SystemExit:
print 'Someone sys.exit()d'
return 0
if __name__ == '__main__':
sys.exit(main())
However! You should probably redesign your library. Instead of calling sys.exit() when something unexpected happens, you should raise an Exception. Having a library abruptly exit the interpreter is bad design.
You could try setting this off by throwing an exception:
class ExitFromMain(Exception):
pass
def trace_check(func):
try:
# try stuff
except:
# traceback stuff you had
raise ExitFromMain()
def main():
try:
# Stuff
trace_check()
# More stuff that will not run if the exception is thrown
except ExitFromMain:
print "I hit my excception to flag a quit from this function"
sys.exit(0)
I have this code:
import sys
def random(size=16):
return open(r"C:\Users\ravishankarv\Documents\Python\key.txt").read(size)
def main():
key = random(13)
print(key)
When I try running the script, there are no errors, but nothing appears to happen. I expected it to print some content from the key file, but nothing is printed.
What is wrong? How do I make the code run?
You've not called your main function at all, so the Python interpreter won't call it for you.
Add this as the last line to just have it called at all times:
main()
Or, if you use the commonly seen:
if __name__ == "__main__":
main()
It will make sure your main method is called only if that module is executed as the starting code by the Python interpreter. More about that here: What does if __name__ == "__main__": do?
If you want to know how to write the best possible 'main' function, Guido van Rossum (the creator of Python) wrote about it here.
Python isn't like other languages where it automatically calls the main() function. All you have done is defined your function.
You have to manually call your main function:
main()
Also, you may commonly see this in some code:
if __name__ == '__main__':
main()
There's no such main method in python, what you have to do is:
if __name__ == '__main__':
main()
Something does happen, it just isn't noticeable
Python runs scripts from top to bottom. def is a statement, and it executes when it is encountered, just like any other statement. However, the effect of this is to create the function (and assign it a name), not to call it. Similarly, import is a statement that loads the other module (and makes its code run top to bottom, with its own global-variable context), and assigns it a name.
When the example code runs, therefore, three things happen:
The code for the sys standard library module runs, and then the name sys in our own module's global variables is bound to that module
A function is created from the code for random, and then the name random is bound to that function
A function is created from the code for main, and then the name main is bound to that function
There is nothing to call the functions, so they aren't called. Since they aren't called, the code inside them isn't run - it's only used to create the functions. Since that code doesn't run, the file isn't read and nothing is printed.
There are no "special" function names
Unlike in some other languages, Python does not care that a function is named main, or anything else. It will not be run automatically.
As the Zen of Python says, "Explicit is better than implicit". If we want a function to be called, we have to call it. The only things that run automatically are the things at top level, because those are the instructions we explicitly gave.
The script starts at the top
In many real-world scripts, you may see a line that says if __name__ == '__main__':. This is not "where the script starts". The script runs top to bottom.
Please read What does if __name__ == "__main__": do? to understand the purpose of such an if statement (short version: it makes sure that part of your top-level code is skipped if someone else imports this file as a module). It is not mandatory, and it does not have any kind of special "signalling" purpose to say where the code starts running. It is just a perfectly normal if statement, that is checking a slightly unusual condition. Nothing requires you to use it in a script (aside from wanting to check what it checks), and nothing prevents you from using it more than once. Nothing prevents you from checking whether __name__ is equal to other values, either (it's just... almost certainly useless).
You're not calling the function. Put main() at the bottom of your code.