Message242689
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>
> _______________________________________
> |
|
Date |
User |
Action |
Args |
2015-05-06 16:50:43 | projetmbc | set | recipients:
+ projetmbc, paul.moore, pitrou |
2015-05-06 16:50:43 | projetmbc | link | issue24132 messages |
2015-05-06 16:50:42 | projetmbc | create | |
|