Ticket #221 (new enhancement)

Opened 1 year ago

Last modified 1 year ago

[PATCH] add a rowgetter companion to itemgetter

Reported by: arnarbi@gmail.com Assigned to: somebody
Priority: normal Milestone: MochiKit 1.5
Component: component1 Version:
Severity: normal Keywords:
Cc:

Description

Attached is a patch that modifies Base.itemgetter such that it accepts an array-like as a parameter. The semantics in that case is that the resulting function returns an array of the corresponding attributes of the object.

>>> var o = {a: 1, b: 2, c: 4, d: -1};
>>> itemgetter(['a', 'c', 'd'])(o)
[1, 4, -1]

The typical use case is when you have an array of homogeneous objects, one can easily turn that into a table of selected properties with

map(itemgetter(['firstname', 'lastname', 'phone']), listOfPersonObjects);

The patch also adds some tests for itemgetter, which was not covered before.

Attachments

itemgetter_array.patch (1.2 kB) - added by arnarbi@gmail.com on 01/27/07 06:09:40.
Patch to make itemgetter accept array-like param and tests
rowgetter.patch (2.0 kB) - added by arnarbi@gmail.com on 02/03/07 06:51:49.
Adds rowgetter and leaves itemgetter alone. Adds tests for both.
rowgetter.2.patch (2.0 kB) - added by arnarbi@gmail.com on 02/03/07 06:51:52.
Adds rowgetter and leaves itemgetter alone. Adds tests for both.

Change History

01/27/07 06:09:40 changed by arnarbi@gmail.com

  • attachment itemgetter_array.patch added.

Patch to make itemgetter accept array-like param and tests

(follow-ups: ↓ 2 ↓ 4 ) 01/31/07 14:38:49 changed by bob@redivi.com

Not a bad idea, but I'd much rather call it something else (e.g. "rowgetter") instead of making itemgetter do two different things based upon the arguments.

(in reply to: ↑ 1 ) 02/03/07 06:36:04 changed by arnarbi@gmail.com

Replying to bob@redivi.com:

Not a bad idea, but I'd much rather call it something else (e.g. "rowgetter") instead of making itemgetter do two different things based upon the arguments.

Sounds reasonable, I'll post a new patch.

02/03/07 06:51:49 changed by arnarbi@gmail.com

  • attachment rowgetter.patch added.

Adds rowgetter and leaves itemgetter alone. Adds tests for both.

02/03/07 06:51:52 changed by arnarbi@gmail.com

  • attachment rowgetter.2.patch added.

Adds rowgetter and leaves itemgetter alone. Adds tests for both.

02/03/07 06:53:07 changed by arnarb@gmail.com

Sorry for the double submit - I had some http gateway erros. The two files (rowgetter.patch) are the same.

(in reply to: ↑ 1 ) 02/07/07 07:37:25 changed by anonymous

Replying to bob@redivi.com:

Not a bad idea, but I'd much rather call it something else (e.g. "rowgetter") instead of making itemgetter do two different things based upon the arguments.

Will this go to version 1.4?

05/14/07 18:41:51 changed by rsayre@mozilla.com

  • summary changed from [PATCH] Modify itemgetter to accept an array-like parameter to [PATCH] add a rowgetter companion to itemgetter.
  • milestone set to MochiKit 1.5.

no activity in 2 months, so not well-tested for release