From 1e6ac7d58491b8b7af5e14da13d7d83f39b7fd85 Mon Sep 17 00:00:00 2001 From: Patrick Lucas Date: Mon, 20 Oct 2014 13:40:02 -0700 Subject: [PATCH] Depend on simplejson for Python < 2.7 This adds a dependency on simplejson for Python < 2.7 and moves the stanza to import the appropriate json module into pyleus.compat. Closes #46 --- examples/top_urls/requirements.txt | 1 - examples/top_urls/top_urls/requests_generator.py | 7 +------ pyleus/compat.py | 11 ++++++++++- pyleus/json_fields_bolt.py | 9 ++------- pyleus/storm/component.py | 7 +------ pyleus/storm/serializers/json_serializer.py | 7 +------ setup.py | 4 ++++ tests/storm/serializers/json_serializer_test.py | 7 +------ 8 files changed, 20 insertions(+), 33 deletions(-) delete mode 100644 examples/top_urls/requirements.txt diff --git a/examples/top_urls/requirements.txt b/examples/top_urls/requirements.txt deleted file mode 100644 index 322630e..0000000 --- a/examples/top_urls/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -simplejson diff --git a/examples/top_urls/top_urls/requests_generator.py b/examples/top_urls/top_urls/requests_generator.py index c4decda..63403e5 100644 --- a/examples/top_urls/top_urls/requests_generator.py +++ b/examples/top_urls/top_urls/requests_generator.py @@ -5,12 +5,7 @@ from random import choice import time -try: - import simplejson as json - _ = json # pyflakes -except ImportError: - import json - +from pyleus.compat import json from pyleus.storm import Spout diff --git a/pyleus/compat.py b/pyleus/compat.py index 617e8aa..52d7194 100644 --- a/pyleus/compat.py +++ b/pyleus/compat.py @@ -1,6 +1,8 @@ import sys -if sys.version_info[0] < 3: +python_version = (sys.version_info[0], sys.version_info[1]) + +if python_version < (3, 0): from cStringIO import StringIO BytesIO = StringIO else: @@ -9,3 +11,10 @@ _ = BytesIO # pyflakes _ = StringIO # pyflakes + +if python_version < (2, 7): + import simplejson as json +else: + import json + +_ = json # pyflakes diff --git a/pyleus/json_fields_bolt.py b/pyleus/json_fields_bolt.py index 0c39f02..47378d6 100644 --- a/pyleus/json_fields_bolt.py +++ b/pyleus/json_fields_bolt.py @@ -3,13 +3,8 @@ import logging -try: - import simplejson as json - _ = json # pyflakes -except ImportError: - import json - -from .storm import SimpleBolt +from pyleus.compat import json +from pyleus.storm import SimpleBolt log = logging.getLogger(__name__) diff --git a/pyleus/storm/component.py b/pyleus/storm/component.py index 7194010..d67c9e1 100644 --- a/pyleus/storm/component.py +++ b/pyleus/storm/component.py @@ -11,12 +11,7 @@ class around Storm configurations. import sys import traceback -try: - import simplejson as json - _ = json # pyflakes -except ImportError: - import json - +from pyleus.compat import json from pyleus.storm import DEFAULT_STREAM from pyleus.storm import StormTuple from pyleus.storm.serializers.msgpack_serializer import MsgpackSerializer diff --git a/pyleus/storm/serializers/json_serializer.py b/pyleus/storm/serializers/json_serializer.py index 6b70310..b4a726f 100644 --- a/pyleus/storm/serializers/json_serializer.py +++ b/pyleus/storm/serializers/json_serializer.py @@ -1,11 +1,6 @@ """JSON implementation of Pyleus serializer""" -try: - import simplejson as json - _ = json # pyflakes -except ImportError: - import json - +from pyleus.compat import json from pyleus.storm import StormWentAwayError from pyleus.storm.serializers.serializer import Serializer diff --git a/setup.py b/setup.py index a2d00bb..739b69b 100644 --- a/setup.py +++ b/setup.py @@ -65,6 +65,10 @@ def run(self): # argparse is in the standard library of Python >= 2.7 extra_install_requires.append("argparse") + # simplejson has better beformance in Python < 2.6 than the built-in json + # module + extra_install_requires.append("simplejson") + setup( name="pyleus", diff --git a/tests/storm/serializers/json_serializer_test.py b/tests/storm/serializers/json_serializer_test.py index ef79aae..293c629 100644 --- a/tests/storm/serializers/json_serializer_test.py +++ b/tests/storm/serializers/json_serializer_test.py @@ -1,11 +1,6 @@ import mock -try: - import simplejson as json - _ = json # pyflakes -except ImportError: - import json - +from pyleus.compat import json from pyleus.compat import StringIO from pyleus.storm.serializers.json_serializer import JSONSerializer from testing.serializer import SerializerTestCase