Re: [Python-ideas] Let try-except check the exception instance

2018-05-30 Thread Franklin? Lee
Would guards (such as from the switch-case discussions) be a good fit? try: ... except RuntimeError as e if e.message == "tie": ... On Thu, May 31, 2018, 00:48 Danilo J. S. Bellini wrote: > Hi! > I was working on handling some exceptions from external software > (e.g. database

Re: [Python-ideas] Let try-except check the exception instance

2018-05-30 Thread Steven D'Aprano
On Thu, May 31, 2018 at 01:47:17AM -0300, Danilo J. S. Bellini wrote: > >>> try: > ... # [...] > ... session.commit() # Here it raises! > ... # [...] > ... except DatabaseError as exc: > ... msg = get_db_error_msg_from_exception(exc) > ... if msg == "beyond_limit": > ...

[Python-ideas] Let try-except check the exception instance

2018-05-30 Thread Danilo J. S. Bellini
Hi! I was working on handling some exceptions from external software (e.g. database constraint triggers) switching the handler based on the messages that had been sent. Today we can do something like (running on Python 3.6.5): >>> try: ... # [...] ... session.commit() # Here it raises!

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Neil Girdhar
On Wed, May 30, 2018 at 9:02 PM Steven D'Aprano wrote: > On Thu, May 31, 2018 at 10:05:33AM +1000, Chris Angelico wrote: > > On Thu, May 31, 2018 at 9:53 AM, Steven D'Aprano > wrote: > > >> There is no nice, equivalent := version as far as I can tell. > > > > > > Given (pun intended) the fact

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Neil Girdhar
On Wed, May 30, 2018 at 7:54 PM Steven D'Aprano wrote: > On Wed, May 30, 2018 at 01:59:37PM -0400, Neil Girdhar wrote: > > > This example shows additional flexibility: > > > > z = {a: transformed_b > > for b in bs > > given transformed_b = transform(b) > > for a in as_} > > Is

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Neil Girdhar
On Wed, May 30, 2018 at 8:10 PM Steven D'Aprano wrote: > On Thu, May 31, 2018 at 04:06:51AM +1000, Chris Angelico wrote: > > On Thu, May 31, 2018 at 3:59 AM, Neil Girdhar > wrote: > > > This example shows additional flexibility: > > > > > > z = {a: transformed_b > > > for b in bs > > >

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Steven D'Aprano
On Thu, May 31, 2018 at 10:05:33AM +1000, Chris Angelico wrote: > On Thu, May 31, 2018 at 9:53 AM, Steven D'Aprano wrote: > >> There is no nice, equivalent := version as far as I can tell. > > > > Given (pun intended) the fact that you only use transformed_b in a > > single place, I don't think

Re: [Python-ideas] Proposal: A Reduce-Map Comprehension and a "last" builtin

2018-05-30 Thread Rob Cliffe via Python-ideas
On 30/05/2018 17:05, Peter O'Connor wrote: On Thu, May 24, 2018 at 2:49 PM, Steven D'Aprano > wrote: On Thu, May 24, 2018 at 02:06:03PM +0200, Peter O'Connor wrote: > We could use given for both the in-loop variable update and the variable >

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Chris Angelico
On Thu, May 31, 2018 at 10:05 AM, Steven D'Aprano wrote: > On Thu, May 31, 2018 at 04:06:51AM +1000, Chris Angelico wrote: >> On Thu, May 31, 2018 at 3:59 AM, Neil Girdhar wrote: >> > This example shows additional flexibility: >> > >> > z = {a: transformed_b >> > for b in bs >> > given

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Steven D'Aprano
On Thu, May 31, 2018 at 04:06:51AM +1000, Chris Angelico wrote: > On Thu, May 31, 2018 at 3:59 AM, Neil Girdhar wrote: > > This example shows additional flexibility: > > > > z = {a: transformed_b > > for b in bs > > given transformed_b = transform(b) > > for a in as_} > > > > There

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Chris Angelico
On Thu, May 31, 2018 at 9:53 AM, Steven D'Aprano wrote: >> There is no nice, equivalent := version as far as I can tell. > > Given (pun intended) the fact that you only use transformed_b in a > single place, I don't think it is necessary to use := at all. > > z = {a: transform(b) for b in bs for

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Steven D'Aprano
On Wed, May 30, 2018 at 01:59:37PM -0400, Neil Girdhar wrote: > This example shows additional flexibility: > > z = {a: transformed_b > for b in bs > given transformed_b = transform(b) > for a in as_} Is that even legal? Again, you're putting half of the comprehension in the

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Adam Bartoš
A side comment: > potential_updates = { > y: potential_update > for x in need_initialization_nodes > for y in [x, *x.synthetic_inputs()] > given potential_update = command.create_potential_update(y) > if potential_update is not None} Probably, I would write @make_dict def

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Neil Girdhar
Peter wrote: > Well you could just do: z = {a: b for b in (transform(bi) for bi in bs) for a in as_} That works, but I prefer the implicit nesting of a sequence of "comp_for" expressions to a the nested generator. On Wed, May 30, 2018 at 2:16 PM Chris Angelico wrote: >

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Chris Angelico
On Thu, May 31, 2018 at 3:59 AM, Neil Girdhar wrote: > This example shows additional flexibility: > > z = {a: transformed_b > for b in bs > given transformed_b = transform(b) > for a in as_} > > There is no nice, equivalent := version as far as I can tell. True. However, it took

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Peter O'Connor
On Wed, May 30, 2018 at 7:59 PM, Neil Girdhar wrote: > > z = {a: transformed_b > for b in bs > given transformed_b = transform(b) > for a in as_} > > There is no nice, equivalent := version as far as I can tell. > Well you could just do: z = {a: b for b in

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Neil Girdhar
On Wed, May 30, 2018 at 1:56 PM Neil Girdhar wrote: > On Wed, May 30, 2018 at 1:52 PM Chris Angelico wrote: > >> On Thu, May 31, 2018 at 1:23 AM, Peter O'Connor >> wrote: >> >> In comparison, I think that := is much simpler. >> > >> > >> > In this case that's true, but a small modification:

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Neil Girdhar
On Wed, May 30, 2018 at 1:52 PM Chris Angelico wrote: > On Thu, May 31, 2018 at 1:23 AM, Peter O'Connor > wrote: > >> In comparison, I think that := is much simpler. > > > > > > In this case that's true, but a small modification: > > > > updates = { > > y:

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Chris Angelico
On Thu, May 31, 2018 at 1:23 AM, Peter O'Connor wrote: >> In comparison, I think that := is much simpler. > > > In this case that's true, but a small modification: > > updates = { > y: do_something_to(potential_update) > for x in need_initialization_nodes >

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Neil Girdhar
On Wed, May 30, 2018 at 11:32 AM Peter O'Connor wrote: > In comparison, I think that := is much simpler. > > > In this case that's true, but a small modification: > > updates = { > y: do_something_to(potential_update) > for x in need_initialization_nodes >

[Python-ideas] Add shutil.chown(..., recursive=False)

2018-05-30 Thread Wes Turner
On Wednesday, May 30, 2018, Nick Coghlan wrote: > On 30 May 2018 at 02:38, Guido van Rossum wrote: > >> Honestly, despite the occasional use case(1), I'm not sure that this is a >> battery we need in the stdlib. Nobody seems too excited about writing the >> code, and when the operation is

Re: [Python-ideas] Proposal: A Reduce-Map Comprehension and a "last" builtin

2018-05-30 Thread Peter O'Connor
On Thu, May 24, 2018 at 2:49 PM, Steven D'Aprano wrote: > On Thu, May 24, 2018 at 02:06:03PM +0200, Peter O'Connor wrote: > > We could use given for both the in-loop variable update and the variable > > initialization: > >smooth_signal = [average given average=(1-decay)*average + decay*x >

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Peter O'Connor
> > In comparison, I think that := is much simpler. In this case that's true, but a small modification: updates = { y: do_something_to(potential_update) for x in need_initialization_nodes for y in [x, *x.synthetic_inputs()] if

Re: [Python-ideas] Was `os.errno` undocumented?

2018-05-30 Thread Guido van Rossum
Mypy would totally catch this. PEP 484 has specific words for whether imports are re-exported (they aren't unless you use 'as'). On Wed, May 30, 2018 at 4:58 AM, Steven D'Aprano wrote: > On Wed, May 30, 2018 at 04:18:51AM +, Steve Barnes wrote: > > > Maybe what we need is to add a, possibly

Re: [Python-ideas] Add shutil.chown(..., recursive=False)

2018-05-30 Thread Nick Coghlan
On 30 May 2018 at 02:38, Guido van Rossum wrote: > Honestly, despite the occasional use case(1), I'm not sure that this is a > battery we need in the stdlib. Nobody seems too excited about writing the > code, and when the operation is needed, shelling out to the system chown is > not too

Re: [Python-ideas] A real life example of "given"

2018-05-30 Thread Steven D'Aprano
On Wed, May 30, 2018 at 02:42:21AM -0700, Neil Girdhar wrote: > With "given", I can write: > > potential_updates = { > y: potential_update > for x in need_initialization_nodes > for y in [x, *x.synthetic_inputs()] > given potential_update =

Re: [Python-ideas] Was `os.errno` undocumented?

2018-05-30 Thread Steven D'Aprano
On Wed, May 30, 2018 at 04:18:51AM +, Steve Barnes wrote: > Maybe what we need is to add a, possibly optional, or suppressible, > warning whenever the import system encounters an implicit/indirect > import? I don't think your terminology ("implicit/indirect") is very accurate. from os

[Python-ideas] A real life example of "given"

2018-05-30 Thread Neil Girdhar
I thought I would share a recent use I had for "given": I have this comprehension: potential_updates = {y: command.create_potential_update(y) for x in need_initialization_nodes for y in [x, *x.synthetic_inputs()]} I want to

Re: [Python-ideas] Add shutil.chown(..., recursive=False)

2018-05-30 Thread Wes Turner
Configuration management tools with far more code than this are regularly run with root privileges. OTOH, Salt and Ansible, for example, both support recursive chown and chmod; and report what actually changed. Yum/dnf probably do, too. Supporting recursive chmod/chown OOB may be useful. That it