I come from a C# background where the language has some built in “protect the developer” features. I understand that Python takes the “we’re all adults here” approach and puts responsibility on the developer to code thoughtfully and carefully. That said, Python suggests conventio…
Tag: python
Finding a key recursively in a dictionary
I’m trying to write a very simple function to recursively search through a possibly nested (in the most extreme cases ten levels deep) Python dictionary and return the first value it finds from the given key. I cannot understand why my code doesn’t work for nested dictionaries. It returns None. It…
How to remove escape sequence like ‘xe2’ or ‘x0c’ in python
I am working on a project (content based search), for that I am using ‘pdftotext’ command line utility in Ubuntu which writes all the text from pdf to some text file. But it also writes bullets, now when I’m reading the file to index each word, it also gets some escape sequence indexed(like …
Sum the digits of a number
If I want to find the sum of the digits of a number, i.e.: Input: 932 Output: 14, which is (9 + 3 + 2) What is the fastest way of doing this? I instinctively did: and I found this online: Which is best to use for speed, and are there any other methods which are even faster? Answer Both
How to return a matplotlib.figure.Figure object from Pandas plot function
I have a dt: And use pandas function plot to create a barplot object But I want a <matplotlib.figure.Figure object> instead to pass to other function, just like the object type below: So how can I transform <matplotlib.axes.AxesSubplot object> to <matplotlib.figure.Figure object> or directly…
change the colorbar width
I have created this, as you can see the colorbar is very thin, is there anyway of increasing the width of the colorbar without increasing the height Answer You need to get hold of the axes of the colorbar. Assuming you hold a reference to the colorbar in cbar you get to the axes via cbar.ax. You can control t…
Kill program listening to a given port
Before the main code execution starts, I want to check if a particular port is open or not.. and if it is.. then close the port so that code can start “cleanly”. So I google online and found the code here: http://www.paulwhippconsulting.com.au/tips/63-finding-and-killing-processes-on-ports But jus…
Sum of numbers in array, not counting 13 and number directly after it (CodingBat puzzle)
The Question: Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count. My Code: Where It’s Failing: Answer Your problem is when x is zero. x – 1 will be -1
Override a “private” method in Python
Consider a class with a “private” method such as: When I try to subclass Foo and override method __method, it can be seen that Foo.__method is still called, instead of MoreFoo.__method. What would be the way to override such a method? Answer The point of using the double-underscore name convention…
Merge xml files with nested elements without external libraries
I am trying to merge multiple XML files together using Python and no external libraries. The XML files have nested elements. Sample File 1: Sample File 2: What I Want: What I have tried: From this answer. What I Get: I hope you can see and understand my problem. I am looking for a proper solution, any guidanc…