Feb-01-2019, 09:47 AM
(This post was last modified: Feb-01-2019, 10:01 AM by DreamingInsanity.)
(Jan-31-2019, 10:13 AM)perfringo Wrote:(Jan-31-2019, 09:42 AM)DreamingInsanity Wrote: I got the idea at the time of posting but waited to see if there were other ways.
There are many other ways how to do it (including your endswith).
However, suggesting the 'other ways' one must know what is the end goal (what do you want accomplish). For printing out this solution is good enough. But your objective is probably not to just print out something. If there is match you probably want to act upon this information. Therefore to present 'other ways' one must know what you want to do after match is found.
The only part that wasnt included in the code was that once it found what ending the url had, it would call a function and it would download the file from that url. I needed the ending so I could save it with that specific ending.
urllib.request.urlretrieve(url, img_dir + str(name) + '.gif') / urllib.request.urlretrieve(url, img_dir + str(name) + '.gifv')Thanks,
Dream
(Jan-31-2019, 10:59 AM)buran Wrote: if you want to check string end, better usesome_string.endswith(search_string)
. in checks for membership, so it will returnTrue
even if the search_string
is in the middle ofsome_string
. .endswith() is also recommened by PEP8 compared to using slicing.
Alternative approach is using something like
def spam(): print('Doing something within spam()') def eggs(): print('Doing something within eggs()') options = (('gifv', spam), ('gif', eggs)) some_string = 'foo.gifv' for end, func in options: if some_string.endswith(end): func() breakThis way it will allow to expand the list of options without ending with monstrous if/elif/elif/.../else block.
Options can be dict in python 3.7 or collections.OrderedDict (in version before that) in order to preserve the order in which will be checked
Thanks, I will use this in the future!
Dream