[Python-ideas] Re: itertools.compress default selectors

2021-09-14 Thread [email protected]
OK, If I might tell some story... :) Main motivation was a use case where we gather command line options, and some of them are… optional. Below is a realistic example. Namely options for LLVM Clang: >>> flags = [“-O3”, “-fomit-frame-pointer”, maybe_pgo(context)] Here “maybe_pgo” returns either

[Python-ideas] Re: itertools.compress default selectors

2021-09-14 Thread Paul Moore
On Tue, 14 Sept 2021 at 08:38, [email protected] wrote: > Main motivation was a use case where we gather command line options, and some > of them are… optional. [...] > And yet I’m solid that we need some compact and nice way for rendering > strings with command options. That would be a thin

[Python-ideas] Re: itertools.compress default selectors

2021-09-14 Thread Steven D'Aprano
On Tue, Sep 14, 2021 at 11:31:43AM +0400, [email protected] wrote: > Thus I have collection of options and some of them are empty or None. > In order to get rendered command line options string I use “compress” > in conjunction with “join": > > >>> opts = " ".join(compress(flags, flags)) Wh

[Python-ideas] Re: classmethod __eq__ comparisons and Any wildcards

2021-09-14 Thread johnmelendowski
As pointed out, you want metaclass for this. A warning, I've overwritten many of the dunders on classes for fun and I actually broke the ability to repr my class in certain situations by overriding __eq__ and/or __hash__ something deep in ipython/python with the repr. I forget the exact situatio

[Python-ideas] Re: classmethod __eq__ comparisons and Any wildcards

2021-09-14 Thread Chris Angelico
On Tue, Sep 14, 2021 at 11:30 PM wrote: > > I think I implemented __eq__ but not __hash__ and it broke the golden rule of > x == y => x is y i,.e. hash(x) == hash(y) > Not sure what you're referring to there. The rule regarding hashes is that if x == y, then hash(x) == hash(y). Identity doesn't

[Python-ideas] Addition of a "plus-minus" binary numeric operator

2021-09-14 Thread yahbai
Hi all, I was wondering on whether there is any interest in introducing a "plus-minus" operator: Conceptually very simple; instead of: upper, lower = a + b, a - b use instead: upper, lower = a +- b In recent projects I've been working on, I've been having to do the above "plu

[Python-ideas] Re: Addition of a "plus-minus" binary numeric operator

2021-09-14 Thread Paul Moore
I doubt it, it seems way too specialised to be worth making into a language feature. If you want to, you can write a function: def limits(a, b): return a+b, a-b Paul On Tue, 14 Sept 2021 at 14:55, wrote: > > Hi all, > > I was wondering on whether there is any interest in introducing a > "

[Python-ideas] Re: Addition of a "plus-minus" binary numeric operator

2021-09-14 Thread Chris Angelico
On Tue, Sep 14, 2021 at 11:58 PM wrote: > > Hi all, > > I was wondering on whether there is any interest in introducing a > "plus-minus" operator: > > Conceptually very simple; instead of: > > upper, lower = a + b, a - b > > use instead: > > upper, lower = a +- b > > In recent pro

[Python-ideas] Re: Addition of a "plus-minus" binary numeric operator

2021-09-14 Thread Jonathan Fine
Hi Yahbai You might wish to reconsider your proposal in light of existing behaviour >>> 1+-1 0 Consider also your proposed >>> a, b = 2 +- 1 We know that {a, b} = {1, 3}. But which is which? In your use case you might be better off introducing a custom type >>> lims = pm(2, 1) >>> lims.lo, lims.

[Python-ideas] Re: Addition of a "plus-minus" binary numeric operator

2021-09-14 Thread Ir. Robert Vanden Eynde
Add you can call this function plusminus Then use the funcoperators module to write : upper, lower = a +plusminus- b pip install funcoperators Le mar. 14 sept. 2021 à 16:02, Paul Moore a écrit : > I doubt it, it seems way too specialised to be worth making into a > language feature. > > If you

[Python-ideas] Re: Addition of a "plus-minus" binary numeric operator

2021-09-14 Thread Ricky Teachey
May I ask what you are actually doing with upper and lower after creating them? Are you checking to see if another value is between? Something else? If they are always being used together and you're doing the same things with them all the time, it starts to smell like you might want to just write a

[Python-ideas] Re: itertools.compress default selectors

