Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-16 Thread Philip Martin
I think it could inherit from the Mapping abc?

class frozendict(Mapping):
def __new__(cls, *args, **kwargs):
d = dict(*args, **kwargs)
proxy = MappingProxyType(d)
instance = super().__new__(cls)
instance.__proxy = proxy

return instance

def __hash__(self):
return hash(tuple(self.items()))

def __getattr__(self, name):
return getattr(self.__proxy, name)

def __getitem__(self, key):
return self.__proxy[key]

def __iter__(self):
return self.__proxy.__iter__()

def __len__(self):
return len(self.__proxy)

def __repr__(self):
return "%s(%r)" % (type(self).__name__, dict(self))


On Tue, Oct 16, 2018 at 4:29 AM Steven D'Aprano  wrote:

> On Tue, Oct 16, 2018 at 01:02:03AM -0700, George Leslie-Waksman wrote:
> > Would a frozendict require that keys and values be hashable?
>
> Keys, yes. Values, no.
>
> If the values were hashable, the frozendict itself would also be
> hashable. If not, then it would be like trying to hash a tuple with
> unhashable items:
>
> py> hash((1, 2, {}, 3))
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: unhashable type: 'dict'
>
>
> > It seems to me that we would need this restriction to make a reasonably
> > universal frozendict that is, itself, hashable.
>
> When people talk about frozendicts being hashable, they mean it in the
> same sense that tuples are hashable.
>
>
> For what it is worth, here's an incomplete, quick and dirty proof of
> concept frozendict, using automatic delegation:
>
> # Not good enough for production, not tested, buyer beware, etc.
> class frozendict:
> def __new__(cls, *args, **kwargs):
> d = dict(*args, **kwargs)
> proxy = types.MappingProxyType(d)
> instance = super().__new__(cls)
> instance.__proxy = proxy
> return instance
> def __hash__(self):
> return hash(tuple(self.items()))
> def __getattr__(self, name):
> return getattr(self.__proxy, name)
> def __getitem__(self, key):
> return self.__proxy[key]
> def __repr__(self):
> return "%s(%r)" % (type(self).__name__, dict(self))
>
>
> --
> Steve
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Powerset

2018-10-16 Thread Nick Timkovich
"more-itertools" seems to be a modestly popular library (top 100-500?) that
includes the stdlib itertools recipes and more.
https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.powerset

On Tue, Oct 16, 2018 at 3:42 AM Hasan Diwan  wrote:

> Pal,
> On Tue, 16 Oct 2018 at 01:36, Pål Grønås Drange 
> wrote:
> > I do however agree that there could be a powerset function there for
> convenience, but only +0.
>
> That is the best argument I could come up with to justify a
> set#powerset method. -- H
> --
> OpenPGP:
> https://sks-keyservers.net/pks/lookup?op=get&search=0xFEBAD7FFD041BBA1
> If you wish to request my time, please do so using
> bit.ly/hd1AppointmentRequest.
> Si vous voudrais faire connnaisance, allez a bit.ly/hd1AppointmentRequest.
>
> Sent from my mobile device
> Envoye de mon portable
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-16 Thread Zaur Shibzukhov


вторник, 16 октября 2018 г., 12:29:55 UTC+3 пользователь Steven D'Aprano 
написал:
>
>
> > It seems to me that we would need this restriction to make a reasonably 
> > universal frozendict that is, itself, hashable. 
>
> When people talk about frozendicts being hashable, they mean it in the 
> same sense that tuples are hashable. 
>
>
> For what it is worth, here's an incomplete, quick and dirty proof of 
> concept frozendict, using automatic delegation: 
>
> # Not good enough for production, not tested, buyer beware, etc. 
> class frozendict: 
> def __new__(cls, *args, **kwargs): 
> d = dict(*args, **kwargs) 
> proxy = types.MappingProxyType(d) 
> instance = super().__new__(cls) 
> instance.__proxy = proxy 
> return instance 
> def __hash__(self): 
> return hash(tuple(self.items())) 
> def __getattr__(self, name): 
> return getattr(self.__proxy, name) 
> def __getitem__(self, key): 
> return self.__proxy[key] 
> def __repr__(self): 
> return "%s(%r)" % (type(self).__name__, dict(self)) 
>
> For those who need more performant variant (Cython compiled) of frozendict 
frozenmap  project can be offer.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Powerset

2018-10-16 Thread Jonathan Fine
Wes Turner wrote:

> Obviously, this is combinatorics and set theory (category theory (HOTT)); 
> here in the itertools library for iterables.

Powerset is a small problem, and HoTT is a big solution. I know this
because I've got the standard book sitting, largely unread, on my
bookshelf for over a year.

https://en.wikipedia.org/wiki/Homotopy_type_theory
https://homotopytypetheory.org/book/

HoTT is based on the Coq Proof Assistant: https://coq.inria.fr/. One
of the applications of CoQ is certification of properties of
programming languages.

And now we're a long way from the original post and topic.

-- 
Jonathan
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Powerset

2018-10-16 Thread Wes Turner
On Tuesday, October 16, 2018, Greg Ewing 
wrote:

> Wes Turner wrote:
>
>> Is there a name for an iteration of the powerset which is more useful for
>> binary search? I.e. instead of starting with null set, start with the
>> "middle" ( r/2 ).
>>
>
> You'll have to provide more detail about what you want to search
> and how you intend to search it. There isn't a single "middle" to
> the set of powersets, since in general there are many subsets with
> about half the elements of the original set. Also there is no
> obvious ordering to use for bisection.


When searching for combinations of factors which most correlate to the
dependent variable, it doesn't always make sense to start with single
factors; especially when other factors 'cancel out'.

