Python Forum
List of pathlib.Paths Not Ordered As Same List of Same String Filenames - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: List of pathlib.Paths Not Ordered As Same List of Same String Filenames (/thread-6224.html)

Pages: 1 2 3


RE: List of pathlib.Paths Not Ordered As Same List of Same String Filenames - Larz60+ - Nov-12-2017

Quote:phrase
In my book means 'in my opinion' or 'according to my beliefs'.
The greatest manager there has ever been, or ever will be in my book, is retiring.
Synonyms: in my opinion, to me, as far as I am concerned   More Synonyms of in my book

Sorry, I have written magazine articles, but not a book yet (have one with 350 pages, but got burnt out)


RE: List of pathlib.Paths Not Ordered As Same List of Same String Filenames - heiner55 - Nov-12-2017

Sorry, English is not my native language.


RE: List of pathlib.Paths Not Ordered As Same List of Same String Filenames - Larz60+ - Nov-12-2017

Not a problem. When I went to work in England and went out to a workplace pub for a beer with the guys,
after we had a couple, one stated 'I'm pissed'.
In USA, that means angry.
In England, intoxicated.
I didn't understand how he could be 'pissed' and laughing at the same time.


RE: List of pathlib.Paths Not Ordered As Same List of Same String Filenames - QbLearningPython - Nov-14-2017

Thanks, @heiner55 and @larz6+. However, I still think that comparing pathlib.Path is flawed if it cannot provide a alphabetical order equal to string order of corresponding filenames. In most cases, this is not a problem for sure.


RE: List of pathlib.Paths Not Ordered As Same List of Same String Filenames - Larz60+ - Nov-14-2017

Looking at this more closely, I think there is credibility to your argument,
enough so that the author of pathlib Antoine Pitrou should be asked.
His github account is: https://github.com/pitrou


RE: List of pathlib.Paths Not Ordered As Same List of Same String Filenames - QbLearningPython - Nov-15-2017

Ok, I do that. Thank you.


RE: List of pathlib.Paths Not Ordered As Same List of Same String Filenames - buran - Nov-15-2017

(Nov-11-2017, 05:54 PM)QbLearningPython Wrote: I supposed that the order retrieved by sorted() a list of Paths would be the same as the order retrieved by sorted() a list of their (string) filenames.

The problems comes from the fact that you assume certain behaviour, i.e. the sort is based on the full path as string. However if you look at the code of pathlib, you will see it is based on tuple of path parts.
https://bitbucket.org/pitrou/pathlib/src/1f9672002e3a5e54c48dd48912cc6d45f8a2137d/pathlib.py?at=default&fileviewer=file-view-default
look at lines 827-869
this example mimics the behaviour
a = ('spam', 'spams.txt')
b = ('spam', 'spams', 'spams.ttt')
c = ('spam', 'spams', 'spams01.txt')
d = ('spam', 'spams', 'spams02.txt')
my_list = [a,b,c,d]
print sorted(my_list)
Output:
[('spam', 'spams', 'spams.ttt'), ('spam', 'spams', 'spams01.txt'), ('spam', 'spams', 'spams02.txt'), ('spam', 'spams.txt')]



RE: List of pathlib.Paths Not Ordered As Same List of Same String Filenames - QbLearningPython - Nov-15-2017

You are right, @buran. That is the current behaviour. Your example explains that very well.

I am talking about a would-be behaviour: that a list of pathlib.Paths and a list of string filename would behaved in the same manner. Of course it is just an opinion.

Perhaps the reason behind my opinion is that I regularly use pathlib.Path and string filenames as different implementations of the same notion: a path to a file or folder. Maybe I am disorientated on this point and should change my view.

In any case, I have submitted this issue as a possible bug for developers' consideration. I do not really think that it is a bug but... Just in case...

Thank you.


RE: List of pathlib.Paths Not Ordered As Same List of Same String Filenames - buran - Nov-15-2017

it is clearly not a bug, but intended behavior. you can easily supply key argument  to sorted to get what you want

 
from pathlib import Path

filenames_for_testing = (
    '/spam/spams.txt',
    '/spam/spam.txt',
    '/spam/another.txt',
    '/spam/binary.bin',
    '/spam/spams/spam.ttt',
    '/spam/spams/spam01.txt',
    '/spam/spams/spam02.txt',
    '/spam/spams/spam03.ppp',
    '/spam/spams/spam04.doc',
)

paths_for_testing = [
    Path(filename)
    for filename in filenames_for_testing
]
sorted_paths = sorted(paths_for_testing, key=lambda p: p.as_posix())
print(sorted_paths)
Output:
[PosixPath('/spam/another.txt'), PosixPath('/spam/binary.bin'), PosixPath('/spam/spam.txt'), PosixPath('/spam/spams.txt'), PosixPath('/spam/spams/spam.ttt'), PosixPath('/spam/spams/spam01.txt'), PosixPath('/spam/spams/spam02.txt'), PosixPath('/spam/spams/spam03.ppp'), PosixPath('/spam/spams/spam04.doc')]



RE: List of pathlib.Paths Not Ordered As Same List of Same String Filenames - DeaD_EyE - Nov-15-2017

Please look into the source code. Github makes it very easy for us.

The bult-in function sorted calls __lt__ (means less then) of an object.
So we should look into the pathlib module and seek for __lt__

It's hard to follow the code, because there is heavy inheritance and some other patterns.
You should know that pathobject._cparts returns a list spitted by it's parts. A path separator is also a part, if it's the root directory (absolute path).

Comparing lists in Python does not do what you think it should do. It compares in lexicographical order. I got this answer from here.

Comparing the paths as str:
'/spam/spams.txt' < '/spam/spams/spam04.doc'
Output:
True
Comparing the _cparts:
['/', 'spam', 'spams.txt'] < ['/', 'spam', 'spams', 'spam04.doc']
Output:
False
Index 0 and 1 are identical. Index 3 is bigger than index 3 from the other list. Then there are no more elements in the first list for comparison and the operation stops and yields the last result, which is False.
'spams.txt' < 'spam04.doc'
In simple words: Paths are sorted in alphabetical order by their elements.