Author projetmbc
Recipients paul.moore, pitrou, projetmbc
Date 2015-05-06.16:50:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <CAAb4jGk5s6WjdXJXCC+TWZ3xeV1LE_9m6OsBJf2L3nz2rzhV6A@mail.gmail.com>
In-reply-to <CAAb4jGkk7mvyxqPMjgZsmmoLLsnDraS+1TZapPoh=9rWKVus+w@mail.gmail.com>
Content
Here are for example two extra methods that I have implemented.

def __sub__(cls, path):
    """
This magic method allows to use ``onepath - anotherpath`` instead of the
long
version ``onepath.relative_to(anotherpath)`` given by ``pathlib.Path``.
    """
    return cls.relative_to(path)

def _ppath_common_with(cls, paths):
    """
This method returns the path of the smaller common "folder" of the current
path
and at least one paths.

python::
    from mistool import os_use

    path   = os_use.PPath("/Users/projects/source/doc")
    path_1 = os_use.PPath("/Users/projects/README")
    path_2 = os_use.PPath("/Users/projects/source/misTool/os_use.py")

    print(path.common_with((path_1, path_2)))
    """
    if not isinstance(paths, (list, tuple)):
        paths = [paths]

    commonparts = list(cls.parts)

    for onepath in paths:
        i = 0

        for common, actual in zip(commonparts, onepath.parts):
            if common == actual:
                i += 1
            else:
                break

        commonparts = commonparts[:i]

        if not commonparts:
            break

    commonpath = pathlib.Path("")

    for part in commonparts:
        commonpath /= part

    return commonpath

*Christophe BAL*
*Enseignant de mathématiques en Lycée **et développeur Python amateur*
*---*
*French math teacher in a "Lycée" **and **Python **amateur developer*

2015-05-06 14:13 GMT+02:00 Christophe BAL <report@bugs.python.org>:

>
> Christophe BAL added the comment:
>
> Hello.
>
> I will give a real example in 5 hours after my job. I will try tomorrow a
> solution to ease the subclassing using another dedicazted class PathPlus,
> sorry for the name. The idea would be to use this new class for
> customization, and also to define WindowsPath and PosixPath sub-classing
> this new class. By default PathPlus would be an empty class. I do not know
> if this works well. Maybe my idea is a bad one.
>
> *Christophe BAL*
> *Enseignant de mathématiques en Lycée **et développeur Python amateur*
> *---*
> *French math teacher in a "Lycée" **and **Python **amateur developer*
>
> 2015-05-06 13:05 GMT+02:00 Antoine Pitrou <report@bugs.python.org>:
>
> >
> > Antoine Pitrou added the comment:
> >
> > The Path classes were not designed to be subclassable by the user.
> > I'm not against making subclassing easier, but someone will have to
> > propose a viable approach for that.
> >
> > ----------
> > versions: +Python 3.5 -Python 3.4
> >
> > _______________________________________
> > Python tracker <report@bugs.python.org>
> > <http://bugs.python.org/issue24132>
> > _______________________________________
> >
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue24132>
> _______________________________________
>
History
Date User Action Args
2015-05-06 16:50:43projetmbcsetrecipients: + projetmbc, paul.moore, pitrou
2015-05-06 16:50:43projetmbclinkissue24132 messages
2015-05-06 16:50:42projetmbccreate