summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorD.B. Tsai <dbtsai@dbtsai.com>2012-08-22 16:45:53 -0700
committerD.B. Tsai <dbtsai@dbtsai.com>2012-08-27 20:02:35 -0700
commit694ff00b2e7362ce2b128d7d02c7ccc2852300c2 (patch)
treec5ddfa0e9e413d279305b5c88d024c393e27d40d
parent82a8d0ea03833ad5fe779868437fa4c9811a7c78 (diff)
downloadpython-mimeparse-694ff00b2e7362ce2b128d7d02c7ccc2852300c2.tar.gz
Fixed ``has_key()`` which is deprecated in python3.
Also cleaned up several PEP8 warnings.
-rwxr-xr-xmimeparse.py26
-rwxr-xr-xmimeparse_test.py18
-rwxr-xr-xsetup.py10
3 files changed, 29 insertions, 25 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
diff --git a/mimeparse_test.py b/mimeparse_test.py
index 4b4f95e..ce639e4 100755
--- a/mimeparse_test.py
+++ b/mimeparse_test.py
@@ -10,14 +10,11 @@ __author__ = 'Ade Oshineye'
__email__ = "ade@oshineye.com"
__credits__ = ""
+import json
import mimeparse
import unittest
from functools import partial
-# Conditional import to support Python 2.5
-try:
- import json
-except ImportError:
- import simplejson as json
+
def test_parse_media_range(args, expected):
expected = tuple(expected)
@@ -25,33 +22,39 @@ def test_parse_media_range(args, expected):
message = "Expected: '%s' but got %s" % (expected, result)
assert expected == result, message
+
def test_quality(args, expected):
result = mimeparse.quality(args[0], args[1])
message = "Expected: '%s' but got %s" % (expected, result)
assert expected == result, message
+
def test_best_match(args, expected):
result = mimeparse.best_match(args[0], args[1])
message = "Expected: '%s' but got %s" % (expected, result)
assert expected == result, message
+
def test_parse_mime_type(args, expected):
expected = tuple(expected)
result = mimeparse.parse_mime_type(args)
message = "Expected: '%s' but got %s" % (expected, result)
assert expected == result, message
+
def add_tests(suite, json_object, func_name, test_func):
test_data = json_object[func_name]
for test_datum in test_data:
args, expected = test_datum[0], test_datum[1]
- desc = "%s(%s) with expected result: %s" % (func_name, str(args), str(expected))
+ desc = "%s(%s) with expected result: %s" % (func_name, str(args),
+ str(expected))
if len(test_datum) == 3:
desc = test_datum[2] + " : " + desc
- func = partial(test_func, *(args, expected))
+ func = partial(test_func, *(args, expected))
testcase = unittest.FunctionTestCase(func, description=desc)
suite.addTest(testcase)
+
def run_tests():
json_object = json.load(open("testdata.json"))
@@ -64,5 +67,6 @@ def run_tests():
test_runner = unittest.TextTestRunner(verbosity=1)
test_runner.run(suite)
+
if __name__ == "__main__":
run_tests()
diff --git a/setup.py b/setup.py
index a3c03a9..e9a1d82 100755
--- a/setup.py
+++ b/setup.py
@@ -1,6 +1,7 @@
-# -*- coding: utf-8 -*-
+
+# -*- coding: utf-8 -*-
-#old way
+#old way
from distutils.core import setup
#new way
@@ -11,7 +12,7 @@ setup(name='mimeparse',
description='A module provides basic functions for parsing mime-type names and matching them against a list of media-ranges.',
long_description="""
This module provides basic functions for handling mime-types. It can handle
-matching mime-types against a list of media-ranges. See section 14.1 of
+matching mime-types against a list of media-ranges. See section 14.1 of
the HTTP specification [RFC 2616] for a complete explanation.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
@@ -30,7 +31,7 @@ Contents:
'Programming Language :: Python',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Software Development :: Libraries :: Python Modules',
- ],
+ ],
keywords='mime-type',
author='Joe Gregorio',
author_email='joe@bitworking.org',
@@ -41,4 +42,3 @@ Contents:
py_modules=['mimeparse'],
zip_safe=True,
)
-