Re: Why no list as dict key?

2022-04-20 Thread 2QdxY4RzWzUUiLuE
On 2022-04-21 at 06:22:53 +0400,
Abdur-Rahmaan Janhangeer  wrote:

> Maybe hashes should point to an object rather than being the hash of an
> object themselves.
> Maybe the speed drop is not worth it.

Then you have a different problem.

x = [1, 2, 3]
y = [n for n in 1, 2, 3]

Those two lists (x and y) are separate but equal objects.  Being
separate, pointers to them would not be equal.  Being equal, they have
to have the same hash.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Style for docstring

2022-04-22 Thread 2QdxY4RzWzUUiLuE
On 2022-04-22 at 15:35:15 -0500,
"Michael F. Stemper"  wrote:

> On 22/04/2022 14.59, Chris Angelico wrote:
> > On Sat, 23 Apr 2022 at 05:56, Michael F. Stemper
> >  wrote:
> > > 
> > > I'm writing a function that is nearly self-documenting by its name,
> > > but still want to give it a docstring. Which of these would be
> > > best from a stylistic point of view:
> > > 
> > > 
> > > Tells caller whether or not a permutation is even.
> > > 
> > > Determines if a permutation is even. (Alternative is that it's odd.)
> > > 
> > > Returns True if permutation is even, False if it is odd.
> 
> 
> > 
> > I'd go with the third one, but "Return" rather than "Returns". Or
> > possibly "Test whether a permutation is even".
> 
> "So let it be written. So let it be done."

"Test whether a permutation is even," while technically factual, leaves
the reader to wonder what form the result takes, and what happens to
that result.  Yes, we'd all like to think that programmers are smart
enough to *assume* that the function returns the result of the test.
I've also seen functions that perform tests and then print the results
out, or write them to a database, or simply execute the tests for their
side effects (or leave it up to the individual tests to do something
with the result).

Do you want callers of the function also to assume that True means that
the permutation is even?  There are other reasonable strategies, such as
an enumerated type (whose items are Even, Odd, and FileNotFound), or
throwing an exception if the permutation is odd.

I prefer the "return" (rather than "returns") version of the third
option.  Assuming that the programmers are familiar with the domain, the
other two leave out important information.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Style for docstring

2022-04-22 Thread 2QdxY4RzWzUUiLuE
On 2022-04-23 at 08:33:37 +1000,
Chris Angelico  wrote:

> On Sat, 23 Apr 2022 at 08:24, <2qdxy4rzwzuui...@potatochowder.com> wrote:
> >
> > On 2022-04-22 at 15:35:15 -0500,
> > "Michael F. Stemper"  wrote:
> >
> > > On 22/04/2022 14.59, Chris Angelico wrote:
> > > > On Sat, 23 Apr 2022 at 05:56, Michael F. Stemper
> > > >  wrote:
> > > > >
> > > > > I'm writing a function that is nearly self-documenting by its name,
> > > > > but still want to give it a docstring. Which of these would be
> > > > > best from a stylistic point of view:
> > > > >
> > > > >
> > > > > Tells caller whether or not a permutation is even.
> > > > >
> > > > > Determines if a permutation is even. (Alternative is that it's 
> > > > > odd.)
> > > > >
> > > > > Returns True if permutation is even, False if it is odd.
> > >
> > >
> > > >
> > > > I'd go with the third one, but "Return" rather than "Returns". Or
> > > > possibly "Test whether a permutation is even".
> > >
> > > "So let it be written. So let it be done."
> >
> > "Test whether a permutation is even," while technically factual, leaves
> > the reader to wonder what form the result takes, and what happens to
> > that result.
> 
> While it's definitely possible to have other results and other ways to
> deliver them, the return of a boolean would be the most obvious
> default.

Maybe, depending on the context and purpose of the application.

> > Do you want callers of the function also to assume that True means that
> > the permutation is even?  There are other reasonable strategies, such as
> > an enumerated type (whose items are Even, Odd, and FileNotFound), or
> > throwing an exception if the permutation is odd.
> 
> I'm assuming that the function is called something like "is_even()"
> and that it either is a method on a permutation object, or its
> parameters make it very clear what the permutation is.
> 
> If it returns an enumeration, I would say that in the docstring. If
> the docstring doesn't say, I would assume it returns True or False.

I think we're agreeing, but the OP didn't provide that information.
I've seen enough oddball (yes, that's my professional opinion :-)) APIs
(and worked with enough programmers from enough backgrounds) to know
that "most obvious default" is subjective.

> > I prefer the "return" (rather than "returns") version of the third
> > option.  Assuming that the programmers are familiar with the domain,
> > the other two leave out important information.

[...]

> We don't need to say "Figures out algorithmically whether the
> permutation is even. If it is, will return True; if it isn't, will
> return False; if something goes wrong, will raise an exception". This
> is Python; we know that if something goes wrong, an exception is
> raised. (Though it can help to say WHICH exception will be raised
> under WHAT circumstances). Some things are obvious.

No, we don't need to say it that way.  I believe that we do need to say
what the function returns under what circumstances.

If, in fact, the function in question is named is_permutation_even, then
it *is* as simple as the OP's third option, but not simpler.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python/New/Learn

2022-05-06 Thread 2QdxY4RzWzUUiLuE
On 2022-05-05 at 16:51:49 -0700,
Grant Edwards  wrote:

> On 2022-05-05, Mats Wichmann  wrote:
> 
> > Without having any data at all on it, just my impressions, more
> > people these days learn from in-person or video experiences.
> 
> I've always been utterly baffled by video tutorials for
> programming. There must be people who prefer that format, but it seems
> like absolutely the worst possible option for me. You can't cut/paste
> snippets from the examples. You have to constantly pause them so you
> can try out examples. Sometimes it's not even easy to read the
> examples. Perhaps if there was an accompanying web page or PDF...

+1 (maybe more), except that an accompanying web page or PDF only solves
the problem of copying/pasting examples badly, at the expense of the
cognitive load to keep track of one more thing (because it's highly
unlikely that the web page or PDF tracks the video "automatically").

As far as easy-to-read examples go, writing them down doesn't always
help.  One of my physics textbooks used upsilon and nu to describe some
phenomenon related to lasers.  IIRC, the text, the math, and the physics
were pretty straightforward, until you looked at the fraction υ/ν in
something resembling Times Roman Italic (although, to be fair, once you
got that far, it was pretty obvious that it was upsilon over nu rather
than nu over upsilon).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tail

2022-05-09 Thread 2QdxY4RzWzUUiLuE
On 2022-05-08 at 18:52:42 +,
Stefan Ram  wrote:

>   Remember how recently people here talked about how you cannot copy
>   text from a video? Then, how did I do it? Turns out, for my
>   operating system, there's a screen OCR program! So I did this OCR
>   and then manually corrected a few wrong characters, and was done!

When you're learning, and the example you tried doesn't work like it
worked on the video, you probably don't know what's wrong, let alone how
to correct it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Changing calling sequence

2022-05-11 Thread 2QdxY4RzWzUUiLuE
On 2022-05-11 at 08:33:27 -0500,
"Michael F. Stemper"  wrote:

> I have a function that I use to retrieve daily data from a
> home-brew database. Its calling sequence is;
> 
> def TempsOneDay( year, month, date ):
> 
> After using it (and its friends) for a few years, I've come to
> realize that there are times where it would be advantageous to
> invoke it with a datetime.date as its single argument.
> 
> As far as I can tell, there are three ways for me to proceed:
> 1. Write a similar function that takes a single datetime.date
>as its argument.
> 2. Rewrite the existing function so that it takes a single
>argument, which can be either a tuple of (year,month,date)
>or a datetime.date argument.
> 3. Rewrite the existing function so that its first argument
>can be either an int (for year) or a datetime.date. The
>existing month and date arguments would be optional, with
>default=None. But, if the first argument is an int, and
>either of month or date is None, an error would be raised.
> 
> The first would be the simplest. However, it is obviously WET
> rather than DRY.

It's also the least disruptive to existing code and tests, and the most
clear to readers (whether or not they're familiar with said existing
code).

What pieces, exactly, do you think you would repeat, especially after
you extract the common logic into a new function that should be simpler
than either API-level function.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tail

2022-05-13 Thread 2QdxY4RzWzUUiLuE
On 2022-05-13 at 12:16:57 +0200,
Marco Sulla  wrote:

> On Fri, 13 May 2022 at 00:31, Cameron Simpson  wrote:

[...]

> > This is nearly the worst "specification" I have ever seen.

> You're lucky. I've seen much worse (or no one).

At least with *no* documentation, the source code stands for itself.  If
I can execute it (whatever that entails), then I can (in theory) figure
out *what* it does.  I still don't what it's *supposed* to do, and
therefore *cannot* know how well it does or doesn't "work,", but at
least source code is deterministic and unambiguous (except when it
isn't, but let's not go there).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Changing calling sequence

2022-05-14 Thread 2QdxY4RzWzUUiLuE
On 2022-05-15 at 10:22:15 +1200,
dn  wrote:

> That said, a function which starts with a list of ifs-buts-and-maybes*
> which are only there to ascertain which set of arguments have been
> provided by the calling-routine; obscures the purpose/responsibility
> of the function and decreases its readability (perhaps not by much,
> but varying by situation).

Agreed.

> Accordingly, if the function is actually a method, recommend following
> @Stefan's approach, ie multiple-constructors. Although, this too can
> result in lower readability.

(Having proposed that approach myself (and having used it over the
decades for functions, methods, procedures, constructors, ...), I also
agree.)

Assuming good names,¹ how can this lead to lower readability?  I guess
if there's too many of them, or programmers have to start wondering
which one to use?  Or is this in the same generally obfuscating category
as the ifs-buts-and-maybes at the start of a function?

¹ and properly invalidated caches
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Changing calling sequence

2022-05-15 Thread 2QdxY4RzWzUUiLuE
On 2022-05-15 at 14:44:09 +1000,
Chris Angelico  wrote:

> On Sun, 15 May 2022 at 14:27, dn  wrote:
> >
> > On 15/05/2022 11.34, 2qdxy4rzwzuui...@potatochowder.com wrote:
> > > On 2022-05-15 at 10:22:15 +1200,
> > > dn  wrote:
> > >
> > >> That said, a function which starts with a list of ifs-buts-and-maybes*
> > >> which are only there to ascertain which set of arguments have been
> > >> provided by the calling-routine; obscures the purpose/responsibility
> > >> of the function and decreases its readability (perhaps not by much,
> > >> but varying by situation).
> > >
> > > Agreed.
> > >
> > >> Accordingly, if the function is actually a method, recommend following
> > >> @Stefan's approach, ie multiple-constructors. Although, this too can
> > >> result in lower readability.
> > >
> > > (Having proposed that approach myself (and having used it over the
> > > decades for functions, methods, procedures, constructors, ...), I also
> > > agree.)
> > >
> > > Assuming good names,¹ how can this lead to lower readability?  I guess
> > > if there's too many of them, or programmers have to start wondering
> > > which one to use?  Or is this in the same generally obfuscating category
> > > as the ifs-buts-and-maybes at the start of a function?
> > >
> > > ¹ and properly invalidated caches
> >
> > Allow me to extend the term "readability" to include "comprehension".
> > Then add the statistical expectation that a class has only __init__().

Aha.  In that light, yeah, in geeral, the more stuff there is, the
harder it is to get your head around it.  And even if I document the
class (or the module), no one makes the time to read (let alone
comprehend) the document, which *should* clarify all those things that
are hard to discern from the code itself.

> > Thus, assuming this is the first time (or, ... for a while) that the
> > class is being employed, one has to read much further to realise that
> > there are choices of constructor.
> 
> Yeah. I would generally say, though, that any classmethod should be
> looked at as a potential alternate constructor, or at least an
> alternate way to obtain objects (eg preconstructed objects with
> commonly-used configuration - imagine a SecuritySettings class with a
> classmethod to get different defaults).

I think opening up the class and sifting through its classmethods to
find the factory functions is what dn is talking about.  Such a design
also means that once I have a SecuritySettings object, its (the
instance's) methods include both instance and class level methods.  IMO,
classmethods were/are a bad idea (yes, I'm probably in the minority
around here, but someone has to be).  The first person to scream "but
discoverability" will be severely beaten with a soft cushion.

> > Borrowing from the earlier example:
> >
> > >   This would be quite pythonic. For example, "datetime.date"
> > >   has .fromtimestamp(timestamp), .fromordinal(ordinal),
> > >   .fromisoformat(date_string), ...
> >
> > Please remember that this is only relevant if the function is actually a
> > module - which sense does not appear from the OP (IMHO).

Note that datetime.date is a class, not a module.

> > The alternatives' names are well differentiated and (apparently#)
> > appropriately named*.

[...]

> > Continuing the 'have to read further' criticism (above), it could
> > equally-well be applied to my preference for keyword-arguments, in that
> > I've suggested defining four parameters but the user will only call the
> > function with either three or one argument(s). Could this be described
> > as potentially-confusing?

Potentially.  :-)

In a well designed *library*, common keywords across multiple functions
provide consistency, which is generally good.  Even a bit of redundancy
can be good for the same reason.

OTOH, when there's only one function, and it has a pile of keyword
parameters that can only be used in certain combinations, then it
definitely can be harder to read/understand/use than separate functions
with simpler interfaces.

> Yes, definitely. Personally, I'd split it into two, one that takes the
> existing three arguments (preferably with the same name, for
> compatibility), and one with a different name that takes just the one
> arg. That could be a small wrapper that calls the original, or the
> original could become a wrapper that calls the new one, or the main
> body could be refactored into a helper that they both call. It all
> depends what makes the most sense internally, because that's not part
> of the API at that point.
> 
> But it does depend on how the callers operate. Sometimes it's easier
> to have a single function with switchable argument forms, other times
> it's cleaner to separate them.

"Easier" and "cleaner" are very often orthogonal.  ;-)  (Rich Hickey
(creator of Clojure) talks a lot about the difference between "easy" and
"simple."  Arguemnts for and against Unix often involve similar terms.)

And "easier" or "cleaner" for whom?  The person writing the function(s)

Re: How to test characters of a string

2022-06-07 Thread 2QdxY4RzWzUUiLuE
On 2022-06-07 at 21:35:43 +0200,
Dave  wrote:

> I’m new to Python and have a simple problem that I can’t seem to find
> the answer.

> I want to test the first two characters of a string to check if the
> are numeric (00 to 99) and if so remove the fist three chars from the
> string.

> Example: if “05 Trinket” I want “Trinket”, but “Trinket” I still want
> “Trinket”. I can’t for the life of work out how to do it in Python?

How would you do it without Python?

Given that if the string is called x, then x[y] is the y'th character
(where what you would call "the first character," Python calls "the
zeroth character"), describe the steps you would take *as a person* (or
in some other programming language, if you know one) to carry out this
task.

Translating that algorithm to Python is the next step.  Perhaps
https://docs.python.org/3/library/string.html can help.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread 2QdxY4RzWzUUiLuE
On 2022-06-08 at 07:29:03 +1000,
Chris Angelico  wrote:

> On Wed, 8 Jun 2022 at 07:24, Barry  wrote:
> >
> >
> >
> > > On 7 Jun 2022, at 22:04, Dave  wrote:
> > >
> > > It depends on the language I’m using, in Objective C, I’d use isNumeric, 
> > > just wanted to know what the equivalent is in Python.
> > >
> > > If you know the answer why don’t you just tell me and if you don’t, don’t 
> > > post!
> >
> > People ask home work questions here and we try to teach a student with 
> > hints not finished answers.
> > Your post was confused with a home work question.
> >
> 
> In the future, to make it look less like a homework question, show
> your current code, which would provide context. Last I checked,
> homework questions don't usually involve ID3 tags in MP3 files :)