For example, in clinical medicine, differential diagnosis is a matter of
determining what the most likely diagnosis/es is/are; given lots of noise
and one or more differentiating factors.

Testing individual factors first may not be the most efficient because
combinations/permutations are more likely to be highly correlated with
specific diagnoses.

Random search of the powerset and mutation (or a neuralnet) may be faster
anyways. Just wondering whether there's a name for differently ordered
powerset (and Cartesian product) traversals?

Obviously, this is combinatorics and set theory (category theory (HOTT));
here in the itertools library for iterables.


> --
> Greg
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-16 Thread Steven D'Aprano
On Tue, Oct 16, 2018 at 01:02:03AM -0700, George Leslie-Waksman wrote:
> Would a frozendict require that keys and values be hashable?

Keys, yes. Values, no.

If the values were hashable, the frozendict itself would also be 
hashable. If not, then it would be like trying to hash a tuple with 
unhashable items:

py> hash((1, 2, {}, 3))
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type: 'dict'


> It seems to me that we would need this restriction to make a reasonably
> universal frozendict that is, itself, hashable.

When people talk about frozendicts being hashable, they mean it in the 
same sense that tuples are hashable. 


For what it is worth, here's an incomplete, quick and dirty proof of 
concept frozendict, using automatic delegation:

# Not good enough for production, not tested, buyer beware, etc.
class frozendict:
def __new__(cls, *args, **kwargs):
d = dict(*args, **kwargs)
proxy = types.MappingProxyType(d)
instance = super().__new__(cls)
instance.__proxy = proxy
return instance
def __hash__(self):
return hash(tuple(self.items()))
def __getattr__(self, name):
return getattr(self.__proxy, name)
def __getitem__(self, key):
return self.__proxy[key]
def __repr__(self):
return "%s(%r)" % (type(self).__name__, dict(self))


-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-16 Thread Chris Angelico
On Tue, Oct 16, 2018 at 7:02 PM George Leslie-Waksman  wrote:
>
> Would a frozendict require that keys and values be hashable?

Keys would already have to be hashable - regular dicts demand this,
and there's no reason not to for frozendict.

Values? Not so sure. Personally I would say that no, they don't HAVE
to be hashable - but that the frozendict itself would then not be
hashable.

> It seems to me that we would need this restriction to make a reasonably 
> universal frozendict that is, itself, hashable.
>

The tuple provides a good precedent here:

>>> hash((1,2,3))
2528502973977326415
>>> hash((1,2,[]))
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type: 'list'
>>>

ChrisA
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Powerset

2018-10-16 Thread Nicolas Rolin
Can we get an utilisation context ?
I don't think it belongs in the stdlib alone on the basis that its output
is not linear in the size of its input (but exponential), so it explode
even for a mid-sized list, which by nature limits greatly its usage.
The question would be wether or not it is used enough to push it to
itertools, which is pretty hard to decide with no examples.

2018-10-16 10:41 GMT+02:00 Hasan Diwan :

> Pal,
> On Tue, 16 Oct 2018 at 01:36, Pål Grønås Drange 
> wrote:
> > I do however agree that there could be a powerset function there for
> convenience, but only +0.
>
> That is the best argument I could come up with to justify a
> set#powerset method. -- H
> --
> OpenPGP: https://sks-keyservers.net/pks/lookup?op=get&search=
> 0xFEBAD7FFD041BBA1
> If you wish to request my time, please do so using
> bit.ly/hd1AppointmentRequest.
> Si vous voudrais faire connnaisance, allez a bit.ly/hd1AppointmentRequest.
>
> Sent from my mobile device
> Envoye de mon portable
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 

--
*Nicolas Rolin* | Data Scientist
+ 33 631992617 - nicolas.ro...@tiime.fr 


*15 rue Auber, **75009 Paris*
*www.tiime.fr *
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Powerset

2018-10-16 Thread Hasan Diwan
Pal,
On Tue, 16 Oct 2018 at 01:36, Pål Grønås Drange  wrote:
> I do however agree that there could be a powerset function there for 
> convenience, but only +0.

That is the best argument I could come up with to justify a
set#powerset method. -- H
-- 
OpenPGP: https://sks-keyservers.net/pks/lookup?op=get&search=0xFEBAD7FFD041BBA1
If you wish to request my time, please do so using bit.ly/hd1AppointmentRequest.
Si vous voudrais faire connnaisance, allez a bit.ly/hd1AppointmentRequest.

Sent from my mobile device
Envoye de mon portable
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Powerset

2018-10-16 Thread Pål Grønås Drange
> [...] when one searches for a powerset function, the
> logical place to look isn't itertools, it's the set class. -- H

That's a rather object-oriented view, I think.

So you look for the permutation function in the list class?

I prefer these functions gathered in one place, and I find that itertools
does the job.

I do however agree that there could be a powerset function there for
convenience, but only +0.

- Pål GD
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-16 Thread George Leslie-Waksman
Would a frozendict require that keys and values be hashable?

It seems to me that we would need this restriction to make a reasonably
universal frozendict that is, itself, hashable. With this restriction for
the general case, is there still sufficient value for everyone that is
asking for a frozendict?

Without this restriction and without frozendict being hashable, is there
still sufficient value for everyone that is asking for a frozendict?

On Fri, Oct 12, 2018 at 7:31 AM Greg Ewing 
wrote:

> Chris Barker - NOAA Federal via Python-ideas wrote:
>
> > Or maybe come up with a new name
>
> We should call it a birdseyedict, because of this:
>
>
> http://www.vulture.com/2016/12/unearthing-a-rare-1971-monty-python-film-all-about-peas.html
>
> --
> Greg
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/