Ticket #3589 (new enhancement )

Opened 6 months ago

Last modified 5 days ago

Convenience method for verifying if one path is ancestor of another

Reported by: jml Assigned to: jml
Type: enhancement Priority: normal
Milestone: Component: core
Keywords: Cc:
Branch: Author:
Launchpad Bug:

Description

I recently found myself wanting to check whether one path is the ancestor of another. It's a pain in the neck using os.path, so I checked out FilePath too see if it offered any help.

FilePath.segmentsFrom is very close to what I want. In fact, something like the following would be perfect:

def isParent(self, ancestor):
    try:
        self.segmentsFrom(ancestor)
    except ValueError:
        return False
    else:
        return True

Incidentally, why ValueError?

Attachments

Change History

  2008-12-22 04:06:07+00:00 changed by glyph

Probably a misguided attempt to fit into the idiom of the standard library, and use an existing exception. The rationalization would go something like: it is raised when ancestor is an invalid value. Sort of like the 'x not in list' and 'substring not found' ValueErrors from str.index and list.index.

Maybe there should be a more specific exception but it's unlikely it's worth it.

  2008-12-22 04:12:25+00:00 changed by jml

OK, that makes sense. Mostly I like having more specific exceptions because they make it harder to accidentally suppress real errors, but here it's not such a big deal.

  2008-12-22 04:14:55+00:00 changed by glyph

Perhaps segmentsFrom should take a "default" argument, which in your case could be "False"? (I could perhaps see wanting a default iterable as well).

  2008-12-22 04:15:34+00:00 changed by glyph

Actually, I'll propose another implementation strategy on #3588 too...

  2008-12-22 04:17:22+00:00 changed by jml

... with the default default implying raise ValueError? Yeah, that doesn't seem too bad. I'd rather something named isAncestor, since that will make the intent of the calling code clearer, but a default would be a reasonably convenient API.

  2009-06-30 08:25:54+00:00 changed by glyph

  • owner changed from glyph to jml
Note: See TracTickets for help on using tickets.