The original question in this thread didn't say anything about MP3
files.  Jumping to that conclusion from strings like '05 Trinket' was
left as an exercise for the interested reader.  :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread 2QdxY4RzWzUUiLuE
On 2022-06-07 at 23:07:42 +0100,
Regarding "Re: How to test characters of a string,"
MRAB  wrote:

> On 2022-06-07 21:23, Dave wrote:
> > Thanks a lot for this! isDigit was the method I was looking for and 
> > couldn’t find.
> > 
> > I have another problem related to this, the following code uses the code 
> > you just sent. I am getting a files ID3 tags using eyed3, this part seems 
> > to work and I get expected values in this case myTitleName (Track name) is 
> > set to “Deadlock Holiday” and myCompareFileName is set to “01 Deadlock 
> > Holiday” (File Name with the Track number prepended). The is digit test 
> > works and myCompareFileName is set to  “Deadlock Holiday”, so they should 
> > match, right?
> > 
> OT, but are you sure about that name? Isn't it "Dreadlock Holiday" (by
> 10cc)?

Edsger Dijkstra originally wrote Deadlock Holiday for his band, The
Semaphores.  10cc lost the race condition and had to change the lyrics.

Sorry.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-08 Thread 2QdxY4RzWzUUiLuE
On 2022-06-08 at 08:07:40 -,
De ongekruisigde  wrote:

