I noticed several times that a simple python script (with some straightforward algebraic computation in a loop) runs considerably faster (up to a factor 6) when launched on a shell command line as compared to a run via IDLE's shell. There is nothing fancy going on in the script. I only print a loop variable to visually follow progress in the loop.
Surely just this print statement cannot be the reason for the speed loss in IDLE, or can it ?
Can someone explain me why this is ?
The Tkinter text widget is slow, and IDLE uses this to print text to screen.
See this question.
Related
I've been working on a small script in python to press some keys when I press one specific (I created a macro).
All works fine since I try to use it in any other application like for example a videogame or other programs.
The problem is that it only executes once, and then the only way it executes again is chainging to my IDE tab (Visual studio code) and returning back to the application, and this is quite annoying.
Any idea about it and how to solve that window problem? Here is the code
def macros():
t=threading.Timer(0.1,macros)
t.start()
if keyboard.is_pressed('k'):
keyboard.press('e')
keyboard.press('q')
print("active")
I want to make a program stop running for a certain amount of time to save CPU power. I have a self.after function that I want to run to make the program stop running for a set amount of time. However, it always crashes the program.
self.after(int(self.timeSleep*1000),print("Sleeping."))
The word Sleeping. is printed but the window crashes. I thought that Sleeping. should only be printed after the "sleep" was done. I'm not sure what I'm doing wrong and I couldn't find another question like this. Forgive me if it's a stupid mistake as it's my first time using TKinter.
Thanks in advance.
EDIT:
I am not getting any errors in my terminal. By crash I mean the window stops responding.
Your code has the same effect as below:
execute print("Sleeping.")
execute self.after(int(self.timeSleep*1000), None) which is the same as time.sleep(int(self.timeSleep*1000)).
You need to change it to use lambda:
self.after(int(self.timeSleep*1000), lambda: print("Sleeping."))
I found a solution that doesn't require using self.after. Instead, you can use a thread to run this specific method, allowing you to use while loops and time.sleep without crashing the main GUI program.
Setup: iTerm2 in Mac OS X with a python 2.7 application using curses. Leaving it alone works fine; it uses a timer to update information and rewrite the screen and two pads. If I accidentally "scroll up" with the mousewheel or mousepad, I see the individual "frames" written by iterations in the program's while-loop. Also, this causes some kind of memory or CPU lag such that it takes a VERY long time to get back to the bottom and view the actual application again.
If at all possible, I'd like to avoid this. Any thoughts?
I am new to both blender and python.
I tried to manipulate some properties of an object via python script in script console of blender.
What I don't understand is I can do it in this way.
bpy.data.object['Cube'].rotation_euler.x+=1
but when I put it in a loop.
import time
i=1
while i<100:
i+=1
bpy.data.object['Cube'].rotation_euler.x+=1
print('run once')
time.sleep(5)
Blender freezes without any output of 'run once'.
Would someone please tell me what is wrong with this code.
Your script isn't freezing, blender just isn't getting a chance to update during the loop.
The time.sleep(5) command sleeps for 5 seconds, being run 100 times means the script takes 8 minutes to run at which stage blender updates it's interface again.
You may want to look at a modal operator - there are several samples within the python templates available in blender's text editor.
I am using DataNitro to write Python Script in Excel. Its very useful indeed. However, when I open the Idle editor in excel, the accompanying Python Shell is not interactive, in that it does not return print statements, show errors, nothing. It just restarts every time I run the programme. This makes it incredibly hard to debug as I can't use print statements to trace the errors.
Does anyone know if this is a bug with DataNitro, or is it supposed to be that way, or whats going on? are there any solutions?
Thanks so much
Our IDLE editor is just an editor - it doesn't work as a shell.
The best way to debug programs is to raise an exception. This will freeze the shell that opens when a script is run, and you'll be able to inspect the variables and see any print statements that were generated during execution.
For example, if you run:
print Cell("A1").value
x = Cell("B1").value
raise
You'll see the value of A1 printed to the shell, and you can enter "x" at the prompt to see the value of B1.
You can also import a script you're working on into the regular Python shell (the one that opens when you press "shell"). This will execute the code in that script.
We'll be adding a guide to debugging code to the site soon, as well as some features that make it easier.
Source: I'm one of the founders of DataNitro.
Not as knowledgeable as Ben, but have been using DataNitro quite a bit and here are some tips:
The shell automatically closes once the script has run. If you want to inspect some prints or even interact with the shell I normally place following at end of my script.
raw_input("Press Enter to Exit shell")
Not very elegant, but I have even created a small loop that displays text options in the console. Can then interact with your program and sheet from there. Clever and more elegant way would be to have your script poll an excel cell and then take action form there.
Something else that you might find nice is that it also also you to run Ipython instead of the default python shell. Cannot imagine using python without Ipython... so you get benefits of tab completion Ipython debugging etc. To activate that just click the "Use Ipython" Checkbox in DataNitro Settings (don't know if this is version dependent).