Discussion:
Triggered By Mediocre Code (Posting On Python-List Prohibited)
(too old to reply)
Lawrence D'Oliveiro
2024-08-25 21:53:31 UTC
Permalink
Looking at this article about the top three languages for getting
programming jobs
<https://www.zdnet.com/article/want-a-programming-job-make-sure-you-learn-these-three-languages/>,
naturally I couldn’t help noticing the code in the screenshot at the
top (my transcription):

bufferedNumber = str(doc.GetTime().GetFrame(docFps))
if len(bufferedNumber)<4:
for x in range(len(bufferedNumber),4):
bufferedNumber = "0" + bufferedNumber

I mean, really? Four lines to do what could be done in a single
expression?

Was that written by a PHP programmer, do you think?
Paul Rubin
2024-08-25 22:07:45 UTC
Permalink
Post by Lawrence D'Oliveiro
I mean, really? Four lines to do what could be done in a single
expression? Was that written by a PHP programmer, do you think?
It is not fluent Python, that's for sure. No idea about PHP though. I
have been wondering about PHP recently. The language is painful but
the implementation has some attractions and it's widely available. So
I've wondered if it might be feasible to have a front end for it similar
to Typescript or Purescript.

Is this what you had in mind?

bufferedNumber = f'{doc.GetTime().GetFrame(docFps):04}'
Lawrence D'Oliveiro
2024-08-25 23:49:48 UTC
Permalink
Post by Paul Rubin
Post by Lawrence D'Oliveiro
I mean, really? Four lines to do what could be done in a single
expression? Was that written by a PHP programmer, do you think?
It is not fluent Python, that's for sure. No idea about PHP though. I
have been wondering about PHP recently. The language is painful but the
implementation has some attractions and it's widely available.
I earn part of my living from it. It’s something I use when I have to
(e.g. writing WordPress plugins). I don’t know what these “attractions”
are that you speak of: Python is at least as widely available, and it does
better than PHP at Web-based programming (supposedly PHP’s bread and
butter) because ASGI-based frameworks allow common handling of WebSocket
connections together with regular HTTP ones, which is more awkward in PHP.
Post by Paul Rubin
Is this what you had in mind?
bufferedNumber = f'{doc.GetTime().GetFrame(docFps):04}'
Printf style makes it explicit that it is an integer, and nothing else
will do:

bufferedNumber = "%0.4d" % doc.GetTime().GetFrame(docFps)

The irony of my putdown is that PHP can do it about as simply. But don’t
expect your typical PHP programmers to know that ...
rbowman
2024-08-26 03:01:18 UTC
Permalink
Post by Lawrence D'Oliveiro
The irony of my putdown is that PHP can do it about as simply. But don’t
expect your typical PHP programmers to know that ...
It has had amazing longevity for something that was born as Personal Home
Page.
Paul Rubin
2024-08-26 20:25:48 UTC
Permalink
Post by Lawrence D'Oliveiro
Printf style makes it explicit that it is an integer, and nothing else
bufferedNumber = "%0.4d" % doc.GetTime().GetFrame(docFps)
bufferedNumber = f'{doc.GetTime().GetFrame(docFps):04d}'

seems to handle it too.

Regarding PHP, many shared hosting vendors offer it without offering
Python. It's easier with PHP to serve many customers with a single PHP
instantiation
Lawrence D'Oliveiro
2024-08-26 21:52:41 UTC
Permalink
Post by Paul Rubin
Post by Lawrence D'Oliveiro
Printf style makes it explicit that it is an integer, and nothing else
bufferedNumber = "%0.4d" % doc.GetTime().GetFrame(docFps)
bufferedNumber = f'{doc.GetTime().GetFrame(docFps):04d}'
seems to handle it too.
Note that “04d” is not quite the same as “0.4d”.
Post by Paul Rubin
Regarding PHP, many shared hosting vendors offer it without offering
Python. It's easier with PHP to serve many customers with a single PHP
instantiation
I’m sure they do. It’s typically offered via the “mod_php” module that
executes within an Apache web server process. Turns out there is an
interesting technical limitation of this, though: it doesn’t handle
WebSockets very well.

