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] Revisiting Immutable Mappings

2018-10-11 Thread Zaur Shibzukhov

May be the following simple prototype of frozendict could be useful?

def frozen_error():
   return RuntimeError("frozendict is not mutable")

class frozendict(dict):
#
def __setitem__(self, key, val):
raise frozen_error()
#
def setdefault(self, key, val=None):
raise frozen_error()
#
def update(self, ob):
raise frozen_error()
#
def pop(self):
raise frozen_error()
#
def popitem(self, ob):
raise frozen_error()
#
def clear(self, ob):
raise frozen_error()
#
def __delitem__(self, key):
raise frozen_error()
#
def __repr__(self):
return "frozendict(" + dict.__repr__(self)[1:-1] + ")"
#
def __str__(self):
return "frozendict(" + dict.__str__(self)[1:-1] + ")"
#
def fromkeys(self, keys, val=None):
return frozendict([(key,val) for key in keys])
#
def copy(self):
return frozendict(self.items())

среда, 10 октября 2018 г., 20:06:03 UTC+3 пользователь Philip Martin 
написал:
>
> Hi, I first want to thank everyone in the community for the contributions 
> over the years. I know the idea of a frozendict has been proposed before 
> and rejected. I have a use case for a frozendict implementation that to my 
> knowledge was not discussed during previous debates. My reasoning for a 
> frozendict class stems from patterns I typically see arise when performing 
> ETL or data integrations and conversions. I generally have used 
> MappingProxyType as a way to set default mapping to a function or to set an 
> empty mapping to a function. I've created a gist with an example use case:
>
> https://gist.github.com/pmart123/493edf84d9aa61691ca7321325ebb6ab
>
> I've included an example of what code typically looks like when using 
> MappingProxyType and what it could look like with a 
> frozendict implementation. I believe this use case may also be 
> under-reported in open source code as it often crops up when integrating 
> third-party data sources, which at times can't be open sourced due to 
> licensing issues. I would love to hear if anyone has used MappingProxyType 
> in a similar manner, or if this use case could help warrant a frozendict in 
> the standard library.
>
___
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] Retire or reword the "Beautiful is better than ugly" Zen clause

2018-09-14 Thread Zaur Shibzukhov


пятница, 14 сентября 2018 г., 1:56:58 UTC+3 пользователь Guido van Rossum 
написал:
>
> Everyone who still wants to reply to this thread: please decide for 
> yourself whether the OP, "Samantha Quan" who started it could be a Russian 
> troll. Facts to consider: (a) the OP's address is ...@yandex.com, a 
> well-known Russian website (similar to Google); (b) there's a Canadian 
> actress named Samantha Quan.
>

I completely agree with the fact that this discussion should be stopped 
without starting it. I just want to note that anyone (with this it does not 
have to be Russian at all) can create an account on Yandex. I would not 
want this trolling to be considered in the spirit of forging a negative 
attitude towards the Russians. 
___
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] Add recordlcass to collections module

2018-09-04 Thread Zaur Shibzukhov
---
*Zaur Shibzukhov*


2018-09-03 1:02 GMT+03:00 Wes Turner :

>
> On Sunday, September 2, 2018, Zaur Shibzukhov  wrote:
>
>>
>>
>> ---
>> *Zaur Shibzukhov*
>>
>>
>> 2018-09-02 22:11 GMT+03:00 Wes Turner :
>>
>>> Does the value of __hash__ change when attributes of a recordclass
>>> change?
>>>
>>
>> Currently recordclass's __hash__ didn't implemented.
>>
>
> https://docs.python.org/3/glossary.html#term-hashable
>
> https://docs.python.org/3/reference/datamodel.html#object.__hash__
>
> http://www.attrs.org/en/stable/hashing.html
>

