February 2018
Beginner to intermediate
364 pages
10h 32m
English
The following is the implementation of the .extension_from_contenttype property:
@propertydef extension_from_contenttype(self): self.ensure_response() map = const.ContentTypeToExtensions() if self.contenttype in map: return map[self.contenttype] return None
The first line ensures that we have read the response from the URL. The function then uses a python dictionary, defined in the const module, which contains a dictionary of content-types to extension:
def ContentTypeToExtensions(): return { "image/jpeg": ".jpg", "image/jpg": ".jpg", "image/png": ".png" }
If the content type is in the dictionary, then the corresponding value will be returned. Otherwise, None is returned.
Note the corresponding property, .extension_from_url
Read now
Unlock full access