Re: [Python-ideas] Why shouldn't Python be better at implementing Domain Specific Languages?
wrote a quick article here : https://www.pythonmembers.club/2018/09/03/how-to-create-your-own-dsldomain-specific-language-in-python/ -- Abdur-Rahmaan Janhangeer https://github.com/abdur-rahmaanj Mauritius ___ 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
On Sun, Sep 02, 2018 at 10:56:50PM +0200, Martin Bammer wrote: > Compared to dataclass: > dataclass wins only on the topic object size. When it comes to speed and > functionality (indexing, sorting) dataclass would be my last choice. I see no sign that recordclass supports sorting. (But I admit that I haven't tried it.) What would it mean to sort a recordclass? Person = recordclass('Person', 'personalname familyname address') fred = Person("Fred", "Anderson", "123 Main Street") fred.sort() print(fred) => output: Person(personalname='123 Main Street', familyname='Anderson', address='Fred') [...] > Adding new items: > This is not possible with namedtuple and also not possible with > recordclass. I see no reason why a namedlist should support this, If you want to change the name and call it a "list", then it needs to support the same things that lists support. > because with these object types you define new object types and these > types should not change. Sorry, I don't understand that. How do you get "no insertions" from "can't change the type"? A list remains a list when you insert into it. In case it isn't clear, I think there is zero justification for renaming recordclass to namedlist. I don't think "named list" makes sense as a concept, and recordclass surely doesn't implement a list-like interface. As for the idea of adding a recordclass or mutable-namedtuple or whatever to the stdlib, the idea seems reasonable but its not clear to me that dataclass wouldn't be suitable. -- 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] Add recordlcass to collections module
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 -- 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/
Re: [Python-ideas] Add recordlcass to collections module
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 > >> 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
Hi, then intention of my first mail was to start a discussion about this topic about the pros and cons and possible alternatives. As long as it is not clear that recordclass or something like that is accepted to be implemented to the collections module I do not want to spend any effort on this. My wish that the collections module gets something like namedtuple, but writable, is based on my personal experience when projects are becoming bigger and data structures more complex it is sometimes useful to named items and not just an index. This improves the readability and makes development and maintenance of the code easier. Another important topic for me is performance. When I write applications then they should finish their tasks quickly. The performance of recordclass was one reason for me to use it (some benchmarks with Python 2 can be found on here https://gist.github.com/grantjenks/a06da0db18826be1176c31c95a6ee572). I've done some more recent and additional benchmarks with Python 3.7 on Linux which you can find here https://github.com/brmmm3/compare-recordclass. These new benchmarks show that namedtuple is as fast as recordclass in all cases, but with named attribute access. Named attribute access is faster with recordclass. Compared to dataclass: dataclass wins only on the topic object size. When it comes to speed and functionality (indexing, sorting) dataclass would be my last choice. Yes it is possible to make dataclass fast by using __slots__, but this always an extra programming effort. namedtuple and recordclass are easy to use with small effort. Adding new items: This is not possible with namedtuple and also not possible with recordclass. I see no reason why a namedlist should support this, because with these object types you define new object types and these types should not change. I hope 3.8 will get a namedlist and maybe it will be the recordclass module (currently my choice). As the author of this module already has responded to this discussion I hope he willing to contribute his code to the Python project. Best regards, Martin ___ 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
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/