Hey #python people! Question for you, does anyone know any more details of how the str.split() method is implemented? I'm curious about performance of str.split('.').pop() vs str[str.rfind('.'):] (as in, find the (last part of) extension of a filename) Or maybe there's an even better solution?
Conversation
Notices
-
Rebecca :blobcatcomfygamer: (rebeccasaurus@tech.lgbt)'s status on Wednesday, 30-Nov-2022 17:15:40 CET Rebecca :blobcatcomfygamer:
-
Pybonacci (pybonacci@mastodon.social)'s status on Wednesday, 30-Nov-2022 17:15:30 CET Pybonacci
@stolas_mk2 @rebeccasaurus
There's also this
https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.suffix -
stolas (stolas_mk2@tech.lgbt)'s status on Wednesday, 30-Nov-2022 17:15:35 CET stolas
@rebeccasaurus if it works it works :)
-
Rebecca :blobcatcomfygamer: (rebeccasaurus@tech.lgbt)'s status on Wednesday, 30-Nov-2022 17:15:39 CET Rebecca :blobcatcomfygamer:
@stolas_mk2 ooh, well that's certainly very very convenient at least! I might have to set up some tests to measure the performance..
It's for a script I've written to use at work, parsing potentially millions of lines of output from "find ..." (shell, not python) and summarizing some data. I doubt the performance difference would be particularly large, I just prefer to write efficient code xD
-
Rebecca :blobcatcomfygamer: (rebeccasaurus@tech.lgbt)'s status on Wednesday, 30-Nov-2022 17:15:39 CET Rebecca :blobcatcomfygamer:
@stolas_mk2 the current version uses regex, very unnecessarily, to pull out info from every single input line.. (it was my very first python script, don't judge me! :blobcatmeltcry: )
-
stolas (stolas_mk2@tech.lgbt)'s status on Wednesday, 30-Nov-2022 17:15:40 CET stolas
@rebeccasaurus I know that if I want to fiddle about with paths and filenames in Python, I use the os.path module. I suspect that something like this might help? https://docs.python.org/3/library/os.path.html#os.path.splitext
I couldn't tell you about the internals of how it works or the details how the str.split menthod is implemented. The bulk of what I do with Python the long bit is the database anyway so I don't tend to keep close track of it.
-