Strange Exception in Tkinter callback

mtTkinter allows multithreading with Tkinter, you can get it here: http://tkinter.unpythonic.net/wiki/mtTkinter Just import mtTkinter in place of Tkinter. This will allow you to insert text into a Text widget from multiple threads without conflict. I used it for some instant messaging software I wrote and it works wonderfully.

Python IOError: File not open for reading

You opened the file for writing by specifying the mode as ‘w’; open the file for reading instead: ‘r’ is the default, so it can be omitted. If you need to both read and write, use the + mode: w+ opens the file for writing (truncates it to 0 bytes) but also lets you read from it. If you use r+ it … Read more

Sorting list based on values from another list

Shortest Code Example: Generally Speaking Explained: zip the two lists. create a new, sorted list based on the zip using sorted(). using a list comprehension extract the first elements of each pair from the sorted, zipped list. For more information on how to set\use the key parameter as well as the sorted function in general, take a look at this.

Transposing a 1D NumPy array

It’s working exactly as it’s supposed to. The transpose of a 1D array is still a 1D array! (If you’re used to matlab, it fundamentally doesn’t have a concept of a 1D array. Matlab’s “1D” arrays are 2D.) If you want to turn your 1D vector into a 2D array and then transpose it, just slice it with np.newaxis (or None, they’re … Read more

Python code to remove HTML tags from a string 

Using a regex Using a regex, you can clean everything inside <> : Some HTML texts can also contain entities that are not enclosed in brackets, such as ‘&nsbm‘. If that is the case, then you might want to write the regex as This link contains more details on this. Using BeautifulSoup You could also use BeautifulSoup additional package to find … Read more

IOError: [Errno 32] Broken pipe when piping: `prog.py | othercmd`

I haven’t reproduced the issue, but perhaps this method would solve it: (writing line by line to stdout rather than using print) You could catch the broken pipe? This writes the file to stdout line by line until the pipe is closed. You also need to make sure that othercommand is reading from the pipe before it gets too big – https://unix.stackexchange.com/questions/11946/how-big-is-the-pipe-buffer

Difference between exit(0) and exit(1) in Python

0 and 1 are the exit codes. exit(0) means a clean exit without any errors / problems exit(1) means there was some issue / error / problem and that is why the program is exiting. This is not Python specific and is pretty common. A non-zero exit code is treated as an abnormal exit, and at times, … Read more

How to implement the –verbose or -v option into a script?

My suggestion is to use a function. But rather than putting the if in the function, which you might be tempted to do, do it like this: (Yes, you can define a function in an if statement, and it’ll only get defined if the condition is true!) If you’re using Python 3, where print is already a function (or if you’re … Read more