EDIT: made a mistake, see my next comment bellow. I won't update this comment so that other readers can see the code for the 2 use cases.
Python has itertools.product and unpacking for this case:
from itertools import product
space = product(enumerate(table.rows), enumerate(row))
for (r, row), (c, cell) in space:
if search_query.matches(cell.value):
logger.info("Found cell: (%s,%s)", r, c)
return (r, c)
Note that we don't need the "found" variable neither to get the same beheavior. We can also use break + else, but in this case, the return is cleaner since that's the default value for python functions. Additionnally, the logger should do the interpolation, not format().
I think the Python team is having to do the difficult job of filtering new feature, and it makes sense they don't introduce it in this case. We already have all the facilities in the language to deal with most nested loop cases, and those are rare cases anyway.
For this case, one should use a nested comprehension:
space = ((r, c, cell) for r, row in enumerate(table.rows) for c, cell in enumerate(row))
for (r, c, cell) in space:
if search_query.matches(cell.value):
logger.info("Found cell: (%s,%s)", r, c)
return (r, c)
Or see masklinn comment if you don't need the logger.
In all languages, you separate code for preparing your input and code for processing it. In python, it's also a good idea to do that at the loop level:
- prepare your stream of data so that it's normalized using itertools, generators, a function, a numpy array, etc
- do a single loop pass for your processing
This way you will you obtain a single iterable, and will be able to get the most out of the language tooling:
- you can pass it to map/filter, itertools or another comprehension
- you can convert to a list, a tuble, a Counter, etc. And slice it
- you can use "else" in the loop, which is an underused construct, but very useful when you need break
- you can use advanced unpacking
- you can call iter() and/or next(). This is what masklinn does in his example.
The point being, nested loops are rare enough, and we have tool to deal with them in an idiomatic way in Python, and not adding new syntax for a rare case that usually has a good solution makes sense to me.
Nassim Taleb famously destroys those lines of reasoning in his books: game theory is unpractical for most people, because it almost always ignore variables that don't exist in a lab but are crucial IRL.
Comments have been explaining which ones already apply to this article, I'm not going to repeat them.
But there is an another example from Taleb that always makes me smile:
- If the other player tosses a coin and gets 9 tail in a row, what are the chances of getting tail on the next toss?
- when declaring routing in code, it's harder to shoot yourself in the foot. So many php sites expose what they should not, and .httaccess is rarely crafted correctly
- renaming files don't change your website
- coding locally doesn't require apache if your tooling don't need it for routing. No need for stuff like EasyPhp
- python dev servers automatically reload for you on changes anyway
- declaring routes and parsing them at one place to get url id and slug is less duplication
- declaring url yourself mean you can use the language typing system for their checking too (see fastapi)
- I rarely need an entire file for one route endpoint, making 10 files for 10 enpoints that could fit in one is overkill
- wsgi servers like gunicorn will hapilly reload the workers when it receives SIGHUP in prod. No need to restart the master process manually.
- restarting a process every time mean you can't benefit much from nice python features like lru_cache, process/thread pools, connection pools, etc
- python is slow to start, cgi takes a toll
- it's easier to control the number of max workers that run an any given time
Honestly, even when coding in PHP I will use phpfpm (which is one long running process like python) and frameworks like laravel (in which you declare your routing).
I understand the desire for simplicity, and the one-file/one-route declaration is easy to get into. But if I want simple, I do a static website with a bit of client side JS, not need for a backend.
When I do need a backend, I much prefer long running workers and explicit route declaration.
I'd say they are both slow. Try Perl, Tcl, Lua or some other interpreted language that starts fast, and you'll likely see something like 200-300ms for the equivalent commands to above.
In benchmarking scenarios requiring more elaboration than makes sense in an HN comment, I see times as low as 140 microsec per run on that same machine/OS. In fact, as you push things to low overhead extremes, you can easily see the impact of how much environment variable data is in play (`env -i` vs. not).
It is true that Python start-up time is not that bad compared to a "mean diameter of the Internet in milliseconds" which is probably the relevant time scale for CGIs, but it's probably not so small as to be truly negligible, esp. once imports are happening.
Also, Python3 remains quite a bit slower (1.7X slower on that same machine) to start-up than Python2 even after more than a decade of promises to claw back start-up performance.
Meanwhile, on the other side of the comparison, starting up a Julia REPL seems to take quite a bit longer than the Python 16 ms..Like 340ms or 20X longer.
So, the overarching point of this overlong post is just a follow-up/supporting of @tyingq's "compared to what?" with a little more detail.
I've had a mastodon account for years. Not only you can't get a tenth of the audience or quality publications you get on twitters, but also there is less diversity.
A mastodon instance is often its own island, with one view of the world, and they don't connect to any other instances with a different view of the world because the others are intolerant assholes (but themselves are alright, of course). Ironic, given the protocol is made specifically to link instances together so that the network become more robust, and that people joined because it's a more open design than twitter.
On the other hand, Twitter that is full of trends, SJW, extremists of all sides and censorship, manage to send me toward new paths of thinking regularly.
It's like the problem isn't the platform or the technology, but people aren't meant to voice their ideas for the whole world to see, anonymously. It's such a naive view that it's a good idea to attach a megaphone to anyone's mouth.
The focus on decentralisation and not on solving the Greater Internet Fuckwad Theory [1] means they focused on the wrong problem. They've just created an island for jerkwads that think they are better from the other jerkwads at the bird site.
Now, I wouldn't be against a Twitter-like for direct friends. Like Google+ for a moment tried to do before it imploded.
I love the github trending page, but I need only to visit it 10 times a year for it to fullfill its role. Yet I go to github every day.
So visits are not the proper metric to depict how useful it is. Just because I consult a doctor once a year doesn't make this appointment less useful than the pizza truck I go to every week.
This is bad logic. Almost everyone goes to the doctor in a year. And again, what is the importance of a doctor’s office. I once used a security tool that looked at the world in a very narrow sense. Despite being a general purpose tool, it would often flag really bad false-positives like “this security group allows all ingress!”, while applied to zero resources, while completely missing other, real, consequential issues. My point is, you have to pull your head up, look at the real world and the context of your decision, and use your judgement. These aren’t philosophical mind teasers.
It's not bad logic, it's an imperfect analogy which you are taking at face value instead of understanding the point that something can be useful even if infrequently used.
Wether or not it applies at this case is not determined, but it does underline the fact using only visit metrics for this particular page does not give the whole story.
Which is congruent with the reaction the announcement got.
To be clear, I don’t know what telemetry they are using, I only used percentage of users who visited as an example. It does sound like, of the small group of folks who use it, they do enjoy it.
Sounds like GitHub has to find a way to either expand that pool, or decrease the maintenance cost of the feature, or it’s possibly not worth it.
> This is bad logic. Almost everyone goes to the doctor in a year.
They do?
I'm not even making a point about developing countries and access to healthcare, I assume 'everyone' was implicitly 'everyone here' or in developed countries or whatever. But I would be incredibly surprised if that's true here in the UK. Is it an artefact of US health insurance that you can typically claim a 'check-up' per year (so why not even if not needed, you paid for it) or even that it's insisted on for the validity of the policy (ensure you don't have something expensive developing) or something?
> Is it an artefact of US health insurance that you can typically claim a 'check-up' per year (so why not even if not needed, you paid for it)
The point of a yearly checkup is because, at population scale, people can't know if it's needed or not. Tons of issues only present themselves subtly or not at all until it's too late, or just the fact that most people aren't knowledgeable enough to differentiate issues from normal body things. While less advised for younger people, as you get older it's advised to get a yearly blood check to make sure nothing is trending out of balance.
All that being said, I wouldn't be surprised if most people didn't get a yearly checkup in the US either, even thought it's fairly widespread common sense to do so.
Rather than just a “doctor”, think “brain surgeon”. Very few people have brain surgery every year, or ever. Only 0.5% of doctors are neurosurgeons. But brain surgery is an extremely critical service to co tinge providing.
Point is, the relative value of a feature is not necessarily correlated with its popularity.