[Python-ideas] Re: else without except

2023-06-30 Thread MRAB

On 2023-06-30 14:55, Daniel Walker wrote:

As most of you probably know, you can use else with try blocks:

try:
     do_stuff()
except SomeExceptionClass:
    handle_error()
else:
    no_error_occurred()

Here, no_error_occurred will only be called if do_stuff() didn't raise 
an exception.


However, the following is invalid syntax:

try:
    do_stuff()
else:
    no_error_occurred()

Now you might say that this isn't needed as you can achieve the same 
result with


do_stuff()
no_error_occurred()

However, what if I want to use finally as well:

try:
    do_stuff()
else:
    no_error_occurred()
finally:
    cleanup()

and I need no_error_occurred to be called before cleanup?  For my actual 
use case, I've done


try:
    do_stuff()
except Exception:
     raise
else:
    no_error_occurred()
finally:
    cleanup()

This seems very non-Pythonic.  Is there a reason why else without except 
has to be invalid syntax?



What would be the difference between

try:
   do_stuff()
else:
   no_error_occurred()
finally:
   cleanup()

and

try:
   do_stuff()
   no_error_occurred()
finally:
   cleanup()

?

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HFKEZV343UU7YDUV3FRBCC5D6DD37JLI/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] else without except

2023-06-30 Thread Daniel Walker
As most of you probably know, you can use else with try blocks:

try:
do_stuff()
except SomeExceptionClass:
   handle_error()
else:
   no_error_occurred()

Here, no_error_occurred will only be called if do_stuff() didn't raise an
exception.

However, the following is invalid syntax:

try:
   do_stuff()
else:
   no_error_occurred()

Now you might say that this isn't needed as you can achieve the same result
with

do_stuff()
no_error_occurred()

However, what if I want to use finally as well:

try:
   do_stuff()
else:
   no_error_occurred()
finally:
   cleanup()

and I need no_error_occurred to be called before cleanup?  For my actual
use case, I've done

try:
   do_stuff()
except Exception:
raise
else:
   no_error_occurred()
finally:
   cleanup()

This seems very non-Pythonic.  Is there a reason why else without except
has to be invalid syntax?
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/VSXRFQ7OEZJIOCGHWIT225XMGIARFH5J/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict method to retrieve a key from a value

2023-06-30 Thread Chris Angelico
On Sat, 1 Jul 2023 at 01:15, Christopher Barker  wrote:
> Totally different topic, but I do think that a "curated" package repo would 
> be helpful -- there is a lot of cruft on PyPi :-(
>

That idea gets thrown around every once in a while, but there are a
few problems with it. When you "bless" one package, every other
package doing a similar job will suffer, even if they are just as good
(but simply haven't been added to the curated collection). If the PSF
recommends a package, people will expect a lot of it, which is a huge
burden on the developer(s). And someone has to go through all those
packages, and then discuss it with whoever else has to be responsible
for this curated collection, and come to an agreement.

Instead, what I'd like to see is: Personal, individual blogs,
recommending packages that the author knows about and can give genuine
advice about. Provide YOUR curated collection. Then maybe a metapage
on the Python Wiki could link to some useful/interesting blog posts.

Decentralize!

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/OGFJOPQPNV4YH6QPOAJXKDWVIMXNHHIS/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict method to retrieve a key from a value

2023-06-30 Thread Christopher Barker
Google:

"python more collections pypi"

gets quite a few hits -- I haven't checked out any of them though.

Depending on a third party package does take some thought -- but for
something like this you could simply "vendor" a particular class (i.e.
include the code with yours).

Totally different topic, but I do think that a "curated" package repo would
be helpful -- there is a lot of cruft on PyPi :-(

-CHB


On Fri, Jun 30, 2023 at 3:47 AM Dom Grigonis  wrote:

> In my experience, the implementation of bijective dict is largely
> application specific.
>
> 1. Unique-valued dict is fairly straight forward and there is little
> ambiguity on implementation using 2 dicts.
> 2. However, if values are not to be unique, then it largely depends on
> application. (best (as in most efficient) pypi I have found for this:
> https://pypi.org/project/indexed/)
>
> But to me it feels that there is some sort of gap in container space. E.g. I
> have spent a reasonable amount of time on:
> a) 1-2-1 dict
> b) many-2-many dict
> c) dict-deque
> d) bijective-dict-deque
> e) list-dict
>
> Maybe it would be good to have similar package to `more_itertools`. E.g.
> `more_collections`, where assorted recipes are implemented.
>
> I am not a fan of making my libraries dependent on less known pypi
> packages, but when a package is referenced in python official docs, then I
> am much more at ease.
>
>
> On 30 Jun 2023, at 04:50, Andre Delfino  wrote:
>
> A dict method to retrieve the key of a value from a bijective dict would
> have come in handy to me in several occasions:
>
> names = {'one': 1}
> names.inverse()[1]
>
> 'one'
>
> names = {'one': 1, 'uno': 1}
> names.inverse()[1]
>
> ValueError: dict is not bijective
>
> My usual use case is when both keys and values have simple types like
> int/str, like:
>
> {
> 'account-a': 123,
> 'account-b': 456,
> }
>
> Users may enter the account name, or the ID. The system works with the
> account ID (translating from the account name if needed), but when it needs
> to mention the account to the user, it shows the account name instead.
>
> I do understand that retrieval wouldn't be O(1).
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/BWMIASE5YCDETFSZYAUG7TVWTMBFHKQW/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/DPZMOAICSV2YKBYWFSW36LW5GSPZFRNK/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6G26LGITB4UPWA7C6LJ627HU7RNG5GVJ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict method to retrieve a key from a value

