How can I decorate an instance method with a decorator class?

tl;dr You can fix this problem by making the Timed class a descriptor and returning a partially applied function from __get__ which applies the Test object as one of the arguments, like this class Timed(object): def __init__(self, f): self.func = f def __call__(self, *args, **kwargs): print(self) start = dt.datetime.now() ret = self.func(*args, **kwargs) time = … Read more

multiprocessing.dummy in Python is not utilising 100% cpu

When you use multiprocessing.dummy, you’re using threads, not processes: multiprocessing.dummy replicates the API of multiprocessing but is no more than a wrapper around the threading module. That means you’re restricted by the Global Interpreter Lock (GIL), and only one thread can actually execute CPU-bound operations at a time. That’s going to keep you from fully … Read more

Django SMTPAuthenticationError

A relatively recent change in Google’s authentication system means you’re going to have to “allow less secure app access” to your Google account, in order for this to work. In your error, you are recommended to visit this link: https://support.google.com/mail/answer/78754 On that page: Step #2 asks you to try Displaying an Unlock Captcha Step #3 … Read more

How to terminate process from Python using pid?

Using the awesome psutil library it’s pretty simple: p = psutil.Process(pid) p.terminate() #or p.kill() If you don’t want to install a new library, you can use the os module: import os import signal os.kill(pid, signal.SIGTERM) #or signal.SIGKILL See also the os.kill documentation. If you are interested in starting the command python StripCore.py if it is … Read more

Matplotlib log scale tick label number formatting

Sure, just change the formatter. For example, if we have this plot: import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.axis([1, 10000, 1, 100000]) ax.loglog() plt.show() You could set the tick labels manually, but then the tick locations and labels would be fixed when you zoom/pan/etc. Therefore, it’s best to change the formatter. By default, … Read more

Pretty print 2D list?

To make things interesting, let’s try with a bigger matrix: matrix = [ [“Ah!”, “We do have some Camembert”, “sir”], [“It’s a bit”, “runny”, “sir”], [“Well,”, “as a matter of fact it’s”, “very runny, sir”], [“I think it’s runnier”, “than you”, “like it, sir”] ] s = [[str(e) for e in row] for row in … Read more