improve text.filename_from_url() performance

- urlsplit() is faster than urlparse()
- rpartition() is faster than rindex() + slicing
- new version is 2.3 times as fast
This commit is contained in:
Mike Fährmann
2018-02-18 16:50:07 +01:00
parent d122203be1
commit 731ffd4986

View File

@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2017 Mike Fährmann
# Copyright 2015-2018 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
@@ -36,9 +36,7 @@ def remove_html(text):
def filename_from_url(url):
"""Extract the last part of an url to use as a filename"""
try:
path = urllib.parse.urlparse(url).path
pos = path.rindex("/")
return path[pos+1:]
return urllib.parse.urlsplit(url).path.rpartition("/")[2]
except ValueError:
return url