2023-06-30 Thread Dom Grigonis
In my experience, the implementation of bijective dict is largely application 
specific.

1. Unique-valued dict is fairly straight forward and there is little ambiguity 
on implementation using 2 dicts.
2. However, if values are not to be unique, then it largely depends on 
application. (best (as in most efficient) pypi I have found for this: 
https://pypi.org/project/indexed/ )

But to me it feels that there is some sort of gap in container space. E.g. I 
have spent a reasonable amount of time on:
a) 1-2-1 dict
b) many-2-many dict
c) dict-deque
d) bijective-dict-deque
e) list-dict

Maybe it would be good to have similar package to `more_itertools`. E.g. 
`more_collections`, where assorted recipes are implemented.

I am not a fan of making my libraries dependent on less known pypi packages, 
but when a package is referenced in python official docs, then I am much more 
at ease.


> On 30 Jun 2023, at 04:50, Andre Delfino  wrote:
> 
> A dict method to retrieve the key of a value from a bijective dict would have 
> come in handy to me in several occasions:
> 
 names = {'one': 1}
 names.inverse()[1]
> 'one'
 names = {'one': 1, 'uno': 1}
 names.inverse()[1]
> ValueError: dict is not bijective
> 
> My usual use case is when both keys and values have simple types like 
> int/str, like:
> 
> {
> 'account-a': 123,
> 'account-b': 456,
> }
> 
> Users may enter the account name, or the ID. The system works with the 
> account ID (translating from the account name if needed), but when it needs 
> to mention the account to the user, it shows the account name instead.
> 
> I do understand that retrieval wouldn't be O(1).
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/BWMIASE5YCDETFSZYAUG7TVWTMBFHKQW/
> Code of Conduct: http://python.org/psf/codeofconduct/

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/DPZMOAICSV2YKBYWFSW36LW5GSPZFRNK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict method to retrieve a key from a value

2023-06-30 Thread Barry Scott



> On 30 Jun 2023, at 02:50, Andre Delfino  wrote:
> 
> A dict method to retrieve the key of a value from a bijective dict would have 
> come in handy to me in several occasions:
> 
 names = {'one': 1}
 names.inverse()[1]
> 'one'
 names = {'one': 1, 'uno': 1}
 names.inverse()[1]
