python_utils: replace listlike() with iterable() and sequence()
ClosedPublic

Authored by kparal on May 29 2014, 12:16 PM.

Details

Summary

This fixes a pet peeve of mine - the listlike() function name. It was a bit misleading. Hopefully it should be clearer after this patch.

These two new functions are often handy. As a bonus, they can also check
type validity of items they contain.

The new names match better their purpose. By separating them, it's easy
to pick the right one in different situations. It requires a person to
be familiar with abstract base classes, though (iterable, sequence,
mapping, container, etc).

This patch also improves isinstance() checking on various places and
uses ABCs instead of hardcoded object names (like list, dict, etc).

Test Plan

New tests, all pass.

Diff Detail

Repository
rLTRN libtaskotron
Lint
Lint Skipped
Unit
Unit Tests Skipped
kparal retitled this revision from to python_utils: replace listlike() with iterable() and sequence().May 29 2014, 12:16 PM
kparal updated this object.
kparal edited the test plan for this revision. (Show Details)
kparal added reviewers: tflink, mkrizek, jskladan.
tflink accepted this revision.May 29 2014, 1:45 PM

The code still feels a bit un-pythonic (the concept of type checking, mostly) but I know why it's there and I'm not objecting to it as long as we don't start using it everywhere :)

Overall, it's an improvement. Looks good to me.

This revision is now accepted and ready to land.May 29 2014, 1:45 PM
jskladan added inline comments.May 29 2014, 2:54 PM
libtaskotron/check.py
92

Are you sure you really want to be testing for sequence here? From what the rest of the code looks like, it seems that list is specifically required, not just any sequence - see line #159

libtaskotron/python_utils.py
30

Judging from the diff - do you really think sequence is needed? There is nothing specifically sequence-specific - i.e. nothing in the diff (but I might have missed something, of course) does not need the direct access via integer indices.

Also, should we keep both iterable and sequence, it might be usefull to re-phrase the docstring a bit, so it captures the most basic diffenrece (IMHO) between iterable and sequence - that dict is an iterable, but not a sequence. Other than that, these are quite inter-changeable (given that our code does not specifically need integer indexing).

Also /me has a bit of a bad-english-day, so any perceived snarkiness is unintended byproduct :)

kparal updated this revision to Diff 327.May 30 2014, 11:48 AM
  • address issues in review

Thanks, Josef, very good points. Another attempt posted.

libtaskotron/check.py
92

Good catch, output must be a MutableSequence, not just any Sequence.

And I thought I'd keep the python_utils methods really simple. I had to add an additional method argument.

libtaskotron/python_utils.py
30

For example CheckDetail.output must be a sequence, because it must be ordered - it doesn't make sense to have a set of strings as a check output. The same applies for python directive output.

That's also a reply what I consider the biggest difference between an iterable and a sequence - being ordered and indexable. I don't think dict constitutes a big difference, because you rarely want to consider dict as something else than a Mapping. It's a way too different structure from all those lists, sets and tuples.

kparal requested a review of this revision.May 30 2014, 2:49 PM
tflink accepted this revision.May 30 2014, 3:12 PM

Looks good to me.

This revision is now accepted and ready to land.May 30 2014, 3:12 PM
jskladan accepted this revision.Jun 2 2014, 8:21 AM

OOK!

kparal closed this revision.Jun 2 2014, 9:47 AM
kparal updated this revision to Diff 339.

Closed by commit rLTRN6fe71ab8c461 (authored by @kparal).