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
pull/79/head
Mike Fährmann 7 years ago
parent d122203be1
commit 731ffd4986
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -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

Loading…
Cancel
Save