> Depending on the problem a regular expression may be the much simpler
> solution. I love them for e.g. text parsing and use them all the time.
> Unrivaled when e.g. parts of text have to be extracted, e.g. from lines
> like these:
> 
>   root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash
>   dhcpcd:x:995:991::/var/empty:/run/current-system/sw/bin/nologin
>   nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin
>   avahi:x:997:996:avahi-daemon privilege separation 
> user:/var/empty:/run/current-system/sw/bin/nologin
>   sshd:x:998:993:SSH privilege separation 
> user:/var/empty:/run/current-system/sw/bin/nologin
>   geoclue:x:999:998:Geoinformation 
> service:/var/lib/geoclue:/run/current-system/sw/bin/nologin
> 
> Compare a regexp solution like this:
> 
>   >>> g = re.search(r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$' , s)
>   >>> print(g.groups())
>   ('geoclue', 'x', '999', '998', 'Geoinformation service', 
> '/var/lib/geoclue', '/run/current-system/sw/bin/nologin')
> 
> to the code one would require to process it manually, with all the edge
> cases. The regexp surely reads much simpler (?).

Uh...

>>> import pwd # https://docs.python.org/3/library/pwd.html
>>> [x for x in pwd.getpwall() if x[0] == 'geoclue']
[pwd.struct_passwd(pw_name='geoclue', pw_passwd='x', pw_uid=992, 
pw_gid=992, pw_gecos='Geoinformation service', pw_dir='/var/lib/geoclue', 
pw_shell='/sbin/nologin')]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-08 Thread 2QdxY4RzWzUUiLuE
On 2022-06-09 at 03:18:56 +1000,
Chris Angelico  wrote:

> On Thu, 9 Jun 2022 at 03:15, <2qdxy4rzwzuui...@potatochowder.com> wrote:
> >
> > On 2022-06-08 at 08:07:40 -,
> > De ongekruisigde  wrote:
> >
> > > Depending on the problem a regular expression may be the much simpler
> > > solution. I love them for e.g. text parsing and use them all the time.
> > > Unrivaled when e.g. parts of text have to be extracted, e.g. from lines
> > > like these:
> > >
> > >   root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash
> > >   dhcpcd:x:995:991::/var/empty:/run/current-system/sw/bin/nologin
> > >   nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin
> > >   avahi:x:997:996:avahi-daemon privilege separation 
> > > user:/var/empty:/run/current-system/sw/bin/nologin
> > >   sshd:x:998:993:SSH privilege separation 
> > > user:/var/empty:/run/current-system/sw/bin/nologin
> > >   geoclue:x:999:998:Geoinformation 
> > > service:/var/lib/geoclue:/run/current-system/sw/bin/nologin
> > >
> > > Compare a regexp solution like this:
> > >
> > >   >>> g = re.search(r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$' 
> > > , s)
> > >   >>> print(g.groups())
> > >   ('geoclue', 'x', '999', '998', 'Geoinformation service', 
> > > '/var/lib/geoclue', '/run/current-system/sw/bin/nologin')
> > >
> > > to the code one would require to process it manually, with all the edge
> > > cases. The regexp surely reads much simpler (?).
> >
> > Uh...
> >
> > >>> import pwd # https://docs.python.org/3/library/pwd.html
> > >>> [x for x in pwd.getpwall() if x[0] == 'geoclue']
> > [pwd.struct_passwd(pw_name='geoclue', pw_passwd='x', pw_uid=992, 
> > pw_gid=992, pw_gecos='Geoinformation service', pw_dir='/var/lib/geoclue', 
> > pw_shell='/sbin/nologin')]
> 
> That's great if the lines are specifically coming from your system's
> own /etc/passwd, but not so much if you're trying to compare passwd
> files from different systems, where you simply have the files
> themselves.

In addition to pwent to get specific entries from the local password
database, POSIX has fpwent to get a specific entry from a stream that
looks like /etc/passwd.  So even POSIX agrees that if you think you have
to process this data manually, you're doing it wrong.  Python exposes
neither functon directly (at least not in the pwd module or the os
module; I didn't dig around or check PyPI).

IMO, higher level functions to process such data is way better than a
[insert your own adjective/expletive here] regular expression that
collects the pieces into numbered groups rather than labeled fields.
Readability counts.

Yes, absolutely, use a regular expression when all else fails.  Don't
forget to handle all the edge cases!  (I assume that sane OSes preclude
colons in paths that are likely to come up in the local password
database, but I don't know what happens, e.g., when there's a reason for
GECOS to contain a colon.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-08 Thread 2QdxY4RzWzUUiLuE
On 2022-06-09 at 04:15:46 +1000,
Chris Angelico  wrote:

> On Thu, 9 Jun 2022 at 04:14, <2qdxy4rzwzuui...@potatochowder.com> wrote:
> >
> > On 2022-06-09 at 03:18:56 +1000,
> > Chris Angelico  wrote:
> >
> > > On Thu, 9 Jun 2022 at 03:15, <2qdxy4rzwzuui...@potatochowder.com> wrote:
> > > >
> > > > On 2022-06-08 at 08:07:40 -,
> > > > De ongekruisigde  wrote:
> > > >
> > > > > Depending on the problem a regular expression may be the much simpler
> > > > > solution. I love them for e.g. text parsing and use them all the time.
> > > > > Unrivaled when e.g. parts of text have to be extracted, e.g. from 
> > > > > lines
> > > > > like these:
> > > > >
> > > > >   root:x:0:0:System 
> > > > > administrator:/root:/run/current-system/sw/bin/bash
> > > > >   dhcpcd:x:995:991::/var/empty:/run/current-system/sw/bin/nologin
> > > > >   nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin
> > > > >   avahi:x:997:996:avahi-daemon privilege separation 
> > > > > user:/var/empty:/run/current-system/sw/bin/nologin
> > > > >   sshd:x:998:993:SSH privilege separation 
> > > > > user:/var/empty:/run/current-system/sw/bin/nologin
> > > > >   geoclue:x:999:998:Geoinformation 
> > > > > service:/var/lib/geoclue:/run/current-system/sw/bin/nologin
> > > > >
> > > > > Compare a regexp solution like this:
> > > > >
> > > > >   >>> g = 
> > > > > re.search(r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$' , s)
> > > > >   >>> print(g.groups())
> > > > >   ('geoclue', 'x', '999', '998', 'Geoinformation service', 
> > > > > '/var/lib/geoclue', '/run/current-system/sw/bin/nologin')
> > > > >
> > > > > to the code one would require to process it manually, with all the 
> > > > > edge
> > > > > cases. The regexp surely reads much simpler (?).
> > > >
> > > > Uh...
> > > >
> > > > >>> import pwd # https://docs.python.org/3/library/pwd.html
> > > > >>> [x for x in pwd.getpwall() if x[0] == 'geoclue']
> > > > [pwd.struct_passwd(pw_name='geoclue', pw_passwd='x', pw_uid=992, 
> > > > pw_gid=992, pw_gecos='Geoinformation service', 
> > > > pw_dir='/var/lib/geoclue', pw_shell='/sbin/nologin')]
> > >
> > > That's great if the lines are specifically coming from your system's
> > > own /etc/passwd, but not so much if you're trying to compare passwd
> > > files from different systems, where you simply have the files
> > > themselves.
> >
> > In addition to pwent to get specific entries from the local password
> > database, POSIX has fpwent to get a specific entry from a stream that
> > looks like /etc/passwd.  So even POSIX agrees that if you think you have
> > to process this data manually, you're doing it wrong.  Python exposes
> > neither functon directly (at least not in the pwd module or the os
> > module; I didn't dig around or check PyPI).
> 
> So.. we can go find some other way of calling fpwent, or we can
> just parse the file ourselves. It's a very VERY simple format.

If you insist:

>>> s = 'nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin'
>>> print(s.split(':'))
['nm-iodine', 'x', '996', '57', '', '/var/empty', 
'/run/current-system/sw/bin/nologin']

Hesitantly, because this is the Python mailing list, I claim (a) ':' is
simpler than r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$', and
(b) string.split covers pretty much the same edge cases as re.search.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ModuleNotFoundError

2022-06-17 Thread 2QdxY4RzWzUUiLuE
On 2022-06-17 at 08:03:28 +,
Zoltan Szenderak  wrote:

> How do I reply to: Chris Angelico rosuav at gmail.com so it is listed
> on the Python list?

Please don't.  Please continue replying to python-list@python.org; that
way, other people can help you, and future programmers can find their
issues in the public archives.  ChrisA is a regular on this list, and
will see your replies on it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: "CPython"

2022-06-21 Thread 2QdxY4RzWzUUiLuE
On 2022-06-21 at 17:04:45 +,
Avi Gross via Python-list  wrote:

> My problem with that idea is, believe it or not, that it is too negative. 
> What you meant to be seen as a dash is a minus sign to me. And both C and C++ 
> not only have both a pre and post autoincrement variable using ++x and x++, 
> they also have autodecrement operators using a minus sign such as --x and x-- 
> and it can get pretty weird trying to figure out if some code is legal, let 
> alone what it does, without parentheses. I mean what the heck does this do?
> 
> y = x++-++x

That code evokes (or at least can evoke) nasal demons.

https://en.wikipedia.org/wiki/Undefined_behavior
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: calculate diff between dates

2022-07-12 Thread 2QdxY4RzWzUUiLuE
On 2022-07-12 at 15:37:55 +0300,
נתי שטרן  wrote:

> I glad for any help

http://www.sscce.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Object in List : how?

2022-07-24 Thread 2QdxY4RzWzUUiLuE
On 2022-07-24 at 19:47:38 -0500,
Khairil Sitanggang  wrote:

> Regarding [Peter Otten's] comment : "
> *However, usually object creation and initialization iscombined by allowing
> arguments to the initializer:*" , so which one of the two classes Node1,
> Node2 below is more common in practice? Option 2, I guess.

No.  Please use option 1.

Another option would be to expose b as an optional parameter with a
default value:

class Node:
def __init__(self, a, b=0):
self.a = a
self.b = self.calculation() if b == 0 else b

There are other ways to write that particular assignment to b (because
the default is 0), but the relevant concept for right now is that
callers *can* supply a value for b, but that they don't *have* to:

n1 = Node(a) # uses a default value for b
n2 = Node(a, 22) # overrides the default; use 22 instead

Designing APIs can be tricky, and it's not an exact science.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: More efficient code, but slower program

2022-07-27 Thread 2QdxY4RzWzUUiLuE
On 2022-07-27 at 17:48:47 +0200,
Regarding "Re: More efficient code, but slower program,"
Cecil Westerhof via Python-list  wrote:

> r...@zedat.fu-berlin.de (Stefan Ram) writes:
> 
> > Cecil Westerhof  writes:
> >>values = [*range(100)]
> >
> >   In many cases, any iterable is just fine and a list is not
> >   required, just as peudo-random numbers often are just fine and
> >   real-world entropy is not required.
> 
> In this case both are. I must select (several times) a random element
> from the list. So I need the list.
> I also want the randomness to be as good as possible to make the
> 'simulation' as good as possible.

"[A]s good as possible" for simulations and tests may not require the
cryptographic quality numbers from SystemRandom.  Many/most pseudo
random number generators are optimized for statistically normalized
outputs, and are repeatable as a bonus (again, often a requirement for
certain types of simulations; YMMV).

Also, what if you shuffled the list first (e.g., with random.shuffle)
and then iterated through it directly?  Repeatedly removing arbitrary
elements from the middle of a list is potentially expensive.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: mailbox misbehavior with non-ASCII

2022-07-29 Thread 2QdxY4RzWzUUiLuE
On 2022-07-29 at 23:24:57 +,
Peter Pearson  wrote:

> The following code produces a nonsense result with the input 
> described below:
> 
> import mailbox
> box = mailbox.Maildir("/home/peter/Temp/temp",create=False)
> x = box.values()[0]
> h = x.get("X-DSPAM-Factors")
> print(type(h))
> # 
> 
> The output is the desired "str" when the message file contains this:
> 
> To: recipi...@example.com
> Message-ID: <123>
> Date: Sun, 24 Jul 2022 15:31:19 +
> Subject: Blah blah
> From: f...@from.com
> X-DSPAM-Factors: a'b
> 
> xxx
> 
> ... but if the apostrophe in "a'b" is replaced with a
> RIGHT SINGLE QUOTATION MARK, the returned h is of type 
> "email.header.Header", and seems to contain inscrutable garbage.
> 
> I realize that one should not put non-ASCII characters in
> message headers, but of course I didn't put it there, it
> just showed up, pretty much beyond my control.  And I realize
> that when software is given input that breaks the rules, one
> cannot expect optimal results, but I'd think an exception
> would be the right answer.

Be strict in what you send, but generous is what you receive.

I agree that email headers are supposed to be ASCII (RFC 822, 2822, and
now 5322) all say that, but always throwing an exception seems a little
harsh, and arguably (I'm not arguing for or against) breaks backwards
compatibility.  At least let the exception contain, in its own
attribute, the inscrutable garbage after the space after the colon and
before next CR/LF pair.

> Is this worth a bug report?

If nothing else, the documentation could specify or disclaim the
existing behavior.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Tkinter not working

2022-08-01 Thread 2QdxY4RzWzUUiLuE
On 2022-08-01 at 18:53:47 +0100,
Matthew Barnett  wrote:

> On 01/08/2022 13:17, Daniel Lee wrote:
> > Hello, I my code with tkinter was working before, and now, it has many 
> > errors in it. I’m not sure what has happened. The results after running are 
> > below:
> > 
> > "D:\Python Projects\tes\venv\Scripts\python.exe" "D:/Python 
> > Projects/tes/main.py"
> > Traceback (most recent call last):
> >File "D:\Python Projects\tes\main.py", line 1, in 
> >  import tkinter as tk
> >File 
> > "C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py",
> >  line 3, in 
> >  import tkinter.messagebox
> >File 
> > "C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\messagebox.py",
> >  line 25, in 
> >  from tkinter.commondialog import Dialog
> >File 
> > "C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\commondialog.py",
> >  line 13, in 
> >  from tkinter import Frame, _get_temp_root, _destroy_temp_root
> > ImportError: cannot import name 'Frame' from partially initialized module 
> > 'tkinter' (most likely due to a circular import) 
> > (C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py)
> > 
> > Process finished with exit code 1
> 
> The entry:
> 
>   File 
> "C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py",
> line 3, in 
> import tkinter.messagebox
> 
> in the traceback does not look right to me. On my PC that file starts with a
> long docstring.

My tkinter/__init__.py, too.

Do you have (and did you create since the last time your code worked)
your own module called tkinter?  Is line 3 "import tkinter.messagebox"?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Dictionary order?

2022-08-01 Thread 2QdxY4RzWzUUiLuE
On 2022-08-01 at 13:41:11 -0700,
Dan Stromberg  wrote:

> keys = [5, 10, 15, 14, 9, 4, 1, 2, 8, 6, 7, 12, 11]
> 
> dict_ = {}
> for key in keys:
> dict_[key] = 1

$ python
Python 3.10.5 (main, Jun  6 2022, 18:49:26) [GCC 12.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> [hash(x) for x in range(20)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Just sayin'.  :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Dictionary order?

2022-08-01 Thread 2QdxY4RzWzUUiLuE
On 2022-08-02 at 07:50:52 +1000,
Chris Angelico  wrote:

> On Tue, 2 Aug 2022 at 07:48, <2qdxy4rzwzuui...@potatochowder.com> wrote:
> >
> > On 2022-08-01 at 13:41:11 -0700,
> > Dan Stromberg  wrote:
> >
> > > keys = [5, 10, 15, 14, 9, 4, 1, 2, 8, 6, 7, 12, 11]
> > >
> > > dict_ = {}
> > > for key in keys:
> > > dict_[key] = 1
> >
> > $ python
> > Python 3.10.5 (main, Jun  6 2022, 18:49:26) [GCC 12.1.0] on linux
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> [hash(x) for x in range(20)]
> > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
> >
> > Just sayin'.  :-)
> 
> Yes, but I'm pretty sure that's been true for a LONG time. The hashes
> for small integers have been themselves for as long as I can remember.
> But the behaviour of the dictionary, when fed such keys, is what's
> changed.

I'm not disputing either of those facts.  I'm pointing out that the
apparently arbitrary order of a mapping's keys becomes obvious when you
look at the hashes of those keys.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Register multiple excepthooks?

2022-08-04 Thread 2QdxY4RzWzUUiLuE
On 2022-08-04 at 09:41:00 +0200,
Albert-Jan Roskam  wrote:

>Thank you both. I'll give this a try. I think it would be nice if
>the standard library function atexit.register would be improved,
>such that the registered functions would not only be called upon
>(a) normal program termination, but that one could also register
>functions that are called (b) upon error (c) unconditionally. Much
>like (a) try - (b) except - (c) finally.

There.  You've just designed the top level of a better behaved, more
robust application (completely untested):

on_normal_exit_funcs = list()
on_error_exit_funcs = list()
on_any_exit_funcs = list()

def run_exit_funcs(exit_funcs):
for func in exit_funcs:
try: func()
except e: maybe_do_some_logging(e)

try:
run_the_application()
run_exit_funcs(on_normal_exit_funcs)
except:
run_exit_funcs(on_error_exit_funcs)
finally:
run_exit_funcs(on_any_exit_funcs)

def register_normal_exit_func(f):
on_normal_exit_funcs.append(f)

def register_error_exit_func(f):
on_error_exit_funcs.append(f)

def register_any_exit_func(f):
on_any_exit_funcs.append(f)

No, really.  There are worse ways to build an extremely generic, fairly
minimalist application framework.

Season to taste,¹ add it to your personal (or company) toolbox, and
refine and improve it as things come up.  You may discover some number
of common exit functions that are useful across appliations, too.

¹ are you object oriented, functional, imperative, or something else?
do you like long names, short names, names that follow some existing
coding standard, non-English names? do you have a standardized
logging/exception library?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Parallel(?) programming with python

2022-08-10 Thread 2QdxY4RzWzUUiLuE
On 2022-08-09 at 17:04:51 +,
"Schachner, Joseph (US)"  wrote:

> Why would this application *require* parallel programming?  This could
> be done in one, single thread program.  Call time to get time and save
> it as start_time.  Keep a count of the number of 6 hour intervals,
> initialize it to 0.

In theory, you are correct.

In practice, [stuff] happens.  What if your program crashes?  Or the
computer crashes?  Or there's a Python update?  Or an OS update?  Where
does all that pending data go, and how will you recover it after you've
addressed whatever happened? ¹

OTOH, once you start writing the pending data to a file, then it's an
extremely simple leap to multiple programs (rather than multiple
threads) for all kinds of good reasons.

¹ FWIW, I used to develop highly available systems, such as telephone
switches, which allow [stuff] to happen, and yet continue to function.
It's pretty cool to yank a board (yes, physically remove it, without
warning) from the system without [apparently] disrupting anything.  Such
systems also allow for hardware, OS, and application upgrades, too
(IIRC, we were allowed a handful of seconds of downtime per year to meet
our availability requirements).  That said, designing and building such
a system for the sakes of simplicity and convenience of the application
we're talking about here would make a pretty good definition of
"overkill."
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Mutating an HTML file with BeautifulSoup

2022-08-19 Thread 2QdxY4RzWzUUiLuE
On 2022-08-19 at 20:12:35 +0100,
Barry  wrote:

> > On 19 Aug 2022, at 19:33, Chris Angelico  wrote:
> > 
> > What's the best way to precisely reconstruct an HTML file after
> > parsing it with BeautifulSoup?
> 
> I recall that in bs4 it parses into an object tree and loses the
> detail of the input.  I recently ported from very old bs to bs4 and
> hit the same issue.  So no it will not output the same as went in.
> 
> If you can trust the input to be parsed as xml, meaning all the rules
> of closing tags have been followed. Then I think you can parse and
> unparse thru xml to do what you want.

XML is in the same boat.  Except for "canonical form" (which underlies
cryptographically signed XML documents) the standards explicitly don't
require tools to round-trip the "source code."  The preferred method of
comparing XML documents is at the structural level rather than with
textual representations.  That way, the following two elements are the
same (and similar with a collection of sub-elements in a different order
in another document):



and



Dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to replace an instance method?

2022-09-19 Thread 2QdxY4RzWzUUiLuE
On 2022-09-18 at 09:11:28 +,
Stefan Ram  wrote:

> r...@zedat.fu-berlin.de (Stefan Ram) writes (abbreviated):
> >types.MethodType( function, instance )
> >functools.partial( function, instance )
> >new_method.__get__( instance )
> 
>   I wonder which of these three possibilities expresses
>   the idea of creating a new method from a function and
>   an instance most clearly.

The first one.  And only the first one.

The second one requires too much inside knowledge of Python to make the
leap from currying to instance method.

The third one doesn't even mention the function.  Also, in Python, if
I'm applying dunder methods directly (it's okay to write them), then I'm
doing something wrong.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Changing 'Scripts/*.exe'

2022-10-04 Thread 2QdxY4RzWzUUiLuE
On 2022-10-04 at 14:55:36 +1300,
dn  wrote:

> On 04/10/2022 14.10, Meredith Montgomery wrote:
> > r...@zedat.fu-berlin.de (Stefan Ram) writes:
> > 
> >> Meredith Montgomery  writes:
> >>> Wouldn't it be great if it were portable by default?
> >>
> >>   I think under Windows there is a division of software
> >>   suggested by Microsoft, a division of software into
> >>   executable code and data (data may change in time).
> >>
> >>   The executable code is supposed to rest under
> >>   "C:\Program Files" the contents of which cannot be
> >>   modified by user processes easily. Program configuration
> >>   can be stored below "AppData" in the user directory.
> >>   It is supposed to be more secure when executable code
> >>   cannot be modified easily by user processes.
> >>
> >>   So far, Python has decided to ignore this and install
> >>   everything under AppData as I understand it. So one 
> >>   gets neither the security of "Program Files" nor the
> >>   possibility to move it to another directory easily.
> > 
> > Interesting.  I like portable things.  You copy from one place to
   ^
Copy what?  That's the crux of it.

On vintage (1980s? 1990s? way before OS X, anyway) Mac OS, I could copy
an alias (Mac's version of a symbolic link) to a floppy disk, walk (IOW,
"transfer via sneaker net") that alias to another Mac on the network,
and open *the original file* from that other Mac.  (I could even move
the file to another folder on the original Mac, but that didn't mean
much, because those old file systems were entirely flat (directories and
folders were an illusion maintained by the Finder).)

Permissions?  We don't need no stinkin' permissions!  ;-)

> > another and it just runs.  As it should.  Things should be simple.
> 
> +1

I like simple, portable things, too.  But this kind of simplicity
(applications looking in known locations for certain files) and this
kind of portability (allowing users to move those files) are at oods.
And that's before we consider security.

Also, [almost] by definition, Python virtual environments really blur
the lines between user, system, public, private, programs, and data.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread 2QdxY4RzWzUUiLuE
On 2022-10-12 at 17:43:18 +0100,
Paulo da Silva  wrote:

> Às 17:22 de 12/10/22, Tilmann Hentze escreveu:
> > Paulo da Silva  schrieb:
> > > I have python program that launches a detached rm. It works pretty well
> > > until it is invoked by cron! I suspect that for cron we need to specify
> > > the full path.
> > 
> > Probably you could use os.unlink[1] with no problem.
> No, because I need to launch several rm's that keep running after the script
> ends.

rm doesn't take that long.  Why are you detaching them?

(I'm not debating your point about cron.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to manage python shebang on mixed systems?

2022-11-07 Thread 2QdxY4RzWzUUiLuE
On 2022-11-07 at 11:56:19 +,
"Weatherby,Gerard"  wrote:

> Write the wrapper script.
> 
> #!/bin/bash
> if [ $(hostname) == "oldystem" ]; then
> exec /usr/bin/python $*
> else
> exec /usr/bin/python2 $*
> fi

Use "$@" (with the quotes) instead of a bare $*.  The former preserves
quoted arguments, while the latter does not.

Also, it's probably more robust to check explcitly for python2 instead
of depending on the hostname.  Renaming hosts isn't a common operation,
but in general, checking for what something *does* or *has* is better
than checking for what it *is* (hey, look:  duck typing!).

Implementing both suggestions yields this:

#! /bin/bash
if [ -x /usr/bin/python2 ]; then
exec /usr/bin/python2 "$@"
else
exec /usr/bin/python "$@"
fi

YMMV.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need max values in list of tuples, based on position

2022-11-12 Thread 2QdxY4RzWzUUiLuE
On 2022-11-12 at 15:03:10 +,
"Weatherby,Gerard"  wrote:

> Types are available if you want to use
> them. https://www.pythontutorial.net/python-basics/python-type-hints/

Python has always been a strongly typed language.

Recent changes have given it some level of static type checking in
addition to the dyunamic typing.

> From: Python-list  on 
> behalf of Pancho via Python-list 
> Date: Friday, November 11, 2022 at 6:28 PM
> To: python-list@python.org 
> Subject: Re: Need max values in list of tuples, based on position
> 
> That was one of the things I didn't like about Python. Lack of types and
> subsequent loss of intellisense is the thing I find hardest to deal with.

-- 
I can eat glass, it does not hurt me.
Dan Sommers, http://www.tombstonezero.net/dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What should go to stdout/stderr and why Python logging write everything to stderr?

2023-01-03 Thread 2QdxY4RzWzUUiLuE
On 2023-01-03 at 21:24:20 +,
c.bu...@posteo.jp wrote:

> The main question here is why does Python deciecded to make all logs
> go to stderr?

It makes sense to send all logging to one destination, and stderr is
that place.

> Maybe I totally misunderstood the intention of logging.info()?! Isn't
> this the "usual applicaton output"?

Aha.  This seems to be the misconception.  "Usual" in the sense of "the
application is running as usual," but not in the sense of "the output
the users usually see."

Logging is the process where applications write details that ordinarily
aren't visible to the user.  Logs are for forensics, after the fact.
There is an entire universe of software for managing (writing, reading,
archiving, retrieving, searching, etc.) log files.

At my last job, we wrote gigabytes of compressed logs every hour, and
read roughly two or three out of a million entries to track down
questions from our users.  Users wouldn't have understood 99% of what
was in the logs anyway.

I also worked a lot in embedded systems, like telephone switches.  Users
(i.e., the people with the telephones) didn't have access to the
switches, but when things went wrong, I could look at the switches' logs
to get the their view of what happened.

> If not, what messages should go into logging.info()? Can you name me some
> examples?

INFO level messages usually track "regular" happy-path progress through
your application:  reading config files, calculating results, writing
report.  Again, not meant for users (who would have their own progress
meters, textual or graphical), but for tracking down problems when there
are problems.  For one example, the name of the config file, even if you
only tell the users that you found a config file.

HTH
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What should go to stdout/stderr and why Python logging write everything to stderr?

2023-01-04 Thread 2QdxY4RzWzUUiLuE
On 2023-01-04 at 12:02:55 +,
"Weatherby,Gerard"  wrote:

> Dealing with stdout / stderr is bash is just syntax. I don’t always
> remember it off the top of my head but … stackoverflow.com.

https://tldp.org/LDP/abs/html/io-redirection.html

> On Linux at least it’s possible to pipe to arbitrary streams, it
> seems. The following uses bash to write “Hi” to the file “third”
> opened by Python. I determined the file was 5 empirically.

Empirically?

> import os
> import subprocess
> command= 'echo Hi >/dev/fd/5'
> fd = os.open("third",os.O_WRONLY|os.O_CREAT)

command = f"echo Hi >/dev/fd/{fd}"

Or:

command = f"echo Hi >&{fd}"

> os.set_inheritable(fd,True)
> 
> subprocess.run(('/usr/bin/bash','-c',command),close_fds=False)

By the time I'm that far down that path, I usually write a separate
function to call fork, open, close, and exec myself (old habits die
hard).  In the end, there's no reward for writing and maintaining shell
programs in Python.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Improvement to imports, what is a better way ?

2023-01-19 Thread 2QdxY4RzWzUUiLuE
On 2023-01-19 at 12:59:21 -0500,
Thomas Passin  wrote:

> Well, it's an art, not a science [...]

+1

> # Create a plot
> g2 = (
>   ggplot(df2,
>   aes('Days Since Jan 22',  # Comments can clarify these params
>   + geom_point(size=.1, color='blue') # size, color params optional
>   + theme_bw() # Optional theme (background, grid color, ...)
>  )

You've got a comma followed by a plus sign in there, so I'm not exactly
sure where the parameters to which function begin and end.

When it starts to look like this, I begin breaking out the parameters:

label = 'Days Since Jan 22'
size = geom_point(size=.1, color='blue')
theme = theme_bw()
g2 = ggplot(df2, aes(label, size, theme))

> # Compose a long string:
> msg = ('A very long line .\n'
>   + 'Another long bit of text '
>   + 'plus another '
>   )

If all the pieces are constants, then Python will concatenate them for
you:

msg = ('A very long line .\n'
   'Another long bit of text '
   'plus another')

You can even mix in "f" strings:

msg = ('long line\n'
   f'left text {name} right text'
   'more here')

But watch out for missing spaces between the pieces!  :-)

> The PEP-8 rules are good, but they can't cover all cases perfectly.

Some the PEP-8 rules are debatable.  Regardless, they can't cover all
cases perfectly.  (IOW, we agree on the bit that's relevant to this
thread.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

2023-01-23 Thread 2QdxY4RzWzUUiLuE
On 2023-01-22 at 18:19:13 -0800,
Jach Feng  wrote:

> 1) Modify the sys.argv by inserting an item '--' before parsing it, ie.
> sys.argv.insert(1, '--')
> args = parser.parse_args()

Please don't do that.  :-)

In my mind, sys.argv belongs to Python, not the application.  Instead,
pass a newly created argument list to parse_args:

args = parser.parse_args(['--'] + sys.argv)

This approach (adjusting the actual arguments) will work until your
program actually has options.

> 2) By adding an extra space character before the leading '-' sign, ie.
> e:\Works\Python>py infix2postfix.py " -4^2+5.3*abs(-2-1)/2"
> -4 2 ^ 5.3 -2 1 - abs * 2 / +
> 
> But no idea how it works? and if it can survive in a newer argparse 
> version?:-)

It works because argparse checks the first character of each argument,
and *doesn't* strip/trim whitespace.  So "-x" looks like an option, and
" -x" looks an argument.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: bool and int

2023-01-24 Thread 2QdxY4RzWzUUiLuE
On 2023-01-25 at 08:58:06 +1100,
Chris Angelico  wrote:

> On Wed, 25 Jan 2023 at 08:22, MRAB  wrote:
> > For backwards compatibility, bool was made a subclass of int.
> 
> Plus, it's really REALLY handy in quite a lot of situations.
> 
> > > C# is pickier, which I guess is a good thing.
> >
> 
> Nope, not a good thing. Actually a highly frustrating thing on those
> occasions when I have to write C# code.

The usual complaint is that some people write FORTRAN no matter what
language they're actually using.  Are you writing Python in C#?  ;-)

There's a sweet spot somewhere that includes dynamic typing, high
powered global type inference and optimization systems, a thriving
community, and a metric [boatload] of rock solid libraries.

And an alomost fanatical devotion to the Pope.  :-/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: bool and int

2023-01-25 Thread 2QdxY4RzWzUUiLuE
On 2023-01-25 at 12:14:50 +1100,
Chris Angelico  wrote:

> On Wed, 25 Jan 2023 at 10:32, <2qdxy4rzwzuui...@potatochowder.com> wrote:

> > The usual complaint is that some people write FORTRAN no matter what
> > language they're actually using.  Are you writing Python in C#?  ;-)

> But the way I have to write it in C# is a messed-up version of C:

There's your problem:  C# isn't C, it's Java.  Java looks like C, too,
but it isn't C, either.

> So the problem isn't that I'm trying to write Python in C#, but that
> I'm trying to write code that would work on pretty much *any other
> C-family language*, but doesn't work on C#. I could use those
> techniques in plenty of C-derived and C-inspired languages, but no
> not in C#, despite looking very much C-inspired. Unfortunately the
> truth is that C# is not *actually* C-inspired; it's really Microsoft
> Java, so it has all the stupidities of Java:

There.  Even ChrisA agrees with me.  ;-)

So, I think what you're trying to say is that you prefer the razor sharp
quality of truthiness to the zen of explicit being better than implicit.

To bring this back to Python (sorry), blurring the line between booleans
and integers is an old machine language trick, born of the days when we
measured memory in bytes (and large sums of cash!) rather than gigs[0].
In Python3, there's no more reason to use a boolean value as integer
(whether to accumulate values or to test a value against zero) as there
is to use a string (e.g., from an HTML form) as an integer.

[0] I remember meetings where the agenda was to allocate memory (yes, at
design time) for a particular value, and the answer was along the lines
of "you can have these five bits, and this would have taken a lot less
time had you told us sooner that you needed that value to persist across
input messages."

> But this is hardly a Python-versus-C# thing; it's Java versus most of
> the rest of the world, and C# feigns to be part of the C style while
> retaining the limitations of Java.

IMO, the problem started when Java tried to be too much like C to
attract (or should I say "trap"?) C developers.

> (My apologies if the Java entries look synthetic. It's because they
> are, and that's a consequence of me not having ANY reason to write
> Java code in, like, ever. In fact, I had to go and install a JDK just
> to confirm that Java really did have these limitations.)

They used Java at my last job (as in, the last job I had before I
retired), and it was absolutely awful, for any number of reasons, the
gymnastics (on many levels) required to support "primitive types" being
one of them.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: bool and int

2023-01-25 Thread 2QdxY4RzWzUUiLuE
On 2023-01-25 at 23:11:37 +1100,
Chris Angelico  wrote:

> On Wed, 25 Jan 2023 at 22:55, <2qdxy4rzwzuui...@potatochowder.com> wrote:

> > So, I think what you're trying to say is that you prefer the razor sharp
> > quality of truthiness to the zen of explicit being better than implicit.
> 
> Not sure what you mean here. If you want to bring this back to the Zen
> of Python, I would reference "practicality beats purity". We can do
> arithmetic on integers and floats without having to explicitly cast
> one to the other, because there's really no point in distinguishing
> them. We can do that with booleans and other types, too.

My point was that we all have our own preferences and biases, and in
this case, I think you and I lean in opposite directions, and Python is
big enough for both of us.

> > To bring this back to Python (sorry), blurring the line between booleans
> > and integers is an old machine language trick, born of the days when we
> > measured memory in bytes (and large sums of cash!) rather than gigs[0].
> > In Python3, there's no more reason to use a boolean value as integer
> > (whether to accumulate values or to test a value against zero) as there
> > is to use a string (e.g., from an HTML form) as an integer.
> 
> Strongly disagree. There is PLENTY of practical value in using
> booleans as numbers. This is nothing to do with counting bytes, and
> everything to do with how useful it is in practice.

IMO, the difference in readability between

autothrust_last_dv *= AT_mode == AT.Idle;

and

if(AT_mode != AT.Idle)
autothrust_last_dv = 0;

outweighs the practicality, whether C, C#, Java, or Python (ignoring the
insignificant differences in syntax).

Maintainability, too:  as soon as there's something else to do when
AT_mode isn't AT.Idle, I'm going to rewrite the first one as the second
one anyway.  (No, I won't mess up the braces.)

I could argue that the only reason the first one is readable at all is
that we've both been exposed to languages where fanatics assume that
True is 1 and False is 0.  I've also written low-level code with
hardware fanatics who insist that True is 0 and False is -1 (or 255,
depending on how much math they know).  In a posix shell script (or a
program that knows it might be run inside such a script), 0 is "true"
and non-zero is "false."  My point here is that you have to understand
how to work within whatever environment you're using, and that future
programmers (and future you!)  will have to deal with your choices
regardless of their background, biases, and preferences.

> C# is a pain to work with because it fails on these points of
> practicality. I'm morbidly curious as to how C# fanatics justify this
> sort of thing, given that so many languages just DTRT without needing
> to be told.

They say that Perl is practical.  ;-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Flamebait (was: Re: bool and int)

2023-01-26 Thread 2QdxY4RzWzUUiLuE
On 2023-01-26 at 10:52:06 +,
"Weatherby,Gerard"  wrote:

> I can’t help but wonder if there exists some Java forum /mailing list
> going on about how horrible Python is.

Not some of them.  *All* of them.  Here's the summary:

  - Dynamic Typing causes defects and makes non-toy software projects
impossible.

  - Python is slow.

  - Significant Whitespace [insert pejorative here].

  - Python allows code to exist outside of methods, and methods to exist
outside of classes.  What's a function?

  - There is no backwards compatibility.

Many, if not most, languages are created as improvements over others,
because those others have serious flaws (whether those flaws are real or
perceived).  And even at that, the earliest languages were created
because cores, wires, and machine languages were extremely painful.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: bool and int

2023-01-26 Thread 2QdxY4RzWzUUiLuE
On 2023-01-26 at 12:12:30 -0500,
Dino  wrote:

> On 1/25/2023 5:42 PM, Chris Angelico wrote:
> 
> > 
> > Try this (or its equivalent) in as many languages as possible:
> > 
> > x = (1 > 2)
> > x == 0
> > 
> > You'll find that x (which has effectively been set to False, or its
> > equivalent in any language) will be equal to zero in a very large
> > number of languages. Thus, to an experienced programmer, it would
> > actually be quite the opposite: having it NOT be a number would be the
> > surprising thing!
> 
> I thought I had already responded to this, but I can't see it. Weird.
> 
> Anyway, straight out of the Chrome DevTools console:
> 
> x = (1>2)
> false
> 
> x == 0
> true
> 
> typeof(x)
> 'boolean'
> 
> typeof(0)
> 'number'
> 
> typeof(x) == 'number'
> false
> 
> So, you are technically correct, but you can see that JavaScript - which
> comes with many gotchas - does not offer this particular one.

When you start a new language, try to start from the beginning.  Yes,
you know other languages, to varying degrees, and the language you are
learning is very likely similar, at least superficially, in some way or
another, to one of those other languages.  Use that existing knowledge
to the extent that it is useful; be prepared to forget everything you
know.

Python's choice of type hierarchy is not at all uncommon, and it's only
a gotcha if you come in with a predetermined idea of how certain things
"should" work.  I've already noted that writing FORTRAN in any language
is a meme.

For a completely different take on booleans, take a look at Python's
logical "and" and "or" operators (*not* the arithmetic "|" and "&"
operators), which only sometimes return an actual boolean value.  Then
compare them to the "any" and "all" builtins, which came along much
later.  Idiomatic Python uses the right tool for the job *in Python*,
whether or not that same tool is or isn;'t the right tool for the same
job in some other language.

Python is like Lisp in that it (Python) has strong dynamic typing.  From
my Common Lisp REPL:

CL-USER> (let ((x (< 1 2)))
   (cons x (type-of x)))
(T . BOOLEAN)

CL-USER> (let ((x (> 1 2)))
   (cons x (type-of x)))
(NIL . NULL)

In English, the canonical "true" value in Lisp is T, and its type is
BOOLEAN.  Likewise, the canonical "false" value in Lisp is NIL, and its
type is NULL.  Out of the box, Lisp will not convert T or NIL to a
number.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

2023-01-29 Thread 2QdxY4RzWzUUiLuE
On 2023-01-29 at 16:51:20 +1100,
Cameron Simpson  wrote:

> They're unrelated. As others have mentioned, "--" is _extremely_ common;
> almost _all_ UNIX command like programmes which handle -* style options
> honour the "--" convention. _argparse_ itself honours that convention, as
> does getopt etc.

And why do UNIX programs behave this way?

Because POSIX says they should:

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Organizing modules and their code

2023-02-03 Thread 2QdxY4RzWzUUiLuE
On 2023-02-03 at 13:18:46 -0800,
transreductionist  wrote:

> Here is the situation. There is a top-level module (see designs below)
> containing code, that as the name suggests, manages an ETL pipeline. A
> directory is created called etl_helpers that organizes several modules
> responsible for making up the pipeline. The discussion concerns the
> Python language, which supports OOP as well as Structural/Functional
> approaches to programming.

> I am interested in opinions on which design adheres best to standard
> architectural practices and the SOLID principles. I understand that
> this is one of those topics where people may have strong opinions one
> way or the other. I am interested in those opinions.

Okay, I'll start:  unless one of extract, transform, or load is already,
or will certainly at some point become, complex/complicated enough to be
its own architectural module with its own architectural substructure; or
you're constructing specific ETL pipelines for specific ETL jobs at the
times the jobs are defined; then I think you're overthinking it.

Note that I say that speaking as a notorious overthinker.  ;-)

Keep It Simple:  Put all four modules at the top level, and run with it
until you falsify it.  Yes, I would give you that same advice no matter
what language you're using.

FWIW, I'm not a big fan of OO, but based on what little I know about
your ETL pipelines, I agree with you that it probably doesn't make a big
difference at this level.  Define solid (in pretty much any/every sense
of the word, capitalized or not) interfaces between your modules, and
write your code against those interfaces, whether OO or any other
paradigm.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to read file content and send email on Debian Bullseye

2023-02-04 Thread 2QdxY4RzWzUUiLuE
On 2023-02-04 at 17:59:11 -0500,
Thomas Passin  wrote:

> On 2/4/2023 10:05 AM, ^Bart wrote:
> > Hi guys,
> > 
> > On a Debian Bullseye server I have a lftp upload and after it I should
> > send an email.

[...]

> [...] you could run a shell script that sends the file with lftp, and
> depending on the return code uses xdg-email to send one of the two
> messages. This would probably be simpler than using Python.

xdg-email appears to be for interactive use (it opens the user's
"preferred email composer"); I think sendmail would work much better
from a script.

Otherwise, I had the same initial thought, to add to and/or build a
wrapper around the existing lftp script.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Evaluate once or every time

2023-02-24 Thread 2QdxY4RzWzUUiLuE
On 2023-02-24 at 18:42:39 -0500,
Thomas Passin  wrote:

> VOWELS = 'aeiouAEIOU'
> is_vowel = 'y' in VOWELS
> 
> If I really needed them to be in a list, I'd probably do a list
> comprehension:
> 
> VOWEL_LIST = [ch for ch in VOWELS]

Why use a comprehension when a simple loop will do?  ;-)

No.  Wait.  That's not what I meant.

Why use a comprehension when the constructor will do?

VOWEL_LIST = list(VOWELS)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: No module named _socket, on windows

2023-02-26 Thread 2QdxY4RzWzUUiLuE
On 2023-02-25 at 15:58:35 -0800,
ofek shulberg  wrote:

> On Monday, January 4, 2010 at 9:19:21 PM UTC+2, Gabriel Genellina wrote:
> > En Mon, 04 Jan 2010 14:24:22 -0300, louisJ  escribi�:
> > > I installed python 2.6 (from python.org) for windows XP, and then
> > > Pylab.
> > > When I type "import pylab" in a python shell it shows the error:
> > >
> > > ImportError: No module named _socket
> > Open the Python command line, type the following lines, and tell us what 
> > happens:
> > Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit 
> > (Intel)] on
> > win32
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> import socket
> > >>> socket._socket
> > 
> > 
> > -- 
> > Gabriel Genellina
> 
> 
> hi Gabriel,
> i have the same problem and also pip is not installed for some reason, what 
> can i do?

Gabriel's post is thirteen years old and against a truly ancient version
of Python (on Windows XP, no less).  Please repost your question.
Include the OS and the version of Python you're using, and a copy/paste
of the error(s) you're receiving.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Look free ID genertion (was: Is there a more efficient threading lock?)

2023-03-01 Thread 2QdxY4RzWzUUiLuE
On 2023-03-01 at 14:35:35 -0500,
avi.e.gr...@gmail.com wrote:

> What would have happened if all processors had been required to have
> some low level instruction that effectively did something in an atomic
> way that allowed a way for anyone using any language running on that
> machine a way to do a simple thing like set a lock or check it?

Have happened?  I don't know about "required," but processors have
indeed had such instructions for decades; e.g., the MC68000 from the
early to mid 1980s (and used in the original Apple Macintosh, but I
digress) has/had a Test and Set instruction.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7 range Function provokes a Memory Error

2023-03-02 Thread 2QdxY4RzWzUUiLuE
On 2023-03-02 at 11:25:49 +,
Stephen Tucker  wrote:

> The range function in Python 2.7 (and yes, I know that it is now
> superseded), provokes a Memory Error when asked to deiliver a very long
> list of values.
> 
> I assume that this is because the function produces a list which it then
> iterates through.
> 
> 1. Does the  range  function in Python 3.x behave the same way?

No.

> 2. Is there any equivalent way that behaves more like a  for loop (that is,
> without producing a list)?

Try xrange.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Regular Expression bug?

2023-03-02 Thread 2QdxY4RzWzUUiLuE
On 2023-03-02 at 14:22:41 -0500,
jose isaias cabrera  wrote:

> For the RegExp Gurus, consider the following python3 code:
> 
> import re
> s = "pn=align upgrade sd=2023-02-"
> ro = re.compile(r"pn=(.+) ")
> r0=ro.match(s)
> >>> print(r0.group(1))
> align upgrade
> 
> 
> This is wrong. It should be 'align' because the group only goes up-to
> the space. Thoughts? Thanks.

The bug is in your regular expression; the plus modifier is greedy.

If you want to match up to the first space, then you'll need something
like [^ ] (i.e., everything that isn't a space) instead of that dot.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Baffled by readline module

2023-03-09 Thread 2QdxY4RzWzUUiLuE
On 2023-03-09 at 15:02:53 -0800,
Grant Edwards  wrote:

> Yeesh. What's _really_ embarassing is that I just stumbled across a
> small test program with which I had apparently figured this out 10-12
> years ago.  Must be about time to retire...

Retiring doesn't help.  :-)

I retired almost five years ago, and I just (within the past few days)
(re)discovered a command line parsing library I'd written about a year
and a half ago (i.e., after I retired).

Dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Baffled by readline module

2023-03-09 Thread 2QdxY4RzWzUUiLuE
On 2023-03-10 at 11:02:52 +1100,
Chris Angelico  wrote:

> Traditional retirement: Work till you're 60 or 65 or whatever, then
> society pats you on the head, calls you a "senior citizen", and lets
> you go and be idle till you die (which might be prematurely soon).

Sounds like Free Four¹:

  The memories of a man in his old age
  Are the deeds of a man in his prime.

  You shuffle in the gloom of the sickroom
  And talk to yourself as you die.

Great tune.  Bad life.

> Direction-change retirement: Work till you can afford to zero out your
> income, then finally do what you've always wanted to do, but never had
> time because you spent so much of it earning money.

A little bit of that.  We live in the RV, and we have crisscrossed the
country more than once, coronavirus notwithstanding.

> Tell-the-next-generation: Work till you know so much that you're
> infinitely valuable, then spend the rest of your life empowering the
> next group of amazing people. See for instance: NASA.

And a little bit of that, too.  NASA would have been nice; I did my most
of my time in the commercial space, with a short break in the government
contracting business.

> Programmer retirement: At an early age, learn how to wield PHENOMENAL
> COSMIC POWER, and spend the next X years in an itty bitty working
> space, earning money. Eventually, upgrade to better living/working
> space. Eventually, downgrade to a small wooden box six feet below the
> ground. Never once relinquish the power. Never once abandon that
> feeling of mastery.

I was with you until that part of the small wooden box.  :-)

> We're not really an industry that has a concept of retirement.

Which is why I'm still here (on this mailing list, and a handful of
others like it).

