Re: keying by identity in dict and set
Hi, Yes, there are several options, but they all involve an extra layer that detracts between the interface I am building and my user's code. In this situation, the objects being used as keys are conceptually the unique entities that the user deals with, even if their data is non-unique. And I do not want to subject the user to the un-pythonic use of some operator other than '==' to determine their equivalence. As near as I can tell, returning the id() in __hash__() results in a perfect hash key. I really want to know if that is true. Because if it is true, any further layer is simply covering for a failing in the documentation. Thanks! On Sun, Oct 27, 2019 at 4:54 AM Random832 wrote: > > On Sat, Oct 19, 2019, at 07:31, Steve White wrote: > > Hi, > > > > I have an application that would benefit from object instances > > distinguished by identity being used in dict's and set's. To do this, > > the __hash__ method must be overridden, the obvious return value being > > the instance's id. > > > > This works flawlessly in extensive tests on several platforms, and on > > a couple of different Python versions and implementations. > > > > The documentation seems to preclude a second requirement, however. > > > > I also want to use the == operator on these objects to mean a natural > > comparison of values, different from identity, so that two instances > > comparing equivalent does not imply that they are identical. > > I'd like to jump in to this thread to note that while this is reasonably > easily achieved with a custom mapping class that uses a dict along with a > wrapper class that stores the identity... > > I once tried to make a WeakKeyDictionary that was keyed by identity and had > no end of trouble. > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: keying by identity in dict and set
On Sun, Oct 27, 2019 at 6:26 PM Steve White wrote: > As near as I can tell, returning the id() in __hash__() results in a > perfect hash key. I really want to know if that is true. > Because if it is true, any further layer is simply covering for a > failing in the documentation. Only if your __eq__() does not return True for anything other than itself. That is the entire definition of __hash__ - it needs to return the same value for two objects that are equal. This is exactly what the documentation said, as multiple people have posted here. If your object is equal only to itself, then its hash can be its identity. Otherwise it should not. It is that simple. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: fileinput
Le samedi 26 octobre 2019 17:49:57 UTC+2, Peter Otten a écrit :
> Pascal wrote:
>
> > I have a small python (3.7.4) script that should open a log file and
> > display its content but as you can see, an encoding error occurs :
> >
> > ---
> >
> > import fileinput
> > import sys
> > try:
> > source = sys.argv[1:]
> > except IndexError:
> > source = None
> > for line in fileinput.input(source):
> > print(line.strip())
> >
> > ---
> >
> > python3.7.4 myscript.py myfile.log
> > Traceback (most recent call last):
> > ...
> > UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 799:
> > invalid continuation byte
> >
> > python3.7.4 myscript.py < myfile.log
> > Traceback (most recent call last):
> > ...
> > UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 799:
> > invalid continuation byte
> >
> > ---
> >
> > I add the encoding hook to overcome the error but this time, the script
> > reacts differently depending on the input used :
> >
> > ---
> >
> > import fileinput
> > import sys
> > try:
> > source = sys.argv[1:]
> > except IndexError:
> > source = None
> > for line in fileinput.input(source,
> > openhook=fileinput.hook_encoded("utf-8", "ignore")):
> > print(line.strip())
> >
> > ---
> >
> > python3.7.4 myscript.py myfile.log
> > first line of myfile.log
> > ...
> > last line of myfile.log
> >
> > python3.7.4 myscript.py < myfile.log
> > Traceback (most recent call last):
> > ...
> > UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 799:
> > invalid continuation byte
> >
> > python3.7.4 myscript.py /dev/stdin < myfile.log
> > first line of myfile.log
> > ...
> > last line of myfile.log
> >
> > python3.7.4 myscript.py - < myfile.log
> > Traceback (most recent call last):
> > ...
> > UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 799:
> > invalid continuation byte
> >
> > ---
> >
> > does anyone have an explanation and/or solution ?
>
> '-' or no argument tell fileinput to use sys.stdin. This is already text
> decoded using Python's default io-encoding, and the open hook is not called.
> You can override the default encoding by setting the environment variable
>
> PYTHONIOENCODING=UTF8:ignore
yes, I just found this about it : https://bugs.python.org/issue26756
this modified script is ok in all cases :
import io
import fileinput
import sys
try:
source = sys.argv[1:]
except IndexError:
source = None
sys.stdin = io.TextIOWrapper(sys.stdin.buffer, errors='ignore')
for line in fileinput.input(source, openhook=fileinput.hook_encoded('utf-8',
'ignore')):
print(line.strip())
thanks for the tip !
--
https://mail.python.org/mailman/listinfo/python-list
