From: "Steven D'Aprano" <st...@pearwood.info>
To: python...@python.org
Cc:
Bcc:
Date: Sat, 1 Sep 2018 18:25:21 +1000
Subject: Re: [Python-ideas] Add recordlcass to collections module
On Sat, Sep 01, 2018 at 09:47:04AM +0200, Martin Bammer wrote:
> Hi,
>
> what about adding recordclass
> (https://bitbucket.org/intellimath/recordclass) to the collections module
The first thing you need to do is ask the author of that library whether
or not he or she is willing to donate the library to the Python stdlib,
which (among other things) means keeping to the same release schedule as
the rest of the stdlib.
> It is like namedtuple, but elements are writable and it is written in C
> and thus much faster.
Faster than what?
> And for convenience it could be named as namedlist.
Why? Is it a list?
How or why is it better than dataclasses?
--
Steve
_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
As to the other questions, yes, do we need another module in the standard library?
_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
What's the difference between you proposition and dataclasses ? Introduced in Python 3.7 ?
---------- Forwarded message ----------
From: Martin Bammer <mrb...@gmail.com>
To: python...@python.org
Cc:
Bcc:
Date: Sat, 1 Sep 2018 09:47:04 +0200
Subject: [Python-ideas] Add recordlcass to collections module
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
---Zaur Shibzukhov2018-09-02 22:11 GMT+03:00 Wes Turner <wes.t...@gmail.com>:Does the value of __hash__ change when attributes of a recordclass change?Currently recordclass's __hash__ didn't implemented.
Also, it's rather clear that namedList is a really bad name for a Recordclass. It's cleary not intended to be a list. It's a record you can take out from somewhere, mutate, and push back in.
This feels really useful to me to make some quick changes to a database - perhaps a database layer could return an class of type Recordclass, and then you just simply mutate it and shove it back into the database. Pseudocode:record = database.execute("SELECT * FROM mytable WHERE primary_key = 15")record.mostRecentLoggedInTime = time.time()database.execute(f"UPDATE mytable SET mostRecentLoggedInTime = {record.mostRecentLoggedInTime} WHERE primary_key = {record.primary_key}":)Or any smart database wrapper might just go:database.updateOrInsert(table = mytable, record = record)And be smart enough to figure out that we already have a primary key unequal to some sentinel value like None, and do an update, while it could do an insert if the primary key WAS some kind of sentinel value.
On Mon, Sep 3, 2018 at 5:23 PM, Jacco van Dorp <j.van...@deonet.nl> wrote:
> This feels really useful to me to make some quick changes to a database -
> perhaps a database layer could return an class of type Recordclass, and then
> you just simply mutate it and shove it back into the database. Pseudocode:
>
> record = database.execute("SELECT * FROM mytable WHERE primary_key = 15")
> record.mostRecentLoggedInTime = time.time()
> database.execute(f"UPDATE mytable SET mostRecentLoggedInTime =
> {record.mostRecentLoggedInTime} WHERE primary_key = {record.primary_key}":)
>
> Or any smart database wrapper might just go:
>
> database.updateOrInsert(table = mytable, record = record)
>
> And be smart enough to figure out that we already have a primary key unequal
> to some sentinel value like None, and do an update, while it could do an
> insert if the primary key WAS some kind of sentinel value.
In its purest form, what you're asking for is an "upsert" or "merge" operation:
https://en.wikipedia.org/wiki/Merge_(SQL)
In a multi-user transactional database, there are some fundamentally
hard problems to implementing a merge. I'm not 100% certain, so I
won't say "impossible", but it is certainly *extremely difficult* to
implement an operation like this in application-level software without
some form of race condition.
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
On Sunday, September 2, 2018, Zaur Shibzukhov <szp...@gmail.com> wrote:---Zaur Shibzukhov2018-09-02 22:11 GMT+03:00 Wes Turner <wes.t...@gmail.com>:Does the value of __hash__ change when attributes of a recordclass change?Currently recordclass's __hash__ didn't implemented.
I've seen three things mentioned that might be different from dataclasses:
- instance size
- speed (not sure of what: instance creation? field access?)
- iterating over fields
But I've not seen concrete examples of the first two where dataclasses
doesn't perform well enough. For the third one, there's already a thread
on this mailing list: "Consider adding an iterable option to dataclass".
I'm contemplating adding it.
> Personally, I don’t see it.
I'm skeptical, too.
Eric
>
> -CHB
>
> On Tue, Sep 4, 2018 at 2:04 PM Zaur Shibzukhov <szp...@gmail.com
> <mailto:szp...@gmail.com>> wrote:
>
>
>
> ---
> /Zaur Shibzukhov/
>
>
> 2018-09-03 1:02 GMT+03:00 Wes Turner <wes.t...@gmail.com
> <mailto:wes.t...@gmail.com>>:
>
>
> On Sunday, September 2, 2018, Zaur Shibzukhov <szp...@gmail.com
> <mailto:szp...@gmail.com>> wrote:
>
>
>
> ---
> /Zaur Shibzukhov/
>
>
> 2018-09-02 22:11 GMT+03:00 Wes Turner <wes.t...@gmail.com
> <mailto:wes.t...@gmail.com>>:
>
> 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
> Python...@python.org <mailto:Python...@python.org>
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
> --
>
> 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....@noaa.gov <mailto:Chris....@noaa.gov>