Thanks for asking.

¹ https://www.azlyrics.com/lyrics/pinkfloyd/freefour.html
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Baffled by readline module

2023-03-09 Thread 2QdxY4RzWzUUiLuE
On 2023-03-10 at 12:57:48 +1100,
Chris Angelico  wrote:

> On Fri, 10 Mar 2023 at 12:56, Greg Ewing via Python-list
>  wrote:
> >
> > On 10/03/23 1:46 pm, Grant Edwards wrote:
> > > That's not how it acts for me. I have to "import readline" to get
> > > command line recall and editing.
> >
> > Maybe this has changed? Or is platform dependent?
> >
> > With Python 3.8 on MacOSX I can use up arrow with input()
> > to recall stuff I've typed before, without having to
> > import anything.
> >
> 
> import sys; "readline" in sys.modules
> 
> Is it? Might be that something's pre-importing it.

My ~/.pythonrc contains the following:

import readline
import rlcompleter
readline.parse_and_bind( 'tab: complete' )

IIRC, that's been there "forever," certainly back into Python2, and
probably back into Python1.  On my Arch Linux system Python 3.10.9, I
get readline behavior with or without those lines.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Baffled by readline module

2023-03-09 Thread 2QdxY4RzWzUUiLuE
On 2023-03-09 at 19:11:56 -0800,
Grant Edwards  wrote:

