How do I mock async call from one native coroutine to other one using unittest.mock.patch? I currently have quite an awkward solution: Then This works but looks ugly. Is there more pythonic way to do this? Answer The solution was actually quite simple: I just needed to convert __call__ method of mock into coroutine: This works perfectly, when mock is
How to get Top 3 or Top N predictions using sklearn’s SGDClassifier
In the above code, clf.predict() prints only 1 best prediction for a sample from list X. I am interested in top 3 predictions for a particular sample in the list X, i know the function predict_proba/predict_log_proba returns a list of all probabilities for each feature in list Y, but it has to sorted and then associated with the features in
“Python: ‘unicodeescape’ codec can’t decode bytes…: malformed N character escape” when I’m trying to type an input statement
I was creating a program to store employee-related data. as a mini project. However as I tried to run my (barely-completed program_ I get the unicode error message and Python highlights the parenthesis after input. I’ve googled for answers but it seems like the common issue is related to file paths which I am not dealing with here. This is
PyCharm asks for python interpreter every time project is loaded
Rather frustratingly, every time I load up an existing project or create a new one – it complains there is no interpreter selected and I have to provide the path to it. There is nothing unusual about my python installation, it’s sat in C:/Python27/ as you would expect. It always used to work, but the last few weeks something has
Change resolution of half-degree netCDF to quarter degree netCDF using nco tools
I can change the resolution of a netCDF file to a coarser one by doing something like this: How do I go the other way? I.e. change resolution of coarser scale netCDF to finer scale? Answer Increasing resolution with NCO requires regridding, available in NCO 4.5.1+. This currently requires you have a SCRIP/ESMF-compliant mapfile, which can be generated from SCRIP-compliant
Iterating through pandas groupby groups
I have a pandas dataframe school_df that looks like this: Each row represents one project by that school. I’d like to add two columns: for each unique school_id, a count of how many projects were posted before that date and a count of how many projects were completed before that date. The code below works, but I have ~300,000 unique
How to get form POST input in Tornado?
I amd new to Tornado framework and trying to make a simple form to upload images: I can successfully receive the Posted file using: However I’m unable to receive the alt input. I tried alt = self.request.alt but I get this error and when I use alt = self.request.files[‘alt’], I get: I ran out of ideas so appreciate your help.
Logging module not working with Python3
I am having issues with the standard logging module. If I open a python2.7 shell and import logging everything works fine: But if I open a python3.4 shell and import logging I get the following error: I have no idea what the problem is and can’t seem to find anyone else who has had the same issue. Answer You seem
QtConcurrent in PySide/PyQt
I’m trying to figure out if subclassing QtConcurrent and writing a run method inside it will work: Or is it completely useless? Answer It’s completely useless, because QtConcurrent is a namespace, not a class. Also, neither PyQt nor PySide provide any of the functionality provided by QtConcurrent, because it’s all template-based and therefore impossible to wrap. PS: the PySide documentation
celery doesn’t work with global variable
this is the code I used to test celery. I hope every time I use addone() the return value should increase. But it’s always 1 why??? Results Answer By default when a worker is started Celery starts it with a concurrency of 4, which means it has 4 processes started to handle task requests. (Plus a process that controls the