Explaining Python’s ‘__enter__’ and ‘__exit__’

Using these magic methods (__enter__, __exit__) allows you to implement objects which can be used easily with the with statement. The idea is that it makes it easy to build code which needs some ‘cleandown’ code executed (think of it as a try-finally block). Some more explanation here. A useful example could be a database connection object (which then automagically closes … Read more

How do I calculate the MD5 checksum of a file in Python?

In regards to your error and what’s missing in your code. m is a name which is not defined for getmd5() function. No offence, I know you are a beginner, but your code is all over the place. Let’s look at your issues one by one 🙂 First, you are not using hashlib.md5.hexdigest() method correctly. Please refer explanation on hashlib functions … Read more

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.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)