> On 2023-03-10, 2qdxy4rzwzuui...@potatochowder.com 
> <2qdxy4rzwzuui...@potatochowder.com> wrote:

> If you run this application from the command line, you get command
> recall and editing when entering strings at the "cmd:" prompt?
> 
> #!/usr/bin/python
> while True:
> try:
> line = input('cmd: ')
> except EOFError:
> print()
> break
> print('You entered "%s"' % line)

In my case, no.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Lambda returning tuple question, multi-expression

2023-03-10 Thread 2QdxY4RzWzUUiLuE
On 2023-03-10 at 22:16:05 -0500,
Thomas Passin  wrote:

> I'd make the pattern in this example even more understandable and less
> error-prone:
> 
> def update_pids(target):
> cmd = ["tail", "-n", "1", "-f", f"/var/log/{target}"]
> pids.update({target: subprocess.Popen(cmd)}) if not \
> pids[target] else None

I might be missing something, but how is that more understandable and
less error prone than any of the following:

if not pids[target]:
cmd = ["tail", "-n", "1", "-f", f"/var/log/{target}"]
pids.update({target: subprocess.Popen(cmd)})

or:

cmd = ["tail", "-n", "1", "-f", f"/var/log/{target}"]
pids[target] or pids.update({target: subprocess.Popen(cmd)})

or:

if pids[target]:
pass
else:
cmd = ["tail", "-n", "1", "-f", f"/var/log/{target}"]
pids.update({target: subprocess.Popen(cmd)})

Picking a nit, that's not a good place to continue that line with the
backslash, either.  IMO, "not pids[target]" should be atomic.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Friday finking: IDE 'macro expansions'

2023-03-16 Thread 2QdxY4RzWzUUiLuE
On 2023-03-17 at 11:55:38 +1300,
dn via Python-list  wrote:

> Do you make use of your IDE's expansionist tendencies, and if-so, which
> ones?

Expansionist tendencies.  Nice phrase.  :-)

> NB this is where vi/emacs enthusiasts start chuckling (polite term for
> 'insane cackling') ...

Long time (since the 1980s, when you had to edit config.h and compile it
yourself) insane emacs enthusiast and "Unix is my IDE" claimant here.  I
used one language that required a special editor because the "source
code" was a semi-compiled byte stream instead of ASCII.  A coworker once
awarded me a black belt in vi for knowing how to indent and unindent
code.

They made me write in Java at my last job; that language encourages IDEs
beause they deny that the underlying OS and tools exist at all.  I used
to say that some of my coworkers weren't writing Java, they were writing
IntelliJ.

Anyway, the short answer to your question is no.

I think the main reason is that there are/were too many editors in too
many contexts to start depending on such features.  What happens if I'm
editing on a target box instead of my desktop?  What if I'm helping a
coworker, and they use a different editor (different tools for different
programmers)?

> I haven't made use of such a tool, to-date - OK, yes, I have practised a
> high typing-speed (and accuracy). Puff, puff...

I, too, type relatively quickly, which definitely relieves some of the
appeal.  Saving a few keystrokes isn't worth the extra congnitive load
to remember how to use those features.  And by the time I edit the
template, it's more keystrokes (and usually cursor motion and
placeholder deletion keystrokes not related to the programming task at
hand) than if I had typed the full code in the first place.

> Also, at the time, I'm thinking in 'code', rather than about what tool might
> implement said ideas.

Exactly.  Any excursion away from the programming language and the
programming task at hand is disruptive.

I did finally train myself to use Emacs' dabbrev-expand function, which
completes the current "word" according to the previous occurrance of
that word, which lets me use longer identifiers without having to type
them out or copy/paste them.  That said, I often find myself typing
stuff out anyway, again because any deviation from writing code is a
deviation from writing code.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Friday finking: IDE 'macro expansions'

2023-03-18 Thread 2QdxY4RzWzUUiLuE
On 2023-03-18 at 11:49:24 +,
"Weatherby,Gerard"  wrote:

> For templating, I have two Python programs for starting new work. One
> generates a standalone Python program with the Python shebang, a
> __main__ which calls def main(), and logging and argparser
> intialization. The other generates a pyproject.toml /setup.cfg
> directory structure with a main.py referenced as a console
> script. from setup.cfg

You don't describe the process for using those programs, but if you
"have two Python programs," then it sounds like you're *not* using the
IDE/editor for that.

(I'm not judging, I'm trying to relate your answer to dn's original
question.)

For an opposing view, I'm much more likely to copy an old project (at
the command line, often with sed) and then use my editor to morph it
into a new project.  IME, unless there's an imposed process, those
templates and programs tend to experience bitrot, whereas going back to
an old program gives me the chance to keep it up to date w.r.t. what
I've learned more recently, which helps reinforce said learning.

> From: Python-list  on 
> behalf of dn via Python-list 
> Date: Thursday, March 16, 2023 at 6:59 PM
> To: 'Python' 
> Subject: Friday finking: IDE 'macro expansions'
> *** Attention: This is an external email. Use caution responding, opening 
> attachments or clicking on links. ***
> 
> It is a long, long, time since I've thrown one of these into the
> maelstrom of our musings.
> (have the nightmares receded?)
> 
> 
> Do you make use of your IDE's expansionist tendencies, and if-so, which
> ones?

[...]

-- 
I can eat glass, it does not hurt me.
Dan Sommers, http://www.tombstonezero.net/dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What kind of "thread safe" are deque's actually?

2023-03-27 Thread 2QdxY4RzWzUUiLuE
On 2023-03-27 at 18:25:01 -0700,
Travis Griggs  wrote:

> "Deques support thread-safe, memory efficient appends and pops from
> either side of the deque with approximately the same O(1) performance
> in either direction.”

> (https://docs.python.org/3.11/library/collections.html?highlight=deque#collections.deque)

[...]

> I guess this surprised me. When I see “thread safe”, I don’t expect to
> get errors.

Even without threads, mutating a collection while iterating over it
usually results in bad things happening.

$ python
Python 3.10.10 (main, Mar  5 2023, 22:26:53) [GCC 12.2.1 20230201] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import collections
>>> x = collections.deque()
>>> x.append(44)
>>> x.append(55)
>>> x.append(66)
>>> x.append(77)
>>> x
deque([44, 55, 66, 77])
>>> for y in x:
 x.pop()

77
Traceback (most recent call last):
  File "", line 1, in 
RuntimeError: deque mutated during iteration

Concurrency just increases the likeliness of mutating while iterating.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Windows installer from python source code without access to source code

2023-03-31 Thread 2QdxY4RzWzUUiLuE
On 2023-04-01 at 10:49:18 +1100,
Chris Angelico  wrote:

> [...] I don't have access to the Gmail source code but I'm using the
> service [...]

You have access to Gmail's front end source code.  Your web browser runs
it every time you use the service (and probably while you aren't using
the service, too).  My educated guess is that Google expended some
effort to hinder you from looking at and/or analyzing (or do you say
analysing?) that code, and that their lawyers will come knocking at your
metaphorical door if they so much as think you are using that code in
some way other than running it inside your web browser.  If only this
situation were a cruel April Fool's Day joke.

You don't have access to Gmail's back end source code.

Many/Most web apps follow this pattern to varying degrees.  I do not
know whether this setup meets the OP's requirements.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: built-in pow() vs. math.pow()

2023-04-03 Thread 2QdxY4RzWzUUiLuE
On 2023-04-01 at 08:16:46 -0700,
Andreas Eisele  wrote:

> BTW, there is another difference: built-in pow() deals with complex
> arguments, while functions in math won't accept them at all.

See also .
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Weak Type Ability for Python

2023-04-12 Thread 2QdxY4RzWzUUiLuE
On 2023-04-12 at 14:51:44 -0400,
Thomas Passin  wrote:

> On 4/12/2023 1:11 PM, Chris Angelico wrote:
> > On Thu, 13 Apr 2023 at 03:05, Ali Mohseni Roodbari
> >  wrote:
> > > 
> > > Hi all,
> > > Please make this command for Python (if possible):
> > > 
> > > > > > x=1
> > > > > > y='a'
> > > > > > wprint (x+y)
> > > > > > 1a
> > > 
> > > In fact make a new type of print command which can print and show strings
> > > and integers together.
> > > 
> > 
> > Try:
> > 
> > print(x, y)
> > 
> > ChrisA
> 
> It puts a space between "1" and "a", whereas the question does not want the
> space.  print(f'{x}{y}') would do it, but only works for variables named "x"
> and "y".

Or possibly print(x, y, sep='').

> As happens so often, the OP has not specified what he actually wants to do
> so we can only answer the very specific question.

Agreed.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Weak Type Ability for Python

2023-04-13 Thread 2QdxY4RzWzUUiLuE
On 2023-04-12 at 22:47:17 -0400,
Dennis Lee Bieber  wrote:

>   REXX -- where everything is considered a string until it needs to be
> something else.

I see your REXX, and raise you an awk,¹ except that awk won't add a
string to a number, or a number to string, but it will concatenate in
both cases:

$ echo 1 a | awk '{print $1 $2}{print $1 + $2}'
1a
1

$ echo 1 a | awk '{print $2 $1}{print $2 + $1}'
a1
1

$ echo 1 2 | awk '{print $1 $2}{print $2 + $1}'
12
3

¹ GNU Awk 5.2.1, API 3.2, PMA Avon 8-g1, (GNU MPFR 4.2.0, GNU MP 6.2.1)

Dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RE: Weak Type Ability for Python

2023-04-13 Thread 2QdxY4RzWzUUiLuE
On 2023-04-13 at 22:14:25 -0400,
avi.e.gr...@gmail.com wrote:

> I am looking at a data structure that is an object of some class and
> stores the data in any way that it feels like. But it may be a bit of
> a chameleon that shows one face or another as needed. I can write code
> now that simply adds various access methods to the class used and also
> provides a way to query if it supports some interfaces.

Python dicts act mostly like hash tables.  All by themselves, hash
tables are unordered (and in return for giving up that order, you get
O(1) access to an item if you know its key).

But when you ask a Python dict for the keys, you always get them in the
same order, skipping those that have been deleted since the last time
you asked, and appending the new keys to the end of the list in the
order in which you added them.

There's your chameleon.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Editing PEP-8, in particular "expected 2 blanks, found 1

2023-05-02 Thread 2QdxY4RzWzUUiLuE
On 2023-05-02 at 20:39:35 +,
"Kevin M. Wilson via Python-list"  wrote:

> [...] Where can I edit these out of my IDE?

I doubt anyone will know unless you at least tell us which IDE you're
using.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Addition of a .= operator

2023-05-20 Thread 2QdxY4RzWzUUiLuE
On 2023-05-21 at 06:11:02 +1200,
dn via Python-list  wrote:

> On 21/05/2023 05.54, Alex Jando wrote:
> > I have many times had situations where I had a variable of a certain type, 
> > all I cared about it was one of it's methods.
> > 
> > For example:
> > 
> > 
> > import hashlib
> > hash = hashlib.sha256(b'word')
> > hash = hash.hexdigest()
> > 
> > import enum
> > class Number(enum.Enum):
> >  One: int = 1
> >  Two: int = 2
> >  Three: int = 3
> > num = Number.One
> > num = num.value
> > 
> > 
> > Now to be fair, in the two situations above, I could just access the method 
> > right as I declare the object, however, sometimes when passing values into 
> > functions, it's a lot messier to do that.

Can you give an example, preferably one from an actual program, that
shows the mess?  Is it More Messier™ than the difference between the
following examples?

# example 1
hash = hashlib.sha256(b'word')
f(hash.hexdigest()) # call f with hash's hexdigest

# example 2
hash = hashlib.sha256(b'word')
hash = hash.hexdigest() # extract hash's hexdigest
f(hash) # call f with hash's hexdigest

Can you also show what your code would look like with a .= operator?

> > So what I'm suggesting is something like this:
> > 
> > 
> > import hashlib
> > hash = hashlib.sha256(b'word')
> > hash.=hexdigest()
> > 
> > import enum
> > class Number(enum.Enum):
> >  One: int = 1
> >  Two: int = 2
> >  Three: int = 3
> > num = Number.One
> > num.=value
> > 
> 
> A custom-class wrapper?
> Even, a decorator-able function?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Application window geometry specifier

2021-01-13 Thread 2QdxY4RzWzUUiLuE
On 2021-01-13 at 21:41:26 -0600,
Python  wrote:

> On Thu, Jan 14, 2021 at 12:27:19PM +1100, Chris Angelico wrote:
> > On Thu, Jan 14, 2021 at 12:11 PM Python  wrote:
> > >
> > > On Tue, Jan 12, 2021 at 06:43:39PM -, Grant Edwards wrote:
> > > > And those X11 users will swear at you if you override their window
> > > > managers configured window placement. Application code should not care
> > > > about or try to control window geometry. Period.
> > >
> > > I think this is just plain wrong.
> 
> As a side note, from a purely technical standpoint, Grant's point is
> demonstrably factually false.  A GUI application necessarily must
> "care about or try to control window geometry" so that if the user
> moves or resizes the application, enables or disables additional
> widgets, etc., the widgets it contains are redrawn such that they
> remain usable, possibly expanding the canvas they're drawn on or
> adding new GUI elements like scrollbars to ensure that (at least it
> does if it wants to be useful) ...

A GUI application must care about the geometry (to react to changes made
by the user or the window manager).

OTOH, if I (the user) request a 80x24 character terminal window with a
certain size glyph, and I get a terminal that isn't 80x24 and that glyph
size, then at least one of the application, the window manager, or some
other library is broken.  Period.  Go ahead, tell me, the *user*, that I
shouldn't do that, or that I should reconsider my use case, or that such
a request is too demanding for a modern computer system to honor.

See also Extended Window Manager Hints,¹ with the emphasis on *hints*.

> ... GUI frameworks may provide a core that automatically handles this
> for you, but that is not always the case (as an extreme example, if
> you're writing the whole app from scratch in assembly language) ...

Define "from scratch."

Why does the language in which I'm writing my application matter?

¹ https://en.wikipedia.org/wiki/Extended_Window_Manager_Hints
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: why sqrt is not a built-in function?

2021-01-14 Thread 2QdxY4RzWzUUiLuE
On 2021-01-14 at 17:54:55 +,
Alan Gauld via Python-list  wrote:

> My question is: why do we even have a sqrt() in the
> math module given that pow() and ** are already there?

Probably because the standard C math library has such a function, and
Python's math module is (or at least was) supposed be a thin wrapper
around that library.

For completeness, C doesn't have a exponentiation operator.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A beginning beginner's question about input, output and . . .

2021-01-16 Thread 2QdxY4RzWzUUiLuE
On 2021-01-16 at 15:42:44 -0500,
DonK  wrote:

> Yes, Windows. Iterating open windows with the Window's API is easy the
> hard (very hard) part is sending keystrokes to those windows to Save
> them. It's very tricky because the timing is critical. 
> 
> You have to use one of those "spy" utilities to get the name and
> classid for the controls that you want to send a command to, then use
> the Window's API functions SendMessage or PostMessage to send the
> message directly to the control or just put it in the message que.
>  
> Like sending an "s" to the File menu to (S)ave. It's more complicated
> than you would think and it doesn't work very well. It's like pushing
> on a string. 

Then don't do that?  ;-)

I'm no Windows expert (nor even a user), but do those applications have
a scripting interface?  (DDE?  OLE?  COM?  Do those letters ring a bell?
The right bell?  Are those technologies current?)  What you're trying to
do sounds like working *against* those applications rather than with
them.

> For example, I've found a need to parse text documents quite a number
> of times over the years. Basic/VB is great at doing that. How's
> Python?

Python can do that.  Can you expand on "parse" and "text documents"?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A beginning beginner's question about input, output and . . .

2021-01-16 Thread 2QdxY4RzWzUUiLuE
On 2021-01-16 at 17:46:13 -0500,
DonK  wrote:

> On Sat, 16 Jan 2021 14:56:37 -0600, 2qdxy4rzwzuui...@potatochowder.com
> wrote:
> 
> >On 2021-01-16 at 15:42:44 -0500,
> >DonK  wrote:

> >> For example, I've found a need to parse text documents quite a number
> >> of times over the years. Basic/VB is great at doing that. How's
> >> Python?
> 
> >
> >Python can do that.  Can you expand on "parse" and "text documents"?
> 
> There's nothing that I have any particular need for at the moment but
> it is something that I've done a number of times over the years. I
> think it's a common need?? 

Indeed.  :-)  In my experience, "parse" and "text documents" often mean
different things to different people.

> I've used Pascal and BASIC\VB for string parsing and BASIC\VB is much
> better. VB has all the string handling functions that you need built
> in but, for example, Pascal has some but you have to create others to
> be useful. Since I'm just beginning with Python I have no knowledge or
> criticism of how Python does this but I'm sure that it can.
> 
> Since I've retired I've written parsers for my bank records, medical
> records and other personally useful things. I would categorize them as
> trivial but useful.  i.e utility means useful
> 
> When I was working, in the 1999-2001 range, I wrote a parser in VB
> that unscrambled corrupted "batch files" for credit card processing so
> that vendors didn't have to spend hours rebuilding them by hand. Some
> of those files had over 700 credit card transactions in them. That's
> one example.

Sounds like you're parsing files composed of lines of plain text where
each line represents some kind of record (as opposed to parsing a
document containing programming code as a precursor to generating an
executable, or looking through a word processing document for something
important).

A bare minimum skeleton might look something like this:

with open(filename) as f:
for line in f.readlines():
handle_one_line(f)

Python has a capable string type for handling text; see
.

Or look into the fileinput module for a convenient way to run through a
collection of files and/or standard input.

> I'm looking forward to learning some Python, mostly for fun, but I'm
> sure that it will also be useful.

Absolutely!  :-)

