[Python-ideas] socket module: plain stuples vs named tuples
AFAIK the socket module returns plain tuples in Python3: https://docs.python.org/3/library/socket.html Why not use named tuples? Regards, Thomas Güttler -- I am looking for feedback for my personal programming guidelines: https://github.com/guettli/programming-guidelines ___ 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] socket module: plain stuples vs named tuples
+1 ;-) for example to get IP address of all interfaces we have to use the third (indexed as 2) member of "something" it is much more beatiful to get ip_address: *import *socket *for *ip *in *socket.gethostbyname_ex(socket.gethostname())[2]: print(ip) BR, George 2017-06-13 22:13 GMT+02:00 Thomas Güttler : > AFAIK the socket module returns plain tuples in Python3: > > https://docs.python.org/3/library/socket.html > > Why not use named tuples? > > Regards, > Thomas Güttler > > -- > I am looking for feedback for my personal programming guidelines: > https://github.com/guettli/programming-guidelines > ___ > 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] socket module: plain stuples vs named tuples
Hi, 2017-06-13 22:13 GMT+02:00 Thomas Güttler : > AFAIK the socket module returns plain tuples in Python3: > > https://docs.python.org/3/library/socket.html > > Why not use named tuples? For technical reasons: the socket module is mostly implemented in the C language, and define a "named tuple" in C requires to implement a "sequence" time which requires much more code than creating a tuple. In short, create a tuple is as simple as Py_BuildValue("OO", item1, item2). Creating a "sequence" type requires something like 50 lines of code, maybe more, I don't know exactly. Victor ___ 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] socket module: plain stuples vs named tuples
There are examples in timemodule.c which went through a similar conversion from plain tuples to (sort-of) named tuples. I agree that upgrading the tuples returned by the socket module to named tuples would be nice, but it's a low priority project. Maybe someone interested can create a PR? (First open an issue stating that you're interested; point to this email from me to prevent that some other core dev just closes it again.) On Mon, Jun 19, 2017 at 2:24 PM, Victor Stinner wrote: > Hi, > > 2017-06-13 22:13 GMT+02:00 Thomas Güttler : > > AFAIK the socket module returns plain tuples in Python3: > > > > https://docs.python.org/3/library/socket.html > > > > Why not use named tuples? > > For technical reasons: the socket module is mostly implemented in the > C language, and define a "named tuple" in C requires to implement a > "sequence" time which requires much more code than creating a tuple. > > In short, create a tuple is as simple as Py_BuildValue("OO", item1, item2). > > Creating a "sequence" type requires something like 50 lines of code, > maybe more, I don't know exactly. > > Victor > ___ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ > -- --Guido van Rossum (python.org/~guido) ___ 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] socket module: plain stuples vs named tuples
Oh, about the cost of writing C code, we started to enhance the socket module in socket.py but keep the C code unchanged. I am thinking to the support of enums. Some C functions are wrapped in Python. Victor Le 19 juin 2017 11:59 PM, "Guido van Rossum" a écrit : > There are examples in timemodule.c which went through a similar conversion > from plain tuples to (sort-of) named tuples. I agree that upgrading the > tuples returned by the socket module to named tuples would be nice, but > it's a low priority project. Maybe someone interested can create a PR? > (First open an issue stating that you're interested; point to this email > from me to prevent that some other core dev just closes it again.) > > On Mon, Jun 19, 2017 at 2:24 PM, Victor Stinner > wrote: > >> Hi, >> >> 2017-06-13 22:13 GMT+02:00 Thomas Güttler : >> > AFAIK the socket module returns plain tuples in Python3: >> > >> > https://docs.python.org/3/library/socket.html >> > >> > Why not use named tuples? >> >> For technical reasons: the socket module is mostly implemented in the >> C language, and define a "named tuple" in C requires to implement a >> "sequence" time which requires much more code than creating a tuple. >> >> In short, create a tuple is as simple as Py_BuildValue("OO", item1, >> item2). >> >> Creating a "sequence" type requires something like 50 lines of code, >> maybe more, I don't know exactly. >> >> Victor >> ___ >> Python-ideas mailing list >> Python-ideas@python.org >> https://mail.python.org/mailman/listinfo/python-ideas >> Code of Conduct: http://python.org/psf/codeofconduct/ >> > > > > -- > --Guido van Rossum (python.org/~guido) > ___ 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] socket module: plain stuples vs named tuples
Namedtuple in Python make startup time slow. So I'm very conservative to convert tuple to namedtuple in Python. INADA Naoki On Tue, Jun 20, 2017 at 7:27 AM, Victor Stinner wrote: > Oh, about the cost of writing C code, we started to enhance the socket > module in socket.py but keep the C code unchanged. I am thinking to the > support of enums. Some C functions are wrapped in Python. > > Victor > > Le 19 juin 2017 11:59 PM, "Guido van Rossum" a écrit : >> >> There are examples in timemodule.c which went through a similar conversion >> from plain tuples to (sort-of) named tuples. I agree that upgrading the >> tuples returned by the socket module to named tuples would be nice, but it's >> a low priority project. Maybe someone interested can create a PR? (First >> open an issue stating that you're interested; point to this email from me to >> prevent that some other core dev just closes it again.) >> >> On Mon, Jun 19, 2017 at 2:24 PM, Victor Stinner >> wrote: >>> >>> Hi, >>> >>> 2017-06-13 22:13 GMT+02:00 Thomas Güttler : >>> > AFAIK the socket module returns plain tuples in Python3: >>> > >>> > https://docs.python.org/3/library/socket.html >>> > >>> > Why not use named tuples? >>> >>> For technical reasons: the socket module is mostly implemented in the >>> C language, and define a "named tuple" in C requires to implement a >>> "sequence" time which requires much more code than creating a tuple. >>> >>> In short, create a tuple is as simple as Py_BuildValue("OO", item1, >>> item2). >>> >>> Creating a "sequence" type requires something like 50 lines of code, >>> maybe more, I don't know exactly. >>> >>> Victor >>> ___ >>> Python-ideas mailing list >>> Python-ideas@python.org >>> https://mail.python.org/mailman/listinfo/python-ideas >>> Code of Conduct: http://python.org/psf/codeofconduct/ >> >> >> >> >> -- >> --Guido van Rossum (python.org/~guido) > > > ___ > 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] socket module: plain stuples vs named tuples
On 20 June 2017 at 12:05, INADA Naoki wrote: > Namedtuple in Python make startup time slow. > So I'm very conservative to convert tuple to namedtuple in Python. Aye, I don't think a Python level wrapper would be the right way to go here - while namedtuple is designed to be as cheap as normal tuples in *use*, the same can't be said for the impact on startup time. As context for anyone not familiar with the time module precedent that Guido mentioned, we have a C level `PyStructSequence` that provides some of the most essential namedtuple features, but not all of them: https://github.com/python/cpython/blob/master/Objects/structseq.c So there's potentially a case to be made for: 1. Including the struct sequence header from "Python.h" and making it part of the stable ABI 2. Documenting it in the C API reference The main argument against doing so is that the initialisation API for it is pretty weird by the standards of the rest of the Python C API - it's mainly designed for use as a C level type *factory*, rather than intended to be used directly. That's also why there isn't a Python level API for it - it's designed around accepting C level structs as inputs, which doesn't translate well to pure Python code (whereas the collections.namedtuple API works well in pure Python code, but doesn't translate well to C code). Cheers, Nick. -- Nick Coghlan | ncogh...@gmail.com | Brisbane, Australia ___ 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] socket module: plain stuples vs named tuples
2017-06-20 4:05 GMT+02:00 INADA Naoki : > Namedtuple in Python make startup time slow. > So I'm very conservative to convert tuple to namedtuple in Python. > INADA Naoki While we are talking about startup time, I would be curious of seeing the overhead (python startup time, when importing socket) of the enums added to socket.py ;-) "import enum" added to Lib/re.py was a regression causing a slowdown in the "python_startup" benchmark, but it was related to the site module importing "re" in Python is running in a virtual environment. This very specific use case was fixed by not using the re module in the site module. Victor ___ 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] socket module: plain stuples vs named tuples
On Mon, Jun 19, 2017 at 8:04 PM, Nick Coghlan wrote: > As context for anyone not familiar with the time module precedent that > Guido mentioned, we have a C level `PyStructSequence` that provides > some of the most essential namedtuple features, but not all of them: > https://github.com/python/cpython/blob/master/Objects/structseq.c > > So there's potentially a case to be made for: > > 1. Including the struct sequence header from "Python.h" and making it > part of the stable ABI > 2. Documenting it in the C API reference > +1 -- I was just thinking this morning that a C-level named tuple would be nice. And certainly better than re-implementing it in various places it is needed. Would there be any benefit in making a C implementation available from Python? -CHB -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R(206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception chris.bar...@noaa.gov ___ 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] socket module: plain stuples vs named tuples
> > Would there be any benefit in making a C implementation available from > Python? > > -CHB > Yes, at startup time point of view. Current Python namedtuple implementation uses `eval`. It means we can't cache bytecode in pyc files. For example, importing functools is not so fast and its because `_CacheInfo` namedtuple for `lru_cache`. And structseq is faster than Python namedtuple too. ref: http://bugs.python.org/issue28638#msg282412 Regards, ___ 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] socket module: plain stuples vs named tuples - Thank you
thank you! I am happy that Guido is open for a pull request ... There were +1 votes, too (and some concern about python startup time). I stopped coding in spare time, since my children are more important at the moment .. if some wants to try it ... go ahead and implement named tuples for the socket standard library - would be great. Just for the records, I came here because of this feature request: https://github.com/giampaolo/psutil/issues/928 Regards, Thomas Güttler PS: For some strange reasons I received only some mails of this thread. But I could find the whole thread in the archive. Am 20.06.2017 um 04:05 schrieb INADA Naoki: Namedtuple in Python make startup time slow. So I'm very conservative to convert tuple to namedtuple in Python. INADA Naoki On Tue, Jun 20, 2017 at 7:27 AM, Victor Stinner wrote: Oh, about the cost of writing C code, we started to enhance the socket module in socket.py but keep the C code unchanged. I am thinking to the support of enums. Some C functions are wrapped in Python. Victor Le 19 juin 2017 11:59 PM, "Guido van Rossum" a écrit : There are examples in timemodule.c which went through a similar conversion from plain tuples to (sort-of) named tuples. I agree that upgrading the tuples returned by the socket module to named tuples would be nice, but it's a low priority project. Maybe someone interested can create a PR? (First open an issue stating that you're interested; point to this email from me to prevent that some other core dev just closes it again.) On Mon, Jun 19, 2017 at 2:24 PM, Victor Stinner wrote: Hi, 2017-06-13 22:13 GMT+02:00 Thomas Güttler : AFAIK the socket module returns plain tuples in Python3: https://docs.python.org/3/library/socket.html Why not use named tuples? For technical reasons: the socket module is mostly implemented in the C language, and define a "named tuple" in C requires to implement a "sequence" time which requires much more code than creating a tuple. In short, create a tuple is as simple as Py_BuildValue("OO", item1, item2). Creating a "sequence" type requires something like 50 lines of code, maybe more, I don't know exactly. Victor ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/ -- --Guido van Rossum (python.org/~guido) ___ 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/ -- Thomas Guettler http://www.thomas-guettler.de/ ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/