I’d like to know what happens when I pass the result of a generator function to python’s enumerate(). Example: Is the enumeration iterated lazily, or does it slurp everything into the <enumerate object> first? I’m 99.999% sure it’s lazy, so can I treat it exactly the same as the generator function, or do I need to watch out for anything?
Aggregating save()s in Django?
I’m using Django with an sqlite backend, and write performance is a problem. I may graduate to a “proper” db at some stage, but for the moment I’m stuck with sqlite. I think that my write performance problems are probably related to the fact that I’m creating a large number of rows, and presumably each time I save() one it’s
python: list assignment index out of range
i am getting this error on the last line: i dont understand how it can be out of range if it exists! please help Answer If row is a list, then don’t forget that deleting an element of a list will move all following elements back one place to fill the gap, so all of the later indices into the
Check if multiple strings exist in another string
How can I check if any of the strings in an array exists in another string? For example: How can I replace the if a in s: line to get the appropriate result? Answer You can use any: Similarly to check if all the strings from the list are found, use all instead of any.
Python: Regex to extract part of URL found between parentheses
I have this weirdly formatted URL. I have to extract the contents in ‘()’. Sample URL : http://sampleurl.com/(K(ThinkCode))/profile/view.aspx If I can extract ThinkCode out of it, I will be a happy man! I am having a tough time with regexing special chars like ‘(‘ and ‘/’. Answer Explanation In regex-world, a lookbehind is a way of saying “I want to
How to use `pytest` from Python?
I’m working in a project that recently switched to the pytest unittest framework. I was used to calling my tests from Eclipse, so that I can use the debugger (e.g. placing breakpoints to analyze how a test failure develops). Now this is no longer possible, since the only way to run the tests is via the command line blackbox. Is
Product code looks like abcd2343, how to split by letters and numbers?
I have a list of product codes in a text file, on each line is the product code that looks like: abcd2343 abw34324 abc3243-23A So it is letters followed by numbers and other characters. I want to split on the first occurrence of a number. Answer Or, if you want to split on the first occurrence of a digit: d+
Splicing NumPy arrays
I am having a problem splicing together two arrays. Let’s assume I have two arrays: When I do vstack((a,b)) I get and if I do hstack((a,b)) I get: But what I really want is: How do I accomplish this without using for loops (it needs to be fast)? Answer column_stack.
How to insert a character after every 2 characters in a string
Is there a pythonic way to insert an element into every 2nd element in a string? I have a string: ‘aabbccdd’ and I want the end result to be ‘aa-bb-cc-dd’. I am not sure how I would go about doing that. Answer Assume the string’s length is always an even number, The t can also be eliminated with The algorithm
Numpy and line intersections
How would I use numpy to calculate the intersection between two line segments? In the code I have segment1 = ((x1,y1),(x2,y2)) and segment2 = ((x1,y1),(x2,y2)). Note segment1 does not equal segment2. So in my code I’ve also been calculating the slope and y-intercept, it would be nice if that could be avoided but I don’t know of a way how.