Happy Hacking!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread 2QdxY4RzWzUUiLuE
On 2021-01-27 at 17:41:52 -0500,
C W  wrote:

> Great tutorial Irv, very simple with if-else example, gets the point
> across.

Disclaimer:  I did not watch the video.

> My main takeaway from the discussion so far is that: you can't
> troubleshoot Python without some kind of breakpoint or debugger.

I disagree.  :-)

Having spent a long time developing and debugging embedded systems (some
soft- or hard- real time), I believe that you can troubleshoot programs
(regardless of source language) without debuggers.  If a system can
generate output, then I (the programmer) can change that output, observe
the results, and figure out what's working and what's not.  Yes, some
systems are more painful than others, but yes, some debugging
environments are more painful than others, too.

A well placed call to print (they're not "print statements" anymore!)
can be much more enlightening and much faster than single stepping
through code in a debugger, and seeing the output from the same print
statement inside a loop can be much better than manually examining
variables iteration after iteration and trying to remember what the
value was before.

The best debugging tool, however, remains your brain.  Way before I add
a call to print or you set up your debugger, thinking about what went
wrong and where to look can solve the problems without resorting to
external tools.  :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I need some help interpreting this error

2021-02-17 Thread 2QdxY4RzWzUUiLuE
On 2021-02-17 at 15:40:27 +,
Chris Green  wrote:

> I'm running this using Python 3.7 on a Linux system.
> 
> Most of the time (i.e. for a couple of days now) the program has been
> satifactorily delivering mail messages, hundreds of them.  However one
> mail message has provoked the following error:-
> 
> chris@cheddar$ tail mail.err
> Traceback (most recent call last):
>   File "/home/chris/.mutt/bin/filter.py", line 95, in 
> if sbstrip in msghdr["subject"]:
> TypeError: argument of type 'Header' is not iterable
> 
> 
> But msghdr["subject"] is surely just a string isn't it?  Why is it 
> complaining about something of type 'Header'?

Isn't it?  ;-)

First step:  Print msghdr["subject"] and its type to know for sure.  The
worst case is that you'll verify your assumption.

IIRC, the subject header is actually optional.  Maybe someone sent a
message without a subject?  Is msghdr["subject"] None?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I need some help interpreting this error

2021-02-17 Thread 2QdxY4RzWzUUiLuE
On 2021-02-17 at 16:42:03 +,
Chris Green  wrote:

> 2qdxy4rzwzuui...@potatochowder.com wrote:
> > On 2021-02-17 at 15:40:27 +,
> > Chris Green  wrote:
> > 
> > > I'm running this using Python 3.7 on a Linux system.
> > > 
> > > Most of the time (i.e. for a couple of days now) the program has been
> > > satifactorily delivering mail messages, hundreds of them.  However one
> > > mail message has provoked the following error:-
> > > 
> > > chris@cheddar$ tail mail.err
> > > Traceback (most recent call last):
> > >   File "/home/chris/.mutt/bin/filter.py", line 95, in 
> > > if sbstrip in msghdr["subject"]:
> > > TypeError: argument of type 'Header' is not iterable
> > > 
> > > 
> > > But msghdr["subject"] is surely just a string isn't it?  Why is it 
> > > complaining about something of type 'Header'?
> > 
> > Isn't it?  ;-)
> > 
> > First step:  Print msghdr["subject"] and its type to know for sure.  The
> > worst case is that you'll verify your assumption.
> > 
> The documentation says "Headers are represented by customized
> subclasses of str", so it's a sub-class of str.  

So we still don't know what the content of msghdr["subject"] is at the
time the error occurs.  I don't mean to sound harsh, but that the
documentation and the code are correct, and that they match, remain
assumptions.  Sometimes, seeing an actual value tells you what went
wrong (e.g., "oh, that's the sender's address, not the receiver's
address," "oh, that's my 'time' class, not the one from the standard
library").

The traceback tells you that msghdr["subject"] is of type Header.  Is
Header a sub-class of str?

Again, the worst case of looking at the value (whether in a log or in a
debugger) is that you verify your assumption.

> > IIRC, the subject header is actually optional.  Maybe someone sent a
> > message without a subject?  Is msghdr["subject"] None?
> 
> If you look at the code (and the documentation) if there's no subject
> header I'll get the string "unknown", I've also tried sending myself
> an E-Mail with no header and not provoked the error.

That's good news about subject-less emails not generating an error, and
separate (possibly related) good news about your code handling an email
without a subject header.  ;-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I need some help interpreting this error

2021-02-17 Thread 2QdxY4RzWzUUiLuE
On 2021-02-17 at 16:52:55 +,
Chris Green  wrote:

> Stefan Ram  wrote:
> > Chris Green  writes:
> > >But msghdr["subject"] is surely just a string isn't it?  Why is it 
> > >complaining about something of type 'Header'?
> > 
> >   What would you do to debug-print the type of an object?
> > 
> I don't know, what would I do?  :-)
> 
> Without knowing what provokes the problem I could be waiting for days
> or weeks even before I see the error again.  As I'd need to print the
> type for every message I'd get some big logs or I'd need to add a try:
> except to trap the specific error.

An intermittent problem!  They're our favorite!  ;-)

Seriously, though, do you have a message that provokes the problem?  Is
there a log at all?  Can you re-run that message through the code, in a
debugger or otherwise?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I need some help interpreting this error

2021-02-17 Thread 2QdxY4RzWzUUiLuE
On 2021-02-17 at 17:36:48 +,
Chris Green  wrote:

> Stefan Ram  wrote:
> > Chris Green  writes:
> > >chris@cheddar$ tail mail.err
> > >Traceback (most recent call last):
> > >  File "/home/chris/.mutt/bin/filter.py", line 95, in 
> > >if sbstrip in msghdr["subject"]:
> > >TypeError: argument of type 'Header' is not iterable
> > >But msghdr["subject"] is surely just a string isn't it?  Why is it 
> > >complaining about something of type 'Header'?
> > 
> >   I presume that the error message has been edited (abbreviated).
> > 
> >   In "if sbstrip in msghdr["subject"]:", there is no argument.
> > 
> >   But the error message says "argument of ...".
> > 
> >   When something that is not iterable is presented to a for loop,
> >   the error message does not mention "argument":
> > 
> I have output everything that appears, I've not changed it at all.
> It's the whole content of the file ~/tmp/mail.err as it's the only
> error that has occurred for the last day or so.  The error log is
> created by the line:-
> 
> sys.stderr = open("/home/chris/tmp/mail.err", 'a')
> 
> So that's everything that was output to stderr.
> 
> I think you are puzzled in the same way that I was, the error message
> doesn't make a lot of sense.  

At some point, the internal code for the "in" operator is likely
iterating over the elements of msghdr["subject"].

That error message doesn't make a lot of sense if msghdr["subject"] is a
sub-class of str.  It makes more sense if msghdr["subject"] is something
else.

IMO, you need more information in the log, a try/except block to prevent
the code from crashing, and, yes, perhaps some patience to wait for it
to happen again.

Sometimes, developing software isn't all fortune, fame, and glory.  ;-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a way to subtract 3 from every digit of a number?

2021-02-20 Thread 2QdxY4RzWzUUiLuE
On 2021-02-20 at 09:40:48 -0500,
C W  wrote:

> Hello everyone,
> 
> I'm curious if there is a way take number and back each digit by 3 ?
> 
> 2342 becomes 9019
> 8475 becomes 5142
> 5873 becomes 2540
> 
> The tricky part is that 2 becomes 9, not -1.
> 
> Here's my toy example and what I attempted,
> > test_series = pd.Series(list(['2342', '8475', '5873']))
> > test_series
> 02342
> 18475
> 25873
> dtype: object
> 
> > test_series.str.split('')
> [, 2, 3, 4, 2, ]
> [, 8, 4, 7, 5, ]
> [, 5, 8, 7, 3, ]
> dtype: object
> 
> What a good approach to this? Is there a method or function that should be
> handling this?

I'm assuming that this is homework or some other academic exercise, so
I'm being deliberately vague on some of the details.  That said:

Break it down.

(1) Write a function that takes a number and returns the result of
subtracting three from it.  This function should also handle the
"special" cases of 0, 1, and 2.  Look up the modulo operator for a
better way than actually special-casing them.

(2) Python has several ways of building a list by transforming the
elements of a list one at a time and building a new list from the
results.  Consider a simple iterative approach, but also look up the
builtin "map" function and "list comprehensions."

One interesting decision is where to convert the individual digits of
the original numbers from strings to integers.  There are arguments for
doing it in either of the parts I've just outlined, as well as making it
a sort of Part 1a and applying both Part 1 and Part 1a to each digit.

> Thanks so much!

HTH,
Dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a way to subtract 3 from every digit of a number?

2021-02-20 Thread 2QdxY4RzWzUUiLuE
On 2021-02-20 at 20:49:15 -0800,
Dan Stromberg  wrote:

> On Sat, Feb 20, 2021 at 7:13 PM Ming  wrote:
> 
> > I just wrote a very short code can fulfill your needs:
> >
> > a = 2342
> > b = int("".join(map(lambda x: str((int(x)-3)%10) ,list(str(a)
> >
> I tend to favor plenty of temporary variables with descriptive names, but
> this is indeed short.
> 
> Apart from that, you may find that using a generator expression is shorter
> and clearer than map+lambda.  It should allow to additionally eliminate the
> list conversion.
> 
> So in the terse form you've got there, it'd be more like:
> b =  int(''.join(str((int(x) - 3) % 10) for x in str(a))
> 
> But in real life, I'd try to use descriptive variable names for some of the
> subexpressions in that.  This makes reading and debugging simpler, which is
> important because the maintenance phase of software is almost always much
> longer and costly than the development phase.  And although you could do a
> generator expression for each of the different parts of (int(x) - 3) % 10,
> I kinda like having a named function for just that piece.
> 
> So maybe:
>   def rot_3(character):
>   """Convert to int, subtract 3 and mod 10."""
>   digit = int(character)
>   assert 0 <= digit <= 9
>   return (digit - 3) % 10
> 
> 
>   def descriptive_minus_three_caesar(input_number):
>   """Convert to a -3 caesar cypher on an integer."""
>   string_number = str(input_number)
>   rotated_digits = (rot_3(character) for character in string_number)
>   output_string = ''.join(str(digit) for digit in rotated_digits)
>   output_number = int(output_string)
>   return output_number

>>> descriptive_minus_three_caesar('38')
5

The problem is underspecified, and the examples are lacking, but based
on the phrase "each digit" and the examples that contain a 3, I'd prefer
to see "38" become "05."
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a way to subtract 3 from every digit of a number?

2021-02-21 Thread 2QdxY4RzWzUUiLuE
On 2021-02-21 at 09:28:39 -0500,
C W  wrote:

> I got it to work! I defined a separate function, and put it into
> df['col_1'].apply().
> 
> Not the most elegant, but it worked. I'm also curious how people on this
> mailing list would do it.

I don't know anything about pandas, but for one string of digits
(there's no clear requirements for how to handle leading zeros in the
input or the output), I'd end up with something like this:

OFF_BY_3_MAPPING = dict(zip('0123456789', '7890123456'))
def off_by_3(digits):
return ''.join(OFF_BY_3_MAPPING[c] for c in digits)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: XML RPC changes between 3.7 and 3.9 yield 401 http error

2021-02-24 Thread 2QdxY4RzWzUUiLuE
On 2021-02-24 at 15:29:58 +0100,
lucas  wrote:

> I'm currently trying to understand an error when using the dokuwikixmlrpc
> python module, allowing to easily work with DokuWiki RPC interface.
> 
> Another description of the problem :
>   https://github.com/kynan/dokuwikixmlrpc/issues/8
> 
> Here is the code, tailored to work with the DokuWiki RPC interface:
> 
> from urllib.parse import urlencode
> import xmlrpc.client as xmlrpclib
> 
> URL = 'wiki.example.net'
> USER = 'user'
> PASSWD = 'password'
> USER_AGENT = 'DokuWikiXMLRPC  1.0  for testing'
> 
> script = '/lib/exe/xmlrpc.php'
> url = URL + script + '?' + urlencode({'u': USER, 'p': PASSWD})
> xmlrpclib.Transport.user_agent = USER_AGENT
> xmlrpclib.SafeTransport.user_agent = USER_AGENT
> proxy = xmlrpclib.ServerProxy(url)
> 
> v = proxy.dokuwiki.getVersion()
> print(v)
> 
> When ran with Python 3.7 (a personnal debian server, or a personal windows
> computer), i obtain the expected 'Release 2018-04-22a "Greebo"' as ouput.
> When ran with Python 3.9 (my personnal, manjaro machine), i obtain the
> following stacktrace:
> 
> Traceback (most recent call last):
>   File "/home/project/read.py", line 32, in 
> v = proxy.dokuwiki.getVersion()
>   File "/usr/lib/python3.9/xmlrpc/client.py", line 1116, in __call__
> return self.__send(self.__name, args)
>   File "/usr/lib/python3.9/xmlrpc/client.py", line 1456, in __request
> response = self.__transport.request(
>   File "/usr/lib/python3.9/xmlrpc/client.py", line 1160, in request
> return self.single_request(host, handler, request_body, verbose)
>   File "/usr/lib/python3.9/xmlrpc/client.py", line 1190, in
> single_request
> raise ProtocolError(
> xmlrpc.client.ProtocolError:  wiki.example.net/lib/exe/xmlrpc.php: 401 Unauthorized>

At the risk of stating the obvious, it might actually be an
authentication problem.  In addition to double checking the password:

(1) make sure all of your certificates (under Arch Linux, on which
Manjaro is based), that's the ca-certificates package) are up to date;
and

(2) check with whoever owns the wiki about any other certificates they
require.

After that, all I know about XML RPC is to avoid it.  :-)

> I don't have the possibility to test this on python 3.8 specifically, but
> since the XML and XMLRPC modules have been updated in 3.8, and since 3.9
> doesn't seems to introduce any change for them, i would expect 3.8 to
> introduce some change that dokuwikixmlrpc has somehow to take into
> consideration.
> 
> Can anyone help me with that one ? I don't know anything about RPC and XML,
> i don't know what i need to do know to fix dokuwikixmlrpc.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: name for a mutually inclusive relationship

2021-02-24 Thread 2QdxY4RzWzUUiLuE
On 2021-02-24 at 08:12:58 -0800,
Ethan Furman  wrote:

> I'm looking for a name for a group of options that, when one is specified, 
> all of them must be specified.
> 
> For contrast,
> 
> - radio buttons: a group of options where only one can be specified (mutually 
> exclusive)
> - check boxes:   a group of options that are independent of each other (any 
> number of
>  them may be specified)
> 
> - ???: a group of options where, if one is specified, all must be specified 
> (mutually
>inclusive)

I've run across this before, and almost always end up incorporating one
option or the other.  For example, consider a program's output.  By
default, it goes to stdout, but the program also supports plain output,
logging, and syslog.  So instead of mutually inclusive (for lack of a
better phrase right now) options:

  --output-disposition [logging|syslog|flatfile]
  --output-options [depends on --output-disposition]

do this:

  --output-to-log logging-options
  --output-to-syslog syslog-options
  --output-to-plain-file plain-file-options

aka three mutually *exclusive* options.

> So far, I have come up with:
> 
> - the Three Musketeers
> - clique
> - club
> - best friends
> - tight knit
> - group
> 
> Is there a name out there already to describe that concept?

Codependent?
Circularly dependent?
Entangled?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: name for a mutually inclusive relationship

2021-02-24 Thread 2QdxY4RzWzUUiLuE
On 2021-02-24 at 13:05:05 -0800,
Ethan Furman  wrote:

> entangled (none or all):
> 
>image size override:  height   width

IMO, that's *one* option (-s 640x480 or -s 640,480), not two.  In
argparse/optparse terms, a required argument with a custom type.

(OTOH, in a GUI, it'd be two separate mandatory text fields, both
controlled (i.e., enabled or disabled) by one checkbox.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: name for a mutually inclusive relationship

2021-02-24 Thread 2QdxY4RzWzUUiLuE
On 2021-02-24 at 13:31:42 -0800,
Ethan Furman  wrote:

> On 2/24/21 1:23 PM, 2qdxy4rzwzuui...@potatochowder.com wrote:
> 
> > > entangled (none or all):
> > > 
> > > image size override:  height   width
> > 
> > IMO, that's *one* option (-s 640x480 or -s 640,480), not two.  In
> > argparse/optparse terms, a required argument with a custom type.
> > 
> > (OTOH, in a GUI, it'd be two separate mandatory text fields, both
> > controlled (i.e., enabled or disabled) by one checkbox.)
> 
> I didn't say it was a good example.  ;-)  Hopefully it gets the idea across.

Ditto.  ;-)

IMO, the whole idea of "my program has two options, and the user has to
specify both or neither," isn't a question of whether or not the
argument parsing library supports it, but a question of whether or not
it's a good API.

Is the following a good API for a function?

def override_image_size(image, height=0, width=0):
'''specify height and width, or set both to 0'''
if (height == 0 and width != 0) or (height != 0 and width == 0):
raise ArgumentError('both or neither')
if height != 0 and width != 0:
image.height = height
image.width = width

Or would you more likely write something like this:

def override_image_size(image, size):
'''size is a (height, width) tuple'''
image.height, image.width = size

So why is the former a good API for a command line utility?

Or maybe I don't write command line utilitis with sufficiently complex
argument structures.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: editor recommendations?

2021-02-26 Thread 2QdxY4RzWzUUiLuE
On 2021-02-26 at 06:51:02 -0800,
Ethan Furman  wrote:

> [...] vim [...] truly black screens with it.  By which I mean that I
> have a colorful window title bar, a light-grey menu bar, and then a
> light-grey frame around the text-editing window (aka the only window),
> and a nice, black-background editing area.

That's what my emacs looks like, minus the light-grey frame (the window
manager's frame and border are enough for me).  Emacs has themes now,
but my setup is very old; all I did was set the "base" text background
and foreground colors and set a flag that says "I have a dark background
rather than a light one."

Did you have a Python question?  ;-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pygame "font not intialized"

2021-03-12 Thread 2QdxY4RzWzUUiLuE
On 2021-03-12 at 17:27:36 -0500,
Quentin Bock  wrote:

> my code is almost identical to this so I hope it's okay that I don't
> include all of my code
> error message:
> font = pygame.font.Font('freesansbold.ttf', 32)
> pygame.error: font not initialized
> 
> Tutorial Link: https://www.youtube.com/watch?v=FfWpgLFMI7w
> Timestamp to this part of the video:  1:55:08

https://duckduckgo.com/?q=pygame.error%3A+font+not+initialized

See also .
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: convert script awk in python

2021-03-26 Thread 2QdxY4RzWzUUiLuE
On 2021-03-26 at 21:06:19 -0400,
Avi Gross via Python-list  wrote:

> A generator that opens one file at a time (or STDIN) in a consistent
> manner, would be a reasonable thing to have as part of emulating AWK.

https://docs.python.org/3/library/fileinput.html
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A 35mm film camera represented in Python object

2021-04-01 Thread 2QdxY4RzWzUUiLuE
On 2021-04-01 at 18:10:46 -0400,
Richard Damon  wrote:

> On 4/1/21 5:47 PM, D.M. Procida wrote:
> > D.M. Procida  wrote:
> >
> >> Hi everyone, I've created  -
> >> a representation of a Canonet G-III QL17 in Python.
> >>
> >> There's also documentation: .
> >>
> >> It's a pure Python model of the physical sub-systems of a camera and
> >> their interactions. So far it's at a fairly high level - I haven't yet
> >> got down to the level of individual springs and levers yet.
> > I'm now working on the exposure system, which in the published version
> > so far just models inputs and outputs of the system as a whole. I want
> > to start to model its functionality by modelling the interactions of its
> > components - the levers, cams and so on.
> >
> > It seems to me I have fundamentally two approaches that I could take:
> >
> > The first is to model the movement of each lever as a series of discrete
> > steps (hundreds of tiny steps, to maintain accuracy). Pro: it models
> > movement through physical positions; con: it's ugly that it breaks
> > continuous movement into arbitrary steps.
> >
> > The second is not to move each lever in such detail, but to consider its
> > interactions: given the state of each of the other parts of the system,
> > what *would* be the outcome if something were to try to move the lever
> > from such-and-such a position to another? Pro: it feels more elegant
> > than the brute-force stepping of the alternative; con: it also feels
> > like a bit of a cheat.
> >
> > But neither approach really captures for me the interaction of moving
> > analog components, which in the case of this camera also have some
> > circular logic to them - the movement of A determines that of B which
> > determines that of C which determines that of D which finally also
> > affects the movement of A.
> >
> > Any thoughts or wise ideas? 
> >
> > Daniele
> 
> If you keep track of the positions as a floating point number, the
> precision will be more than you could actually measure it.

I won't disagree with Richard, although I will give you a general
warning about floating point rounding issues:  if you do, in fact, end
up with your first solution and lots and lots (millions?  billions?
more?) of discrete calculations, be aware that what looks like a lot of
precision in the beginning may not be all that precise (or accurate) in
the end.

Also, doesn't the overall motion of the camera as a whole also depend on
external factors, such as whether/how it's mounted or handheld, the
nature of the "ground" (e.g., soft wet sand vs. hard concrete
vs. someone standing on a boat in the water), an experienced
photographer "squeezing" the shutter release vs. a newbie "pressing the
button"?  I can think of plenty of variables; I guess it depends on what
you're trying to model and how accurate you intend to be (or not to be).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Friday Finking: initialising values and implied tuples

2021-04-02 Thread 2QdxY4RzWzUUiLuE
On 2021-04-02 at 19:25:07 -0300,
Marco Ippolito  wrote:

> > (a) basic linear presentation:
> > 
> > resource = "Oil"
> > time = 1
> > crude = 2
> > residue = 3
> > my_list = "long"
> > 
> > (b) using explicit tuples:
> > 
> > ( resource, time, crude, residue, my_list ) = ( "Oil", 1, 2, 3, "long" )
> > 
> > (c) linear and indented tuples:
> > 
> > (
> > resource,
> > time,
> > crude,
> > residue,
> > my_list
> > ) = (
> > "Oil",
> > 1,
> > 2,
> > 3,
> > "long"
> > )
> 
> Choose: (a).

I agree.

That said, I sometimes end up with something like this:

( resource, time, crude, residue, my_list ) = \
( "Oil", 1, 2, 3, "long" )

which mitigates matching up which value goes with which name.  But as
soon as either tuple overflows one physical line, it's back to (a).

I did go through a phase where I tried (c); note the past tense.

> In (b) I have a problem matching identifiers to values horizontally at
> a glance and in (c) I have the same problem vertically: i.e. "is 3 the
> value for residue or crude above/to-the-left?"
> 
> Cognitive burden slows down and fatigues.

Yep.

> Alternatively, if the data "fits together", use a `namedtuple` with
> kwarg initialisation or structured data types like `dataclasses`.

IMO, that's usually more troule than it's worth, although

thing = new_thing(
resource="Oil",
time=1,
crude=2,
residue=3,
my_list="long")

is often better than a collection of positional parameters.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Friday Finking: initialising values and implied tuples

2021-04-02 Thread 2QdxY4RzWzUUiLuE
On 2021-04-03 at 02:41:59 +0100,
Rob Cliffe via Python-list  wrote:

>     x1 = 42; y1 =  3;  z1 = 10
>     x2 = 41; y2 = 12; z2 = 9
>     x3 =  8;  y3 =  8;  z3 = 10
> (please imagine it's in a fixed font with everything neatly vertically
> aligned).
> This has see-at-a-glance STRUCTURE: the letters are aligned vertically
> and the "subscripts" horizontally.  Write it as 9 lines and it becomes
> an amorphous mess in which mistakes are harder to spot.

I agree that writing it as 9 lines is an accident waiting to happen, but
if you must see that structure, then go all in:

(x1, y1, z1) = (43,  3, 10)
(x2, y2, z2) = (41, 12,  9)
(x3, y3, z3) = ( 8,  8, 10)

Or even:

((x1, y1, z1)
 (x2, y2, z2)
 (x3, y3, z3)) = ((43,  3, 10)
  (41, 12,  9)
  ( 8,  8, 10))

Or not.  YMMV.  I guess this doesn't come up enough in my own code to
worry about it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Friday Finking: initialising values and implied tuples

2021-04-04 Thread 2QdxY4RzWzUUiLuE
On 2021-04-05 at 11:47:53 +1200,
dn via Python-list  wrote:

> Did you spot how various contributors identified when they prefer one
> method in a specific situation, but reach for another under differing
> circumstances!

What?  Use cases matter?  I'm *shocked*.  :-/

Of all the methodologies I used since those dinosaurs-on-the-treadmill
days (you had *dinosaurs*; we had to evolve fish with legs to relieve us
from having to run on our own treadmills), use cases was the one I
continued to pull out long after that department went away.

If I have a larger project that uses 3D points, I *might* reach for a
named tuple, but if it's smaller and I know that there will only ever be
two points, then px, py, pz, qx, qy, and qz is good enough.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: question about basics of creating a PROXY to MONITOR network activity

2021-04-08 Thread 2QdxY4RzWzUUiLuE
On 2021-04-09 at 00:17:59 +1000,
Chris Angelico  wrote:

> Also, you'd better be really REALLY sure that your monitoring is
> legal, ethical, and not deceptive.

Not to mention *secure*.  Your monitor increases the attack surface of
the system as a whole.  If I break into your monitor, can I recover
passwords (yours, users, servers, etc.)?  Can I snoop on traffic?  Can I
snoop metadata (like when which users are talking to which servers) not
otherwise available on your network?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Style qeustion: Multiple return values

2021-04-12 Thread 2QdxY4RzWzUUiLuE
On 2021-04-12 at 09:54:13 -0700,
Dan Stromberg  wrote:

> On Mon, Apr 12, 2021 at 1:30 AM Steve Keller  wrote:
> 
> > Just a short style question: When returning multiple return values, do
> > you use parenthesis?
> >
> > E.g. would you write
> >
> > def foo():
> > return 1, 2
> >
> > a, b = foo()
> >
> > or do you prefer
> >
> > def foo():
> > return (1, 2)
> >
> > (a, b) = foo()
> >
> 
> I prefer the parens; it's too easy to miss a comma when reading.   The
> parentheses make it more clear that you're looking at a tuple.

If I were to write it like this:

a,b = foo()

rather than like this:

a, b = foo()

then I might agree that the comma might be mistakable for a dot or even
"disappear" altogether.  To the compiler, the extraneous space doesn't
matter, but to a human reader, it makes a huge difference.

That said, consider this function:

def f(*args):
whatever()

Do you prefer f(a, b) or f((a, b))?

Okay, so it's a trick question, but that's how I think about *returning*
multiple values, too:  "a, b" is two values, but "(a, b)" is one value
that happens to be a tuple.

Lastly, with names like foo, a, and b, we might be misfocused.  Does any
of this change with names like coordinate, account_number, or distance?
Do meaningful names help, or do they merely distract from the commas and
the parentheses?

(x_coord, y_coord) = locate_target(parameters)
likely_name, probability = best_match(parameters)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Comparing text strings

2021-04-12 Thread 2QdxY4RzWzUUiLuE
On 2021-04-12 at 16:11:21 -0700,
Rich Shepard  wrote:

> I'm running Slackware64-14.2 and keep a list of installed packages. When a
> package is upgraded I want to remove the earlier version, and I've not
> before written a script like this. Could there be a module or tool that
> already exists to do this? If not, which string function would be best
> suited to the task?
> 
> Here's an example:
> atftp-0.7.2-x86_64-2_SBo.tgz
> atftp-0.7.4-x86_64-1_SBo.tgz
> 
> and there are others like this. I want the python3 script to remove the
> first one. Tools like like 'find' or 'sort -u' won't work because while the
> file name is the same the version or build numbers differ.
> 
> All suggestions welcome.

The trick to this is to define "one" in the phrase "first one."  What
makes a package name a first one, or a second one?  Is it the prefix
before the first hyphen?  Always?  Does Slackware define all the
possible bits, pieces, and permutations of package names, versions,
builds, etc.?

I don't know whether or how Slackware handles "compound" package names
(e.g., python-flask), but at some point, you're going to have to pull
apart (aka---gasp--parse), the package names to come up with the name
itself and the version (and the architecture, etc.), compare the name
parts to find the "duplicates," and then compare the versions (and maybe
the build number) to eliminate the lesser ones.

You'll also have to watch for the transitions from, say, 0.9 to 0.10, or
from 1.0rc2 to 1.0, which may not sort simply.

So to answer your question, a string function like split is a good
start.  IMO, if you think about the edge cases early, you'll have a
better chance at not being bitten by them.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Not found in the documentation

2021-04-26 Thread 2QdxY4RzWzUUiLuE
On 2021-04-26 at 18:24:18 -0700,
elas tica  wrote:

> [...] I was expecting to find [a description of what str returns for
> various types] in the Built-in Types section from the PSL
> documentation. The representation returned by str over a complex
> number is not stated. The same for fraction objects [...]

Assuming that PSL is the Python Standard Library,
https://docs.python.org/3/library/functions.html#func-str says:

[...] str(object) returns object.__str__(), which is the “informal”
or nicely printable string representation of object [...]

IMO, an exhaustive list of exactly what that means for every type, even
the built in types, is unnecessary, and could create maintenance or
compatibility issues.

Is there a reason you need that information?  What would you do with it
if you had it?  Completely speculatively on my part, would repr
(https://docs.python.org/3/library/functions.html#repr) meet your
need(s) better than str?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: name for new Enum decorator

2021-05-27 Thread 2QdxY4RzWzUUiLuE
On 2021-05-27 at 20:23:29 -0700,
Regarding "name for new Enum decorator,"
Ethan Furman  wrote:

> Greetings!
> 
> The Flag type in the enum module has had some improvements, but I find it
> necessary to move one of those improvements into a decorator instead, and
> I'm having a hard time thinking up a name.
> 
> What is the behavior?  Well, a name in a flag type can be either canonical
> (it represents one thing), or aliased (it represents two or more things).
> To use Color as an example:
> 
> class Color(Flag):
> RED = 1# 0001
> GREEN = 2  # 0010
> BLUE = 4   # 0100
> PURPLE = RED | BLUE# 0101
> WHITE = RED | GREEN | BLUE # 0111
> 
> The flags RED, GREEN, and BLUE are all canonical, while PURPLE and WHITE are
> aliases for certain flag combinations.  But what if we have something like:
> 
> class Color(Flag):
> RED = 1# 0001
> BLUE = 4   # 0100
> WHITE = 7  # 0111
> 
> As you see, WHITE is an "alias" for a value that does not exist in the Flag
> (0010, or 2).  That seems like it's probably an error.  But what about this?
> 
> class FlagWithMasks(IntFlag):
> DEFAULT = 0x0
> 
> FIRST_MASK = 0xF
> FIRST_ROUND = 0x0
> FIRST_CEIL = 0x1
> FIRST_TRUNC = 0x2
> 
> SECOND_MASK = 0xF0
> SECOND_RECALC = 0x00
> SECOND_NO_RECALC = 0x10
> 
> THIRD_MASK = 0xF00
> THIRD_DISCARD = 0x000
> THIRD_KEEP = 0x100
> 
> Here we have three flags (FIRST_MASK, SECOND_MASK, THIRD_MASK) that are
> aliasing values that don't exist, but it seems intentional and not an error.
> 
> So, like the enum.unique decorator that can be used when duplicate names
> should be an error, I'm adding a new decorator to verify that a Flag has no
> missing aliased values that can be used when the programmer thinks it's
> appropriate... but I have no idea what to call it.
> 
> Any nominations?

Exhaustive?

I see two qualitatively different kinds of enums:  those that can/should
be "or"ed together (e.g., RED, GREEN) and those that cannot (e.g.,
FIRST_ROUND, FIRST_CEIL, and FIRST_TRUNC).  The idea of a "missing"
value doesn't seem to apply to the second type (because defining
FIRST_TRUNC as FIRST_ROUND | FIRST_CEIL is nonsensical).  I don't quite
know what that may or may not suggest as far as names go, but it may
give someone else an idea.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The sqlite3 timestamp conversion between unixepoch and localtime can't be done according to the timezone setting on the machine automatically.

2021-08-31 Thread 2QdxY4RzWzUUiLuE
On 2021-09-01 at 08:36:55 +1200,
dn via Python-list  wrote:

> ... there is less consideration about working-out what time it is in
> Pune cf Kolkata, than between (say) San Francisco and Denver -
> although they are in the same country, are they in the same time-zone,
> or not?  (they aren't!)

What about Phoenix?  In the winter, it's the same time there as it is in
San Francisco, but in the summer, it's the same time there as it is in
Denver (Phoenix doesn't observe Daylight Saving Time).

And then there's Indiana, a medium size state that tends to get ignored
(they used to advertise "there's more than just corn in Indiana").  Most
of Indiana is in US/Eastern, but the cities that are (for practical
purposes) suburbs of Chicago are in US/Central (aka America/Chicago).

ChrisA is right; you can't make this [stuff] up.

Having lived in the United States my entire life (and being a nerd), I
can confirm that (1) I'm used to it and handle it as well as possible,
but (2) many people are not and don't.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The sqlite3 timestamp conversion between unixepoch and localtime can't be done according to the timezone setting on the machine automatically.

2021-08-31 Thread 2QdxY4RzWzUUiLuE
On 2021-09-01 at 07:32:43 +1000,
Chris Angelico  wrote:

> On Wed, Sep 1, 2021 at 7:17 AM <2qdxy4rzwzuui...@potatochowder.com> wrote:

> > What about Phoenix?  In the winter, it's the same time there as it is in
> > San Francisco, but in the summer, it's the same time there as it is in
> > Denver (Phoenix doesn't observe Daylight Saving Time).
> 
> I prefer to say: In winter, San Francisco (or Los Angeles) is the same
> as Phoenix, but in summer, Los Angeles changes its clocks away, and
> Denver changes to happen to be the same as Phoenix.

Not exactly.  Sort of.  Phoenix and Denver are both in America/Denver
(aka US/Mountain), but only Denver observes DST.  San Francisco and Los
Angeles are both in America/Los_Angeles, and both observe DST.

> At least the US has governed DST transitions. As I understand it, any
> given city has to follow one of the standard time zones, and may
> EITHER have no summer time, OR transition at precisely 2AM/3AM local
> time on the federally-specified dates. (I think the EU has also
> mandated something similar for member states.)

That's my understanding, too.

> If we could abolish DST world-wide, life would be far easier. All the
> rest of it would be easy enough to handle.

Agreed.

> ... I think Egypt (Africa/Cairo) is currently in the lead for weirdest
> timezone change ...

Yeah, I read about that somewhere.  Remember when the Pope declared that
September should skip a bunch of days?

> > Having lived in the United States my entire life (and being a nerd), I
> > can confirm that (1) I'm used to it and handle it as well as possible,
> > but (2) many people are not and don't.
> 
> Yup, absolutely. I've been working internationally for a number of
> years now, so my employment has been defined by a clock that isn't my
> own. I got used to it and developed tools and habits, but far too many
> people don't, and assume that simple "add X hours" conversions
> suffice.

Way back in the 1990s, I was working with teams in Metro Chicago, Tel
Aviv, and Tokyo (three separate teams, three really separate time zones,
at least two seaprate DST transition dates).  I changed my wristwatch to
24 hour time (and never looked back).  I tried UTC for a while, which
was cute, but confusing.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Scraping of Google Map to get the address of locations for given geocodes.

2021-09-02 Thread 2QdxY4RzWzUUiLuE
On 2021-09-02 at 20:12:19 -0300,
Michio Suginoo  wrote:

> I have the following question regarding how to scrape Google Map to get
> address based on a given list of geocodes.
> Given a list of geocodes (latitude; longitude) of locations, I would like
> to scrape municipalities of all the spots in the list. How can I do that?
> 
> For example, I have
> 
>   lat  lon
> 
> 1) -34.5722317  -58.4314464
> 
> 2) -34.553906   -58.4520949
> 
> 3) -34.5661444  -58.4964289
> 
> 4) -34.5648053  -58.4431567
> 
> 5) -34.5664089  -58.4323004
> 
> 6) -34.5664089  -58.4323004
> 
> 
> 
> And I want to get the name of municipality (city/town), or alternatively
> the address, of each location.
> 
> 
> I would appreciate it, if anyone can advise me how to do that.

Is scraping Google Map a hard requirement?

Both geopy¹ and geocoder² can translate those coordinates into street
addresses directly, and their documentation even explains how to bypass
the libraries and go straight to the service providers.

¹ https://geopy.readthedocs.io/en/latest/
² https://geocoder.readthedocs.io/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: on writing a while loop for rolling two dice

2021-09-06 Thread 2QdxY4RzWzUUiLuE
On 2021-09-06 at 20:11:41 -0400,
Avi Gross via Python-list  wrote:

> And in the python version, has anyone made a generator that returned
> NULL or the like so you can say uselessly:
> 
> for ( _ in forever() ) ...

while "forever":
...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Friday Finking: Contorted loops

2021-09-09 Thread 2QdxY4RzWzUUiLuE
On 2021-09-09 at 22:33:16 +,
Stefan Ram  wrote:

>   One can think of a language where every loop is exited this
>   way, the only loop construct would be
> 
> loop
> ...
> 
>   and it would /always/ have to be exited via enclosed breaks.

I'm not quite sure what you mean by "one can," but that's how the simple
form of Common Lisp's loop macro works.  Friendlier looping constructs
are built with other macros on top of that one and/or its underlying
mechanism.¹

¹ The underlying mechanism is the moral equivalent of a Python suite
that can also contain tags and unconditional jumps to those tags, aka
"goto"s.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Friday Finking: Contorted loops

2021-09-10 Thread 2QdxY4RzWzUUiLuE
On 2021-09-10 at 15:08:19 -0600,
Joe Pfeiffer  wrote:

> r...@zedat.fu-berlin.de (Stefan Ram) writes:

> >   The existence of statements like "break" renders 
> >   proof techniques for loops (such as Hoare's) with
> >   their invariants and inference rules unapplicable.
> 
> Also the reason to avoid repeat-until loops:  the loop "invariant" isn't
> the same on the first iteration as on subsequent iterations.

I am by no means an expert, nor likely even a neophyte, but why would
the loop invariant not be the same on the first iteration?

I can certainly see that the exit condition may not make sense at the
beginning of the first iteration (e.g., there is not yet any data to
compare to the sentinel), but ISTM that claiming that the exit condition
is a loop invariant isn't kosher (because all you're claiming is that
the compiler works).

I can also see that certain state information may not be captured until
the end of the first iteration.  But presumably said state information
can change from iteration to iteration, so I can't see how you'd derive
an invariant involving it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Friday Finking: Contorted loops

2021-09-12 Thread 2QdxY4RzWzUUiLuE
On 2021-09-11 at 18:21:17 +0100,
Alan Gauld via Python-list  wrote:

> On 11/09/2021 15:41, Peter J. Holzer wrote:
> 
> > How is C's do/while loop more horrible than Pascal's repeat/until? 
> 
> Because it is very hard to spot or distinguish from a normal
> while loop.
> 
> while condition ;
> 
> Is a valid (and fairly common) loop in C
> 
> so code that has
> 
> do{
> code
> }
> while condition;
> 
> Looks, for non-trivial cases, like a lot of code followed
> by an empty while loop.
> 
> The do is easy to miss  and the while loop disguised as
> a repeat termination is confusing.

Well, yeah, except that only a macro would ever write it that way.  :-)

At the very least, the code would be indented (making it easier to spot
the "do," before or after you see the while; and my apologies if you
intended it that way and it got lost somewhere between your intent and
my monitor):

do {
code;
}
while(condition);

(Side question:  why put the "{" next to the "do," but the "}" and the
"while" on separate lines?)

And I would put the while on the same line as the closing brace (which
is also where I put the "else" in an if statement):

do {
code;
} while(condition);
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Polymorphic imports

2021-09-21 Thread 2QdxY4RzWzUUiLuE
On 2021-09-22 at 05:10:02 +1000,
Chris Angelico  wrote:

> You can dynamically import modules using importlib.import_module(),
> but an easier way might just be a conditional import:
> 
> # client/__init__.py
> if some_condition:
> import module_a_default as module_a
> else:
> import module_a_prime as module_a
> 
> Now everything that refers to client.module_a.whatever will get the
> appropriate one, either the original or the alternate.

+1

> Alternatively, since you are talking about paths, it might be easiest
> to give everything the same name, and then use sys.path to control
> your import directories. Not sure which would work out best.

-1

Please don't do that.  Mutable shared and/or global state (i.e.,
sys.paths) is the root of all evil.  And homegrown crypto and date
libraries.  And those funny red hats.
-- 
https://mail.python.org/mailman/listinfo/python-list


  1   2   >