> ValueError: dict is not bijective
> 
> My usual use case is when both keys and values have simple types like 
> int/str, like:
> 
> {
> 'account-a': 123,
> 'account-b': 456,
> }
> 
> Users may enter the account name, or the ID. The system works with the 
> account ID (translating from the account name if needed), but when it needs 
> to mention the account to the user, it shows the account name instead.
> 
> I do understand that retrieval wouldn't be O(1).

In cases like this I have two dict one for each look up direction.

by_name = {}
by_id = {}

def add(name, id):
by_name[name] = id
by_id[id] = name

etc.

Barry


> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/BWMIASE5YCDETFSZYAUG7TVWTMBFHKQW/
> Code of Conduct: http://python.org/psf/codeofconduct/
> 

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/NG6QFBDGHCJXKUFHCONO2U545WLI6N5F/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict method to retrieve a key from a value

2023-06-30 Thread Stephen J. Turnbull
Christopher Barker writes:

 > > If O(log N) is good enough and bijectivity is guaranteed by some
 > > other mechanism, a bisection search on d.items() with
 > > key=lambda x: x[1] does the trick.

 > You'd also have to keep it sorted by value.

I assumed you can do that with OrderedDict.  But yeah, it's a little
more complex.  Maybe some of these ideas are complex and useful enough
to deserve PyPI implementations, and if they prove to have corner
cases and get take up, then consideration for stdlib.

 > So now you are guaranteeing bijectivity and keeping it sorted --
 > I'd just use two dicts :-) Though then the values would have to be
 > hashable, so there's that.

Right (and I missed that throughout, so tyvm).  So much as the
"generic" theory of "low O" "bijection" is attractive, the
implementation details are application-specific.

Steve
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/2YV4C22IMTO5A7E3FJTZZ5QU2L7YPJAB/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict method to retrieve a key from a value

2023-06-30 Thread Christopher Barker
I've had use cases for this, and written a class to do it (using Stephen's
two dict approach. I'd be really surprised if there wasn't one on PyPi
--the trick is to know what to call it to search for it.

Now that I think about it -- my use case was many-to-many, not one to one
-- so not quite the same -- each value was a list associated with a given
key -- and vice versa.

I think it would be a bad idea to overload the built in dict with anything
like this.

*maybe* a new class for the collections module, if you can find a good well
respected implementation, and folks can agree on what's wanted (performance
characteristics, API, etc..) -- that's a pretty tall order though.

If O(log N) is good enough and bijectivity is guaranteed by some other
> mechanism, a bisection search on d.items() with key=lambda x: x[1]
> does the trick.
>

You'd also have to keep it sorted by value. So now you are guaranteeing
bijectivity and keeping it sorted -- I'd just use two dicts :-) Though then
the values would have to be hashable, so there's that.

-CHB



>
> If you know the dict is supposed to be one-to-one and the keys and
> values are disjoint sets, as in your example, just
>
> def bijective_add(d, k, v):
> if k in d and d[k] != v:
> raise BijectiveDictValueChangeError
> d[k] = v
> d[v] = k
>
> gives O(1) both ways.  Maybe you need some kind of check to be sure
> you're retrieving the right type for the calling code.
>
> Otherwise you can pair two dicts in a class and endow it with your
> inverse method.  I would do the check for bijectivity on addition (as
> above) rather than on retrieval, though.
>
> If you really want to save the space, you can add an additional
> hashtable for the values in a C module, but it's not clear to me that
> any particular choice for bijectivity checks would be universally
> desirable in applications so that seems premature.
>
> So I think you need to make clear what your goals are since there are
> at least four solutions with varying performance characteristics.
> I'm agnostic on whether a dict type which guarantees bijectivity would
> be a good addition.  The mathematician in me wants it, but my
> experience says the dict pair is good enough, YAGNI (for values of
> "you" == "me", anyway).
>
> Regards,
> Steve
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/5EEE27MQZLFYF3HJMFVR3KINPIZB3HRI/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/35XXAKC4CMJYUTNAZ6VJEYGVAJX7RJX2/
Code of Conduct: http://python.org/psf/codeofconduct/