The os.path.commonprefix function basically returns the common initial characters (if any) shared by a sequence of strings, e.g.     os.path.commonprefix(("Python is great!", "Python is not bad", "Python helps")) # returns "Python " This is purely a string manipulation function which bears no relation to actual file paths or whether they exist.
I have found uses for it unrelated to file paths:
    (a) When updating a progress display by printing backspaces and overtyping, discover the common characters between the old and new values displayed, so as to print as few characters as possible
    (b) Find the first character position where two strings differ.
I am sure there are many others.

It seems to me that this function (or something similar) should not be in os.path, but somewhere else where it would be more visible. (There are a lot of "solutions" on the Internet to finding the common prefix of 2 or more than 2 strings, whose authors obviously don't know about os.path.commonprefix.)
I'm not sure where, though.  Possibilities include
    (1) a str.commonprefix function.  Unfortunately it would have to be used like this
            str.commonprefix(<sequence of strings>)
        which would make it [I think] the only str function which couldn't be called as a string method.
    (2) in the sequence protocol.
          This would mean that it could apply not just to strings, but to any sequences.  E.g.
              [ (1,2,3), (1,2,4) ].commonprefix() # Would return (1,2).
              [ (1,2,3), [1,2,4] ].commonprefix() # Mixed sequence types - undefined?           One wrinkle: os.patch.commonprefix, if passed an empty sequence, returns an empty STRING.               If this function were designed from scratch, it should probably return None.     (3) a builtin commonprefix function.  Again it could apply to any sequences. I also suspect that this function could be made more efficient.  It sorts the sequence.  While sorting is very fast (thanks, Uncle Tim!) it seems a bit OTT in this case.

Thoughts?
Best wishes
Rob Cliffe
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/LFGA6DD3TDHWEXMGHTI4RP2U7NNFRMY6/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to