Skip to content

Lack of library-wide ordered set hurts readability of algorithms #3912

@xpe

Description

@xpe

Updated as of 2020-04-12 1:41pm PDT

Problem

There is at least one place in NetworkX that uses collections.OrderedDict as a substitute for an insertion-ordered set. This is an undesirable workaround: it makes code harder than necessary to read.

Consider this example:

visited[child] = None

What do you think it does? I wouldn't fault anyone for (on a first pass) thinking that it marks child as not visited. Well, in reality, adds an item to an collections.OrderedDict (used as an insertion-ordered set).

In my view, the above code doesn't just "read badly" it is also misleading. In context of a particular algorithm, simple_paths.py, it makes it harder to follow.

Solution

There are options. I understand that Python's collections library does not provide an ordered set container. I recommend either:

  • using an existing ordered set library, or
  • add a library-wide wrapper on top of OrderedDict.

For example, please consider IndexedSet from the boltons package:

IndexedSet is a collections.MutableSet that maintains insertion order and uniqueness of inserted elements. It’s a hybrid type, mostly like an OrderedSet, but also list-like, in that it supports indexing and slicing.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions