I have a program that reads an XML document from a socket. I have the XML document stored in a string which I would like to convert directly to a Python dictionary, the same way it is done in Django’s simplejson library. Take as an example: Then dic_xml would look like {‘person’ : { ‘name’ : ‘john’, ‘age’ : 20
Splitting a list into N parts of approximately equal length
What is the best way to divide a list into roughly equal parts? For example, if the list has 7 elements and is split it into 2 parts, we want to get 3 elements in one part, and the other should have 4 elements. I’m looking for something like even_split(L, n) that breaks L into n parts. The code above
Split a string at newline characters
I have a string, say How do we split the above with the delimiter n (a newline)? The result should be Answer If you are concerned only with the trailing newline, you can do: See, str.lstrip() and str.strip() for variations. If you are more generally concerned by superfluous newlines producing empty items, you can do:
real time subprocess.Popen via stdout and PIPE
I am trying to grab stdout from a subprocess.Popen call and although I am achieving this easily by doing: I would like to grab stdout in “real time”. With the above method, PIPE is waiting to grab all the stdout and then it returns. So for logging purposes, this doesn’t meet my requirements (e.g. “see” what is going on while
How to read specific lines from a file (by line number)?
I’m using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this? Answer If the file to read is big, and you don’t want to read the whole file in memory at once: Note that i == n-1 for the nth line.
Django Query That Get Most Recent Objects From Different Categories
I have two models A and B. All B objects have a foreign key to an A object. Given a set of A objects, is there anyway to use the ORM to get a set of B objects containing the most recent object created for each A object. Here’s an simplified example: So I’m looking for a query that returns
Fastest way to list all primes below N
This is the best algorithm I could come up. Can it be made even faster? This code has a flaw: Since numbers is an unordered set, there is no guarantee that numbers.pop() will remove the lowest number from the set. Nevertheless, it works (at least for me) for some input numbers: Answer Warning: timeit results may vary due to differences
Python How to I check if last element has been reached in iterator tool chain?
How do I achieve this Answer When the loop ends, the elt variable doesn’t go out of scope, and still holds the last value given to it by the loop. So you could just put the code at the end of the loop and operate on the elt variable. It’s not terribly pretty, but Python’s scoping rules aren’t pretty either.
ttk.Button returns None [duplicate]
This question already has answers here: Tkinter: AttributeError: NoneType object has no attribute <attribute name> (4 answers) Closed 8 months ago. I am trying to use the invoke method of a ttk.Button, as shown at TkDocs (look at “The Command Callback”), but I keep getting this error: AttributeError: ‘NoneType’ object has no attribute ‘invoke’ So, I tried this in the
How to access outer class from an inner class?
I have a situation like so… How can I access the Outer class’s method from the Inner class? Answer The methods of a nested class cannot directly access the instance attributes of the outer class. Note that it is not necessarily the case that an instance of the outer class exists even when you have created an instance of the