One of my favorite debug tools is a short piece of code which starts up a console. I sometimes debug by means of (roughly)
try:
#my code
...
except:
launch_console()
However, this requires anticipating where the error will be. If I enclose the entire script in a try/except, I will not have access to any variables within the function I am debugging.
It would be magnificently convenient, if I could run the program and just automatically have launch_console() run in case of an error, without losing scope. I think spyder is supposed to have this functionality, but it does not work.
Wondering if there is some nice way to do this!
I am trying to hot swap a file in python. I am creating a game that takes a really long time to load. But I don't want to reload it every time. I am trying to change some code while the programme is in runtime.
For example:
I want to change this:
while True:
print("Hello")
to this while in runtime:
while True:
print("Hello World")
I looked hot swapping up for python and all of them are answers that I am not looking for. All the other answers change modules. I want to change the current file. Like java in eclipse. Please help!
I want to change the current file. Like java in eclipse.
When you modify Java code in Eclipse, the code is not currently running. In reality, Eclipse has a Java compiler built-in, which attempts to compile your code as you type it. That's how Eclipse is able to give you such fast feedback about syntax errors and type errors in your code. But it can't give you any information about the runtime behaviour of your code (whether or not it produces the right answer), because it doesn't run the code! You need to press the Run button for that.
So I think the question you really want to ask is not "Can I dynamically modify Python code?" (the answer to that is "Yes, but it's complicated and has caveats and is not a good idea") but "Does there exist a Python IDE which can give me feedback about syntax errors while I type?" The answer to that is an emphatic "Yes!". There's an extremely comprehensive list of options in this answer.
You can use dynamic execution using exec function.
Store the code you want to execute in a string and change it during runtime.
So I have been messing around with python with openCV the last couple of weeks and it has worked perfectly. My problem is when I took two desperate code script's and put them together it wouldn't compile. (I know Duh python white space (I fixed all of it,"I think")) It threw an error in the terminal with a line number. At this time I was using text editor in Ubuntu so I threw it into Geany to figure out what line. When I got there I couldn't pick out what the error was all of the indentation was that of the original code and it fitted with that of the rest of the loop it was nested in. So I tried to compile it in Geany and it didn't throw errors at all. I find this extremely weird because Geany is only an editor and it relies on outside compilers to compile the code. I am assuming that the terminal is using the same compilers too (though I know it must not now). I though maybe it has something to do with the library openCV because I am not including it in Geany. So I changed the variable name that was throwing things. After that it still threw the same error so I came here confused.
The error message I am getting is
http://imgur.com/1otMyeZ
My code is at
http://pastebin.com/HYKjnyyc
The part that is giving error is here
http://pastebin.com/6TyXs3uc
Your problem still goes back to the white space you were working on. Everything (except line 297 to the end) below:
# THE DEMO PROGRAM STARTS HERE
is indented, including your three global variables, which makes them appear to the compiler as part of the class Target, which takes the variables out of the global scope, so they are not defined. Remove the unnecessary indentation in that area.
EDIT:
Looking back at your code again, You may also have some indentation problems from the
# Function Definitions
at line 88 and down. From the way the functions are indented they appear to be part of the class, but from the way they are called they appear not to be.
You would find writing in python a lot less frustrating if you use a more standard approach to your indentation/white-space handling. Read this PEP for some best practices in that regard.
I am creating a GUI program using wxPython. I am also using matplotlib to graph some data. This data needs to be animated. To animate the data I am using the FuncAnimate function, which is part of the matplotlib package.
When I first started to write my code I was using a PC, running windows 7. I did my initial testing on this computer and everything was working fine. However my program needs to be cross platform. So I began to run some test using a Mac. This is where I began to encounter an error. As I explained before, in my code I have to animate some data. I programmed it such that the user has the ability to play and pause the animation. Now when the user pauses the animation I get the following error: AttributeError: 'FigureCanvasWxAgg' object has no attribute '_idletimer'. Now I find this to be very strange because like I said I ran this same code on a PC and never got this error.
I was wondering if anyone could explain to me what is meant by this _idletimer error and what are possible causes for this.
_idletimer is likely to be a private, possibly implementation specific member of one of the classes - since you do not include the code or context I can not tell you which.
In general anything that starts with an _ is private and if it is not your own, and specific to the local class, should not be used by your code as it may change or even disappear when you rely on it.
Python is pretty clean, and I can code neat apps quickly.
But I notice I have some minor error someplace and I dont find the error at compile but at run time. Then I need to change and run the script again. Is there a way to have it break and let me modify and run?
Also, I dislike how python has no enums. If I were to write code that needs a lot of enums and types, should I be doing it in C++? It feels like I can do it quicker in C++.
"I don't find the error at compile but at run time"
Correct. True for all non-compiled interpreted languages.
"I need to change and run the script again"
Also correct. True for all non-compiled interpreted languages.
"Is there a way to have it break and let me modify and run?"
What?
If it's a run-time error, the script breaks, you fix it and run again.
If it's not a proper error, but a logic problem of some kind, then the program finishes, but doesn't work correctly. No language can anticipate what you hoped for and break for you.
Or perhaps you mean something else.
"...code that needs a lot of enums"
You'll need to provide examples of code that needs a lot of enums. I've been writing Python for years, and have no use for enums. Indeed, I've been writing C++ with no use for enums either.
You'll have to provide code that needs a lot of enums as a specific example. Perhaps in another question along the lines of "What's a Pythonic replacement for all these enums."
It's usually polymorphic class definitions, but without an example, it's hard to be sure.
With interpreted languages you have a lot of freedom. Freedom isn't free here either. While the interpreter won't torture you into dotting every i and crossing every T before it deems your code worthy of a run, it also won't try to statically analyze your code for all those problems. So you have a few choices.
1) {Pyflakes, pychecker, pylint} will do static analysis on your code. That settles the syntax issue mostly.
2) Test-driven development with nosetests or the like will help you. If you make a code change that breaks your existing code, the tests will fail and you will know about it. This is actually better than static analysis and can be as fast. If you test-first, then you will have all your code checked at test runtime instead of program runtime.
Note that with 1 & 2 in place you are a bit better off than if you had just a static-typing compiler on your side. Even so, it will not create a proof of correctness.
It is possible that your tests may miss some plumbing you need for the app to actually run. If that happens, you fix it by writing more tests usually. But you still need to fire up the app and bang on it to see what tests you should have written and didn't.
You might want to look into something like nosey, which runs your unit tests periodically when you've saved changes to a file. You could also set up a save-event trigger to run your unit tests in the background whenever you save a file (possible e.g. with Komodo Edit).
That said, what I do is bind the F7 key to run unit tests in the current directory and subdirectories, and the F6 key to run pylint on the current file. Frequent use of these allows me to spot errors pretty quickly.
Python is an interpreted language, there is no compile stage, at least not that is visible to the user. If you get an error, go back, modify the script, and try again. If your script has long execution time, and you don't want to stop-restart, you can try a debugger like pdb, using which you can fix some of your errors during runtime.
There are a large number of ways in which you can implement enums, a quick google search for "python enums" gives everything you're likely to need. However, you should look into whether or not you really need them, and if there's a better, more 'pythonic' way of doing the same thing.