2021-09-14 Thread [email protected]
> The signature is wrong. Thanks for remark. Of course proper signature would be: def jin(a: Iterable[Optional[str]], sep=“ “): # … > Why do you (ab)use compress for that? Well, it seems that it is subjective. To me “[None, x, y, z]” -> “[x, y, z]” looks like “compression”. But if community

[Python-ideas] Re: itertools.compress default selectors

2021-09-14 Thread Chris Angelico
On Wed, Sep 15, 2021 at 4:03 AM [email protected] wrote: > > > The signature is wrong. > Thanks for remark. Of course proper signature would be: > > def jin(a: Iterable[Optional[str]], sep=“ “): > # … > > > Why do you (ab)use compress for that? > Well, it seems that it is subjective. To me

[Python-ideas] Re: itertools.compress default selectors

2021-09-14 Thread Tim Peters
[Chris Angelico ] > ... > For it to be accepted, you have to convince people - particularly, > core devs - that it's of value. At the moment, I'm unconvinced, but on > the other hand, all you're proposing is a default value for a > currently-mandatory argument, so the bar isn't TOO high (it's not l

[Python-ideas] Re: itertools.compress default selectors

2021-09-14 Thread Paul Moore
On Tue, 14 Sept 2021 at 19:58, Tim Peters wrote: > Except it's not that simple: Apologies, Tim, it took me a couple of reads to work out what you were saying here. I hope you won't mind if I restate the point for the benefit of anyone else who might have got confused it like I did... > def

[Python-ideas] Re: itertools.compress default selectors

2021-09-14 Thread Chris Angelico
On Wed, Sep 15, 2021 at 4:58 AM Tim Peters wrote: > Or perhaps the `compress()` implementation could grow internal > conditionals to use a different algorithm if the second argument is > omitted. But that would be a major change to support something that's > already easily done in more than one mo

[Python-ideas] os.workdir() context manager

2021-09-14 Thread Marc-Andre Lemburg
Hello all, I sometimes write Python scripts which need to work in specific work directories. When putting such code into functions, the outer function typically does not expect the current work dir (CWD) to be changed, so wrap the code which need the (possibly) modified CWD using a simply context

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Paul Moore
On Tue, 14 Sept 2021 at 20:44, Marc-Andre Lemburg wrote: > > I sometimes write Python scripts which need to work in specific > work directories. > > When putting such code into functions, the outer function typically > does not expect the current work dir (CWD) to be changed, so wrap the > code wh

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Cameron Simpson
On 14Sep2021 21:43, M.-A. Lemburg wrote: >- The context manager is not thread safe. There's no thread safe model > for the current work dir. OTOH, scripts usually don't use threads, > so not a big deal. This is the source of my concerns. Though of course it applies to any process global state.

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Chris Angelico
On Wed, Sep 15, 2021 at 7:43 AM Cameron Simpson wrote: > > On 14Sep2021 21:43, M.-A. Lemburg wrote: > >- The context manager is not thread safe. There's no thread safe model > > for the current work dir. OTOH, scripts usually don't use threads, > > so not a big deal. > > This is the source of m

[Python-ideas] Re: Power Assertions: Is it PEP-able?

2021-09-14 Thread Finn Mason
I think that this is a great idea. However, pipes only point to one character, which can be confusing. (Citation: many tracebacks before 3.10.) I'm wondering: Could failed assertions step you into `pdb`, if they are used for testing purposes? Could there be a way to specify different levels of ass

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Guido van Rossum
Here I think we need to drop our perfectionist attitude. When I saw Marc-Andre's proposal my first response was also "but what about threads." But really, os.chdir() is the culprit here, and since it's a syscall we can't fix it. If we can live with that, we can live with the proposed os.workdir().

[Python-ideas] Re: Power Assertions: Is it PEP-able?

2021-09-14 Thread Juancarlo Añez
If assertions have an associated block, then `pdb` can be invoked within. I almost never use debuggers, so I don't remember, but I think a recent Python version introduced the likes of `debug()` to step into the pre-configured debugger. About the "power assertions" proposal in this thread, once ev

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Cameron Simpson
On 15Sep2021 07:50, Chris Angelico wrote: >On Wed, Sep 15, 2021 at 7:43 AM Cameron Simpson wrote: >> I know I'm atypical, but I have quite a lot of multithreaded stuff, >> including command line code. So while it'd be ok to avoid this context >> manager for my own code, I fear library modules, ei

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Cameron Simpson
On 14Sep2021 15:16, Guido van Rossum wrote: >Here I think we need to drop our perfectionist attitude. When I saw >Marc-Andre's proposal my first response was also "but what about threads." >But really, os.chdir() is the culprit here, and since it's a syscall we >can't fix it. If we can live with t

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Chris Angelico
On Wed, Sep 15, 2021 at 9:36 AM Cameron Simpson wrote: > > On 15Sep2021 07:50, Chris Angelico wrote: > >On Wed, Sep 15, 2021 at 7:43 AM Cameron Simpson wrote: > >> I know I'm atypical, but I have quite a lot of multithreaded stuff, > >> including command line code. So while it'd be ok to avoid t

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Guido van Rossum
On Tue, Sep 14, 2021 at 4:36 PM Cameron Simpson wrote: > On 15Sep2021 07:50, Chris Angelico wrote: > >On Wed, Sep 15, 2021 at 7:43 AM Cameron Simpson wrote: > >> I know I'm atypical, but I have quite a lot of multithreaded stuff, > >> including command line code. So while it'd be ok to avoid th

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Finn Mason
On Tue, Sep 14, 2021, 5:36 PM Cameron Simpson wrote: > On 15Sep2021 07:50, Chris Angelico wrote: > >On Wed, Sep 15, 2021 at 7:43 AM Cameron Simpson wrote: > >> I know I'm atypical, but I have quite a lot of multithreaded stuff, > >> including command line code. So while it'd be ok to avoid this

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Finn Mason
BTW, should it be `WorkDir` instead of `workdir` because it's a class, or would that be too inconsistent? On Tue, Sep 14, 2021, 6:47 PM Finn Mason wrote: > > On Tue, Sep 14, 2021, 5:36 PM Cameron Simpson wrote: > >> On 15Sep2021 07:50, Chris Angelico wrote: >> >On Wed, Sep 15, 2021 at 7:43 AM

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Guido van Rossum
For the stdlib context managers that I know of, all-lowercase seems the convention. Would it even be a class? The simplest implementation would use contextlib.contextmanager (unless that’s a undesirable dependency for os.py). —Guido On Tue, Sep 14, 2021 at 17:53 Finn Mason wrote: > BTW, should

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread David Mertz, Ph.D.
I think this would be convenient. And yes, it's not thread safe. But neither is os.chdir() to start with. Someone whose script, or library, wants to chdir can already shoot themselves in the foot. This makes that slightly less likely, not more. In terms of the bikeshed color, I think putting this

[Python-ideas] Otherwise clause in for statement to run when a empty iterable is used

2021-09-14 Thread Andre Delfino
When working with generators, AFAIK, there's currently no easy way to handle the case of an empty generator (which can be useful if such case is an error). Converting the generator to a, say, list, is not a solution if the generator is intrinsically infinite. I propose to have an "otherwise" cl

[Python-ideas] Re: Otherwise clause in for statement to run when a empty iterable is used

2021-09-14 Thread Andre Delfino
This should have been: When working with generators in a for statement, AFAIK, there's currently no easy way to handle the case of an empty generator (which can be useful if such case is an error). ___ Python-ideas mailing list -- [email protected]

[Python-ideas] Re: Otherwise clause in for statement to run when a empty iterable is used

2021-09-14 Thread Valentin Berlier
I find that when I run into a similar scenario the reason why I need the iterable to be non-empty is because I'm trying to find something in it, and for this the `else` clause works pretty well: for item in get_items(): if check(item): do_thing(item) break else: raise Val

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Finn Mason
On Tue, Sep 14, 2021, 7:58 PM David Mertz, Ph.D. wrote: > I think this would be convenient. > > And yes, it's not thread safe. But neither is os.chdir() to start with. > Someone whose script, or library, wants to chdir can already shoot > themselves in the foot. This makes that slightly less like

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread David Mertz, Ph.D.
On Tue, Sep 14, 2021, 11:17 PM Finn Mason wrote: > I disagree. The way I see it, `pathlib` is home of *Path(). No functions. > Just *Path(). Then again, I didn't write pathlib. I could be > misunderstanding the intent. > When I wrote "in" I was thinking more of a method than a function. E.g. wi