Python has ASGI, which offers your choice of Web frameworks founded on
async/await and asyncio. Instead of running as an addon module in the Web
server, the Python code runs in a separate process, with its own
independent flow of control. This is much more versatile.
Paul Rubin
2024-08-26 22:06:03 UTC
Permalink
Post by Lawrence D'Oliveiro
Note that “04d” is not quite the same as “0.4d”.
For non-negative numbers, I think 04d does what the original verbose
code did. 0.4d for an integer isn't valid in an f-string. With
old-style formatting and negative numbers, 0.4d pads differently than
04d, and the original verbose code appears to be plain wrong, formatting
-1 as "00-1" if I'm not mistaken.
Post by Lawrence D'Oliveiro
Python has ASGI, which offers your choice of Web frameworks founded on
async/await and asyncio. Instead of running as an addon module in the Web
server, the Python code runs in a separate process, with its own
independent flow of control. This is much more versatile.
Yes, and the separate processes use more machine resources which is why
low end hosting places prefer PHP.
Lawrence D'Oliveiro
2024-08-27 03:04:52 UTC
Permalink
Post by Paul Rubin
0.4d for an integer isn't valid in an f-string.
I wonder why not?
Post by Paul Rubin
Post by Lawrence D'Oliveiro
Python has ASGI, which offers your choice of Web frameworks founded on
async/await and asyncio. Instead of running as an addon module in the
Web server, the Python code runs in a separate process, with its own
independent flow of control. This is much more versatile.
Yes, and the separate processes use more machine resources which is why
low end hosting places prefer PHP.
Separate PHP apps need separate web server processes, anyway.

You only need one process per Python app. The thing with ASGI is it *can*
handle multiple connections at once, you know.
Paul Rubin
2024-08-27 16:48:49 UTC
Permalink
Post by Lawrence D'Oliveiro
Post by Paul Rubin
0.4d for an integer isn't valid in an f-string.
I wonder why not?
Alternatively I wonder why it is valid in traditional format. I might
check the docs. I have never looked into this before.
Post by Lawrence D'Oliveiro
Separate PHP apps need separate web server processes, anyway.
I had thought the opposite, but I'm not much of a PHP user.
Lawrence D'Oliveiro
2024-08-27 23:40:15 UTC
Permalink
Post by Paul Rubin
Post by Lawrence D'Oliveiro
Separate PHP apps need separate web server processes, anyway.
I had thought the opposite, but I'm not much of a PHP user.
A PHP code page is written to respond to a single HTTP request and return
a single response. So to handle multiple simultaneous requests, you need
multiple instances of that code running. mod_php can’t do that within a
single process (I don’t think), so you need a separate process for each
simultaneous connection. (Of course Web servers have techniques for
keeping and reusing those worker processes, to avoid doing fork(2) calls
all the time.)

With Python ASGI, you control what happens. A single process can handle
multiple connections, if you so choose.
Paul Rubin
2024-08-28 06:01:33 UTC
Permalink
Post by Lawrence D'Oliveiro
With Python ASGI, you control what happens. A single process can
handle multiple connections, if you so choose.
Oh, I see what you mean. Yes you can write an ASGI server without
multitasking, similar to node.js. Ugh. See:



NSFW warning: this video is very funny but contains lots of swearing.
Better use headphones if you view it at the office.
Lawrence D'Oliveiro
2024-08-28 06:54:26 UTC
Permalink
Post by Paul Rubin
Post by Lawrence D'Oliveiro
With Python ASGI, you control what happens. A single process can handle
multiple connections, if you so choose.
Oh, I see what you mean. Yes you can write an ASGI server without
multitasking, similar to node.js. Ugh.
Why “Ugh”? It avoids the overheads of multiple processes (where these are
unnecessary), and the pitfalls of multiple threads.

Piergiorgio Sartor
2024-08-27 17:38:34 UTC
Permalink
Post by Lawrence D'Oliveiro
Looking at this article about the top three languages for getting
programming jobs
<https://www.zdnet.com/article/want-a-programming-job-make-sure-you-learn-these-three-languages/>,
naturally I couldn’t help noticing the code in the screenshot at the
bufferedNumber = str(doc.GetTime().GetFrame(docFps))
bufferedNumber = "0" + bufferedNumber
I mean, really? Four lines to do what could be done in a single
expression?
Was that written by a PHP programmer, do you think?
That the more correct question would be:
What is easier to read? And to debug?
The four line version or the one liner?

To paraphrase someone:
"If the length of a program would be
measured by the time needed to understand
it, some program would be too short to
be short."

Because the world is plenty of one liner
nobody (almost) doesn't understand.

There is even a category in the OCC
(https://www.ioccc.org/).

Don't get me wrong, I like and dislike
one liner.
Namely, I like mine and dislike the others :-)

bye,
--
piergiorgio
Loading...