Given an Exception object (of unknown origin) is there way to obtain its traceback? I have code like this: How can I extract the traceback from the Exception object once I have it? Answer The answer to this question depends on the version of Python you’re using. In Python 3 It’s simple: exceptions…
Tag: python
How to remove the first and last item in a list?
I have the List How do I remove the first element, Q and 0002, the last element? Answer I’m not sure exactly what you want since it is unclear but this should help. You actually only have a single element in that list. Assuming all your list items are strings with spaces as delimiters, here is how you c…
Monitor thread synchronization in python
Is there any way to use monitor thread synchronization like java methods synchronization,in python class to ensure thread safety and avoid race condition? I want a monitor like synchronization mechanism that allows only one method call in my class or object Answer You might want to have a look at python threa…
Difference between Levenberg-Marquardt-Algorithm and ODR
I was able to fit curves to a x/y dataset using peak-o-mat, as shown below. Thats a linear background and 10 lorentzian curves. Since I need to fit many similar curves I wrote a scripted fitting routine, using mpfit.py, which is a Levenberg-Marquardt-Algorithm. However the fit takes longer and, in my opinion,…
How can I check whether a numpy array is empty or not?
How can I check whether a numpy array is empty or not? I used the following code, but this fails if the array contains a zero. Is this the solution? Answer You can always take a look at the .size attribute. It is defined as an integer, and is zero (0) when there are no elements in the array:
Using a RegEx to match IP addresses
I’m trying to make a test for checking whether a sys.argv input matches the RegEx for an IP address… As a simple test, I have the following… However when I pass random values into it, it returns “Acceptable IP address” in most cases, except when I have an “address” th…
Efficiently accumulating a collection of sparse scipy matrices
I’ve got a collection of O(N) NxN scipy.sparse.csr_matrix, and each sparse matrix has on the order of N elements set. I want to add all these matrices together to get a regular NxN numpy array. (N is on the order of 1000). The arrangement of non-zero elements within the matrices is such that the resulti…
Recording the total time taken for running a spider in scrapy
I am using scrapy to scrap a site I had written a spider and fetched all the items from the page and saved to a csv file, and now i want to save the total execution time taken by scrapy to run the spider file, actually after spider execution is completed and when we have at at terminal it will
Pause an infinite while loop with a single keypress
I’m new to python and I have been trying to find a simple way to pause an operating while-loop, making it possible to start it again from where it was paused. I have searched on Google for help and tips, but everything I find seems very complex. Is there a simple way to do this? I’ve read that you…
Adding delimiters and removing commas
I have a file from which I’ve output several columns of information, 4 to be exact. At this moment they are separated by commas, but in order for my buddy to feed them into another script, he wants the format to be with ‘|’ as delimiter and the commas removed. The commas follow every set of …