diff options
author | D.B. Tsai <dbtsai@dbtsai.com> | 2012-08-22 16:45:53 -0700 |
---|---|---|
committer | D.B. Tsai <dbtsai@dbtsai.com> | 2012-08-27 20:02:35 -0700 |
commit | 694ff00b2e7362ce2b128d7d02c7ccc2852300c2 (patch) | |
tree | c5ddfa0e9e413d279305b5c88d024c393e27d40d /mimeparse.py | |
parent | 82a8d0ea03833ad5fe779868437fa4c9811a7c78 (diff) | |
download | python-mimeparse-694ff00b2e7362ce2b128d7d02c7ccc2852300c2.tar.gz |
Fixed ``has_key()`` which is deprecated in python3.
Also cleaned up several PEP8 warnings.
Diffstat (limited to 'mimeparse.py')
-rwxr-xr-x | mimeparse.py | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/mimeparse.py b/mimeparse.py index b8e6783..3d63fc8 100755 --- a/mimeparse.py +++ b/mimeparse.py @@ -34,11 +34,11 @@ def parse_mime_type(mime_type): into: ('application', 'xhtml', {'q', '0.5'}) - """ + """ parts = mime_type.split(';') - params = dict([tuple([s.strip() for s in param.split('=', 1)])\ - for param in parts[1:] - ]) + params = dict([tuple([s.strip() for s in param.split('=', 1)]) + for param in parts[1:] + ]) full_type = parts[0].strip() # Java URLConnection class sends an Accept header that includes a # single '*'. Turn it into a legal wildcard. @@ -64,7 +64,7 @@ def parse_media_range(range): necessary. """ (type, subtype, params) = parse_mime_type(range) - if not params.has_key('q') or not params['q'] or \ + if not 'q' in params or not params['q'] or \ not float(params['q']) or float(params['q']) > 1\ or float(params['q']) < 0: params['q'] = '1' @@ -84,18 +84,18 @@ def fitness_and_quality_parsed(mime_type, parsed_ranges): best_fitness = -1 best_fit_q = 0 (target_type, target_subtype, target_params) =\ - parse_media_range(mime_type) + parse_media_range(mime_type) for (type, subtype, params) in parsed_ranges: - type_match = (type == target_type or\ - type == '*' or\ + type_match = (type == target_type or + type == '*' or target_type == '*') - subtype_match = (subtype == target_subtype or\ - subtype == '*' or\ + subtype_match = (subtype == target_subtype or + subtype == '*' or target_subtype == '*') if type_match and subtype_match: - param_matches = reduce(lambda x, y: x + y, [1 for (key, value) in \ - target_params.iteritems() if key != 'q' and \ - params.has_key(key) and value == params[key]], 0) + param_matches = reduce(lambda x, y: x + y, [1 for (key, value) in + target_params.iteritems() if key != 'q' and + key in params and value == params[key]], 0) fitness = (type == target_type) and 100 or 0 fitness += (subtype == target_subtype) and 10 or 0 fitness += param_matches |