Skip to content

Tag: python

Extract traceback info from an exception object

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…

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…

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…

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

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 …