Indent Expected?

There are, in fact, multiple things you need to know about indentation in Python: Python really cares about indention. In other languages, indention is not necessary but only serves to improve the readability. In Python, indentation is necessary and replaces the keywords begin / end or { } of other languages. This is verified before the execution of the … Read more

Filtering a NumPy Array

Summary The following tests are meant to give some insights into the different approaches and should be taken with a grain of salt. What is being tested here is not exactly generic filtering, but rather just applying a threshold, which has the notable feature that computing the condition is pretty fast. Very different results would … Read more

How do operator.itemgetter() and sort() work?

Looks like you’re a little bit confused about all that stuff. operator is a built-in module providing a set of convenient operators. In two words operator.itemgetter(n) constructs a callable that assumes an iterable object (e.g. list, tuple, set) as input, and fetches the n-th element out of it. So, you can’t use key=a[x][1] there, because python has no idea what x is. … Read more

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