Imagine I have a mysql cursor and data read. The amount of data might be very big that I want to deal with one line each time. An easy and straight forward way might be like this: But this doesn’t look good, so I wonder whether this way works as imagined: The thing I want to know is: if I
Tag: python
How to sort collections based on current user locale on a Django site
I need to sort a collection of objects by a utf-8 string property (built via ActiveRecord). Currently the code is sorting by ASCII order via the order_by method, however this needs to be changed to locale.strcoll. Unfortunately using the built in locale functionality requires changing the culture for the enti…
What is the best way to exit a function (which has no return value) in python before the function ends (e.g. a check fails)? [duplicate]
This question already has answers here: return, return None, and no return at all? (5 answers) Closed 4 months ago. Let’s assume an iteration in which we call a function without a return value. The way I think my program should behave is explained in this pseudocode: If I implement this in python, it bo…
Shortcut to comment out multiple lines with Python Tools for Visual Studio
What is the shortcut to comment out multiple lines with Python Tools for Visual Studio? Answer CTRL+K then CTRL+C adds the # in VS for selected lines. CTRL+K then CTRL+U removes the # in VS for selected lines.
How can I put multiple statements in one line?
I know a little bit of comprehensions in Python, but they seem very hard to ‘read’. The way I see it, a comprehension might accomplish the same as the following code: This code is much easier to read than how comprehensions currently work, but I’ve noticed you can’t have two :s in one line. …
How to test the membership of multiple values in a list
I want to test if two or more values have membership on a list, but I’m getting an unexpected result: So, Can Python test the membership of multiple values at once in a list? What does that result mean? See also: How to find list intersection?. Checking whether any of the specified values is in the list…
selecting attribute values from lxml
I want to use an xpath expression to get the value of an attribute. I expected the following to work but this gives an error : Am I wrong to expect this to work? Answer find and findall only implement a subset of XPath. Their presence is meant to provide compatibility with other ElementTree implementations (l…
Equivalent to InnerHTML when using lxml.html to parse HTML
I’m working on a script using lxml.html to parse web pages. I have done a fair bit of BeautifulSoup in my time but am now experimenting with lxml due to its speed. I would like to know what the most sensible way in the library is to do the equivalent of Javascript’s InnerHtml – that is, to r…
Detect last iteration over dictionary.iteritems() in python
Is there a simple way to detect the last iteration while iterating over a dictionary using iteritems()? Answer This is a special case of this broader question. My suggestion was to create an enumerate-like generator that returns -1 on the last item: Add gen = iter(gen) if you want it to handle sequences as we…
How to get a random number between a float range?
randrange(start, stop) only takes integer arguments. So how would I get a random number between two float values? Answer Use random.uniform(a, b):