1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 04:02:32 +01:00

replace calls to 'os.path.splitext()' with 'str.rpartition()'

Makes functions who used it more than twice as fast
and we can get rid of an import as well.
This commit is contained in:
Mike Fährmann 2021-03-28 03:54:12 +02:00
parent 0a9af56e3c
commit 8553b218d9
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
2 changed files with 15 additions and 11 deletions

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2019 Mike Fährmann
# Copyright 2015-2021 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
@ -10,7 +10,6 @@
import re
import html
import os.path
import datetime
import urllib.parse
@ -77,18 +76,22 @@ def filename_from_url(url):
def ext_from_url(url):
"""Extract the filename extension of an URL"""
filename = filename_from_url(url)
ext = os.path.splitext(filename)[1]
return ext[1:].lower()
name, _, ext = filename_from_url(url).rpartition(".")
return ext.lower() if name else ""
def nameext_from_url(url, data=None):
"""Extract the last part of an URL and fill 'data' accordingly"""
if data is None:
data = {}
name = unquote(filename_from_url(url))
data["filename"], ext = os.path.splitext(name)
data["extension"] = ext[1:].lower()
filename = unquote(filename_from_url(url))
name, _, ext = filename.rpartition(".")
if name:
data["filename"], data["extension"] = name, ext.lower()
else:
data["filename"], data["extension"] = filename, ""
return data

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2015-2020 Mike Fährmann
# Copyright 2015-2021 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
@ -142,8 +142,9 @@ class TestText(unittest.TestCase):
# standard usage
self.assertEqual(f(""), "")
self.assertEqual(f("filename"), "")
self.assertEqual(f("filename.ext"), result)
self.assertEqual(f("/filename.ext"), result)
self.assertEqual(f("/filename.ExT"), result)
self.assertEqual(f("example.org/filename.ext"), result)
self.assertEqual(f("http://example.org/v2/filename.ext"), result)
self.assertEqual(
@ -160,7 +161,7 @@ class TestText(unittest.TestCase):
# standard usage
self.assertEqual(f(""), empty)
self.assertEqual(f("filename.ext"), result)
self.assertEqual(f("/filename.ext"), result)
self.assertEqual(f("/filename.ExT"), result)
self.assertEqual(f("example.org/filename.ext"), result)
self.assertEqual(f("http://example.org/v2/filename.ext"), result)
self.assertEqual(