There is correction:
recordclass and it's base memoryslots didn't implement __hash__, but
memoryslots implement richcompare (almost as python's list).

>
>
>>
>>> On Sunday, September 2, 2018, Zaur Shibzukhov  wrote:
>>>
>>>> As the author of `recordclass` I would like to shed some light...
>>>>
>>>> Recorclass originated as a response to the [question](
>>>> https://stackoverflow.com/questions/29290359/exis
>>>> tence-of-mutable-named-tuple-in-python/29419745#29419745) on
>>>> stackoverflow.
>>>>
>>>> `Recordclass` was conceived and implemented as a type that, by api,
>>>> memory and speed, would be completely identical to` namedtuple`, except
>>>> that it would support an assignment in which any element could be replaced
>>>> without creating a new instance, as in ` namedtuple`. Those. would be
>>>> almost identical to `namedtuple` and support the assignment (` __setitem__`
>>>> / `setslice__`).
>>>>
>>>> The effectiveness of namedtuple is based on the effectiveness of the
>>>> `tuple` type in python. In order to achieve the same efficiency it was
>>>> necessary to create a type `memoryslots`. Its structure
>>>> (`PyMemorySlotsObject`) is identical to the structure of` tuple`
>>>> (`PyTupleObject`) and therefore takes up the same amount of memory as`
>>>> tuple`.
>>>>
>>>> `Recordclass` is defined on top of` memoryslots` just like `namedtuple`
>>>> above` tuple`. Attributes are accessed via a descriptor
>>>> (`itemgetset`), which supports both` __get__` and `__set__` by the element
>>>> index.
>>>>
>>>> The class generated by `recordclass` is:
>>>>
>>>> `` `
>>>> from recordclass import memoryslots, itemgetset
>>>>
>>>> class C (memoryslots):
>>>> __slots__ = ()
>>>>
>>>> _fields = ('attr_1', ..., 'attr_m')
>>>>
>>>> attr_1 = itemgetset (0)
>>>> ...
>>>> attr_m = itemgetset (m-1)
>>>>
>>>> def __new __ (cls, attr_1, ..., attr_m):
>>>> 'Create new instance of {typename} ({arg_list})'
>>>> return memoryslots .__ new __ (cls, attr_1, ..., attr_m)
>>>> `` `
>>>> etc. following the `namedtuple` definition scheme.
>>>>
>>>> As a result, `recordclass` takes up as much memory as` namedtuple`, it
>>>> supports quick access by `__getitem__` /` __setitem__` and by attribute
>>>> name via the protocol of the descriptors.
>>>>
>>>> Regards,
>>>>
>>>> Zaur
>>>>
>>>> суббота, 1 сентября 2018 г., 10:48:07 UTC+3 пользователь Martin Bammer
>>>> написал:
>>>>>
>>>>> Hi,
>>>>>
>>>>> what about adding recordclass
>>>>> (https://bitbucket.org/intellimath/recordclass) to the collections
>>>>> module
>>>>>
>>>>> It is like namedtuple, but elements are writable and it is written in
>>>>> C
>>>>> and thus much faster.
>>>>>
>>>>> And for convenience it could be named as namedlist.
>>>>>
>>>>> Regards,
>>>>>
>>>>> Martin
>>>>>
>>>>>
>>>>> ___
>>>>> Python-ideas mailing list
>>>>> python...@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] Add recordlcass to collections module

2018-09-03 Thread Zaur Shibzukhov


понедельник, 3 сентября 2018 г., 2:11:06 UTC+3 пользователь Greg Ewing 
написал:
>
> Zaur Shibzukhov wrote: 
>
>  > `Recordclass` is defined on top of` memoryslots` just like `namedtuple` 
>  > above` tuple`. Attributes are accessed via a descriptor (`itemgetset`), 
>  > which supports both` __get__` and `__set__` by the element index. 
>  > 
>  > As a result, `recordclass` takes up as much memory as` namedtuple`, it 
>  > supports quick access by `__getitem__` /` __setitem__` and by attribute 
>  > name via the protocol of the descriptors. 
>
> I'm not sure why you need a new C-level type for this. Couldn't you 
> get the same effect just by using __slots__? 
>
> e.g. 
>
> class C: 
>
>   __slots__ = ('attr_1', ..., 'attr_m') 
>
>   def __new __ (cls, attr_1, ..., attr_m): 
>   self.attr_1 = attr_1 
>   ... 
>   self.attt_m = attr_m 
>
> Yes, you can. The only difference is that access by index to fields are 
slow. So if you don't need fast access by index but only by name then using 
__slots__ is enough. Recordclass is actually a fixed array with named 
access to the elements in the same manner as namedtuple is a actually a 
tuple with named access to it's elements. 
___
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] Add recordlcass to collections module

2018-09-02 Thread Zaur Shibzukhov
As the author of `recordclass` I would like to shed some light...

Recorclass originated as a response to the 
[question](https://stackoverflow.com/questions/29290359/existence-of-mutable-named-tuple-in-python/29419745#29419745)
 
on stackoverflow.

`Recordclass` was conceived and implemented as a type that, by api, memory 
and speed, would be completely identical to` namedtuple`, except that it 
would support an assignment in which any element could be replaced without 
creating a new instance, as in ` namedtuple`. Those. would be almost 
identical to `namedtuple` and support the assignment (` __setitem__` / 
`setslice__`).

The effectiveness of namedtuple is based on the effectiveness of the 
`tuple` type in python. In order to achieve the same efficiency it was 
necessary to create a type `memoryslots`. Its structure 
(`PyMemorySlotsObject`) is identical to the structure of` tuple` 
(`PyTupleObject`) and therefore takes up the same amount of memory as` 
tuple`.

`Recordclass` is defined on top of` memoryslots` just like `namedtuple` 
above` tuple`. Attributes are accessed via a descriptor (`itemgetset`), 
which supports both` __get__` and `__set__` by the element index.

The class generated by `recordclass` is:

`` `
from recordclass import memoryslots, itemgetset

class C (memoryslots):
__slots__ = ()

_fields = ('attr_1', ..., 'attr_m')

attr_1 = itemgetset (0)
...
attr_m = itemgetset (m-1)

def __new __ (cls, attr_1, ..., attr_m):
'Create new instance of {typename} ({arg_list})'
return memoryslots .__ new __ (cls, attr_1, ..., attr_m)
`` `
etc. following the `namedtuple` definition scheme.

As a result, `recordclass` takes up as much memory as` namedtuple`, it 
supports quick access by `__getitem__` /` __setitem__` and by attribute 
name via the protocol of the descriptors.

Regards,

Zaur

суббота, 1 сентября 2018 г., 10:48:07 UTC+3 пользователь Martin Bammer 
написал:
>
> Hi, 
>
> what about adding recordclass 
> (https://bitbucket.org/intellimath/recordclass) to the collections module 
>
> It is like namedtuple, but elements are writable and it is written in C 
> and thus much faster. 
>
> And for convenience it could be named as namedlist. 
>
> Regards, 
>
> Martin 
>
>
> ___ 
> Python-ideas mailing list 
> python...@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/