Hacker Newsnew | past | comments | ask | show | jobs | submit | more BiteCode_dev's commentslogin

The first loop would catch it, it would not reach the next. But you can indeed use any exeption. However, itertools.product does a better job.


Python has had itertools.product for ages for this use case.


product only covers independent nested iterations, some of them have dependencies.

But for those you've got iterools.concat, or generator comprehensions.


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.


Not in this case. 'row' is an item in the 'table.rows' iterable.


Right, I misread the code, my bad.

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.


At that point you might as well make the entire thing into a gencomp inside a `next()` call, the explicit for loop is redundant.


Yes, if you don't need the logger, it is indeed cleaner.


In that case a flatmap does the job fine[0]. In fact you don't even need a flatmap:

    found = next((
        (r, c)
        for r, row in enumerate(table.rows):
        for c, cell in enumerate(row):
        if search_query.matches(cell.value)
    ), None)
Or just return at the location where you found a match. That way you can also provide a better error message in the failure case.

[0] well map + itertools.concat, IIRC the stdlib doesn't have a flatmap per-se, though more_itertools probably does.


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?

- 50%!

- No, 100%. The other player is cheating.


Indeed, but:

- 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.


None of the points you mention are an argument against CGI for me. But it is mostly a matter of taste.

There is one point I would like to address though:

"python is slow to start"

Python is slow. Dead slow. But not to start:

    echo 'print(1)' > test.py; time for i in {1..100}; do python3 test.py > /dev/null; done
Gives me 1.6s

    echo '<?php echo(1);' > test.php; time for i in {1..100}; do php test.php > /dev/null; done
Gives me 1.9s


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.


As in so many measurement/benchmarking situations, "compared to what?" is indeed The Big Question.

For example, you can also compare to a C program not doing much, overhead-wise:

    /usr/bin/time bash -c 'for i in {1..100}; do /bin/true; done'
which gives me like 70 ms for all 100 or 0.7ms per each startup, with also some adjustment to start-up the bash driver shell itself.

Even that 700 microseconds is a high number, from glibc & dynamic linking mostly. If you

    echo 'int main(int ac,char**av){return 0;}' > /tmp/true.c
    musl-gcc -static -O2 /tmp/true.c -o /tmp/true
and repeat, you see 380 microseconds per each.

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.


    echo 'print 1' > test.perl; time for i in {1..100}; do perl test.perl > /dev/null; done
Gives me 0.25s

Clearly the winner!


Just checked my phone:

- audible, work: I can access all previously downloaded books

- bitwarde and andotp: work

- sms, telegram and signal, work: I can access the full history

- countdown, alarm clock, click counter, work

- dynalist, work

- pi music player and vlc, work

- maps.me, organic map and osmand: work, as the maps are downloaded in advance

- firefox & chrome, work: I can access all the app menus and open html files

- podverse, spotify and netflix, work: everything I pre-downloaded is accessible

- twitter app, work. I can see all tweets previously seen and posted today

- pcloud and dropbox, work

- rbnb, work: my history is accessible

- K9 and yandex mail, work: I can access to all my imap inboxes

- oura ring, work: I can access my stats and history

- calendar, calculator, qt scanner, work

- gallery, explorer, home menu, settings: work

What doesn't on my phone:

- gmail, waze

- banks and cryptocoins appp

- stremio, youtube, and discord


It's possible, you just have to accept that people will come and complain. It's not a big deal.


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.

1: https://www.penny-arcade.com/comic/2004/03/19/green-blackboa...


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.


I love the trending tab, but I visit them only 10 times a month. It's enought for me to get the benefit of it.

I suspect lany use it this way. Obviously compared to other tabs, it's going to be very low traffic. But in this case, traffic is a bad metric.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: