From fde2fcc3b3e347a519ff9bf5b4572382b2a73601 Mon Sep 17 00:00:00 2001 From: Tyler Hobbs Date: Fri, 28 Feb 2014 15:27:07 -0600 Subject: [PATCH] Handle OrderedDict import attempt in cassandra.util --- cassandra/cqltypes.py | 6 +- cassandra/decoder.py | 6 +- cassandra/metadata.py | 5 +- cassandra/util.py | 228 ++++++++++--------- tests/integration/standard/test_factories.py | 6 +- tests/integration/standard/test_types.py | 5 +- tests/unit/test_marshalling.py | 7 +- tests/unit/test_parameter_binding.py | 6 +- 8 files changed, 122 insertions(+), 147 deletions(-) diff --git a/cassandra/cqltypes.py b/cassandra/cqltypes.py index 7c6f5550..cd2cdac7 100644 --- a/cassandra/cqltypes.py +++ b/cassandra/cqltypes.py @@ -31,6 +31,7 @@ from cassandra.marshal import (int8_pack, int8_unpack, uint16_pack, uint16_unpac int32_pack, int32_unpack, int64_pack, int64_unpack, float_pack, float_unpack, double_pack, double_unpack, varint_pack, varint_unpack) +from cassandra.util import OrderedDict apache_cassandra_type_prefix = 'org.apache.cassandra.db.marshal.' @@ -46,11 +47,6 @@ except ImportError: sortedset = set -try: - from collections import OrderedDict -except ImportError: # Python <2.7 - from cassandra.util import OrderedDict # NOQA - def trim_if_startswith(s, prefix): if s.startswith(prefix): diff --git a/cassandra/decoder.py b/cassandra/decoder.py index e8b67adb..0c4072eb 100644 --- a/cassandra/decoder.py +++ b/cassandra/decoder.py @@ -9,11 +9,6 @@ import sys import types from uuid import UUID -try: - from collections import OrderedDict -except ImportError: # Python <2.7 - from cassandra.util import OrderedDict # NOQA - try: from cStringIO import StringIO except ImportError: @@ -30,6 +25,7 @@ from cassandra.cqltypes import (AsciiType, BytesType, BooleanType, InetAddressType, IntegerType, ListType, LongType, MapType, SetType, TimeUUIDType, UTF8Type, UUIDType, lookup_casstype) +from cassandra.util import OrderedDict log = logging.getLogger(__name__) diff --git a/cassandra/metadata.py b/cassandra/metadata.py index f0b5777a..124f6942 100644 --- a/cassandra/metadata.py +++ b/cassandra/metadata.py @@ -1,9 +1,5 @@ from bisect import bisect_right from collections import defaultdict -try: - from collections import OrderedDict -except ImportError: # Python <2.7 - from cassandra.util import OrderedDict # NOQA from hashlib import md5 from itertools import islice, cycle import json @@ -21,6 +17,7 @@ except ImportError: import cassandra.cqltypes as types from cassandra.marshal import varint_unpack from cassandra.pool import Host +from cassandra.util import OrderedDict log = logging.getLogger(__name__) diff --git a/cassandra/util.py b/cassandra/util.py index 8204df65..0b52e8b2 100644 --- a/cassandra/util.py +++ b/cassandra/util.py @@ -1,138 +1,140 @@ from __future__ import with_statement -# OrderedDict from Python 2.7+ - -# Copyright (c) 2009 Raymond Hettinger -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation files -# (the "Software"), to deal in the Software without restriction, -# including without limitation the rights to use, copy, modify, merge, -# publish, distribute, sublicense, and/or sell copies of the Software, -# and to permit persons to whom the Software is furnished to do so, -# subject to the following conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. - from UserDict import DictMixin +try: + from collections import OrderedDict +except ImportError: + # OrderedDict from Python 2.7+ -class OrderedDict(dict, DictMixin): - """ A dictionary which maintains the insertion order of keys. """ + # Copyright (c) 2009 Raymond Hettinger + # + # Permission is hereby granted, free of charge, to any person + # obtaining a copy of this software and associated documentation files + # (the "Software"), to deal in the Software without restriction, + # including without limitation the rights to use, copy, modify, merge, + # publish, distribute, sublicense, and/or sell copies of the Software, + # and to permit persons to whom the Software is furnished to do so, + # subject to the following conditions: + # + # The above copyright notice and this permission notice shall be + # included in all copies or substantial portions of the Software. + # + # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + # OTHER DEALINGS IN THE SOFTWARE. - def __init__(self, *args, **kwds): + class OrderedDict(dict, DictMixin): # noqa """ A dictionary which maintains the insertion order of keys. """ - if len(args) > 1: - raise TypeError('expected at most 1 arguments, got %d' % len(args)) - try: - self.__end - except AttributeError: - self.clear() - self.update(*args, **kwds) + def __init__(self, *args, **kwds): + """ A dictionary which maintains the insertion order of keys. """ - def clear(self): - self.__end = end = [] - end += [None, end, end] # sentinel node for doubly linked list - self.__map = {} # key --> [key, prev, next] - dict.clear(self) + if len(args) > 1: + raise TypeError('expected at most 1 arguments, got %d' % len(args)) + try: + self.__end + except AttributeError: + self.clear() + self.update(*args, **kwds) - def __setitem__(self, key, value): - if key not in self: + def clear(self): + self.__end = end = [] + end += [None, end, end] # sentinel node for doubly linked list + self.__map = {} # key --> [key, prev, next] + dict.clear(self) + + def __setitem__(self, key, value): + if key not in self: + end = self.__end + curr = end[1] + curr[2] = end[1] = self.__map[key] = [key, curr, end] + dict.__setitem__(self, key, value) + + def __delitem__(self, key): + dict.__delitem__(self, key) + key, prev, next = self.__map.pop(key) + prev[2] = next + next[1] = prev + + def __iter__(self): + end = self.__end + curr = end[2] + while curr is not end: + yield curr[0] + curr = curr[2] + + def __reversed__(self): end = self.__end curr = end[1] - curr[2] = end[1] = self.__map[key] = [key, curr, end] - dict.__setitem__(self, key, value) + while curr is not end: + yield curr[0] + curr = curr[1] - def __delitem__(self, key): - dict.__delitem__(self, key) - key, prev, next = self.__map.pop(key) - prev[2] = next - next[1] = prev + def popitem(self, last=True): + if not self: + raise KeyError('dictionary is empty') + if last: + key = reversed(self).next() + else: + key = iter(self).next() + value = self.pop(key) + return key, value - def __iter__(self): - end = self.__end - curr = end[2] - while curr is not end: - yield curr[0] - curr = curr[2] + def __reduce__(self): + items = [[k, self[k]] for k in self] + tmp = self.__map, self.__end + del self.__map, self.__end + inst_dict = vars(self).copy() + self.__map, self.__end = tmp + if inst_dict: + return (self.__class__, (items,), inst_dict) + return self.__class__, (items,) - def __reversed__(self): - end = self.__end - curr = end[1] - while curr is not end: - yield curr[0] - curr = curr[1] + def keys(self): + return list(self) - def popitem(self, last=True): - if not self: - raise KeyError('dictionary is empty') - if last: - key = reversed(self).next() - else: - key = iter(self).next() - value = self.pop(key) - return key, value + setdefault = DictMixin.setdefault + update = DictMixin.update + pop = DictMixin.pop + values = DictMixin.values + items = DictMixin.items + iterkeys = DictMixin.iterkeys + itervalues = DictMixin.itervalues + iteritems = DictMixin.iteritems - def __reduce__(self): - items = [[k, self[k]] for k in self] - tmp = self.__map, self.__end - del self.__map, self.__end - inst_dict = vars(self).copy() - self.__map, self.__end = tmp - if inst_dict: - return (self.__class__, (items,), inst_dict) - return self.__class__, (items,) + def __repr__(self): + if not self: + return '%s()' % (self.__class__.__name__,) + return '%s(%r)' % (self.__class__.__name__, self.items()) - def keys(self): - return list(self) + def copy(self): + return self.__class__(self) - setdefault = DictMixin.setdefault - update = DictMixin.update - pop = DictMixin.pop - values = DictMixin.values - items = DictMixin.items - iterkeys = DictMixin.iterkeys - itervalues = DictMixin.itervalues - iteritems = DictMixin.iteritems + @classmethod + def fromkeys(cls, iterable, value=None): + d = cls() + for key in iterable: + d[key] = value + return d - def __repr__(self): - if not self: - return '%s()' % (self.__class__.__name__,) - return '%s(%r)' % (self.__class__.__name__, self.items()) - - def copy(self): - return self.__class__(self) - - @classmethod - def fromkeys(cls, iterable, value=None): - d = cls() - for key in iterable: - d[key] = value - return d - - def __eq__(self, other): - if isinstance(other, OrderedDict): - if len(self) != len(other): - return False - for p, q in zip(self.items(), other.items()): - if p != q: + def __eq__(self, other): + if isinstance(other, OrderedDict): + if len(self) != len(other): return False - return True - return dict.__eq__(self, other) + for p, q in zip(self.items(), other.items()): + if p != q: + return False + return True + return dict.__eq__(self, other) - def __ne__(self, other): - return not self == other + def __ne__(self, other): + return not self == other # WeakSet from Python 2.7+ (https://code.google.com/p/weakrefset) diff --git a/tests/integration/standard/test_factories.py b/tests/integration/standard/test_factories.py index 11fe1659..2a773836 100644 --- a/tests/integration/standard/test_factories.py +++ b/tests/integration/standard/test_factories.py @@ -5,11 +5,7 @@ except ImportError: from cassandra.cluster import Cluster from cassandra.decoder import tuple_factory, named_tuple_factory, dict_factory, ordered_dict_factory - -try: - from collections import OrderedDict -except ImportError: # Python <2.7 - from cassandra.util import OrderedDict # NOQA +from cassandra.util import OrderedDict class TestFactories(unittest.TestCase): diff --git a/tests/integration/standard/test_types.py b/tests/integration/standard/test_types.py index a350c457..6885f379 100644 --- a/tests/integration/standard/test_types.py +++ b/tests/integration/standard/test_types.py @@ -16,10 +16,7 @@ from cassandra import InvalidRequest from cassandra.cluster import Cluster from cassandra.cqltypes import Int32Type, EMPTY from cassandra.decoder import dict_factory -try: - from collections import OrderedDict -except ImportError: - from cassandra.util import OrderedDict # noqa +from cassandra.util import OrderedDict from tests.integration import get_server_versions diff --git a/tests/unit/test_marshalling.py b/tests/unit/test_marshalling.py index 31277e87..59d60fb8 100644 --- a/tests/unit/test_marshalling.py +++ b/tests/unit/test_marshalling.py @@ -13,13 +13,8 @@ try: except ImportError: sortedset = set -try: - from collections import OrderedDict -except ImportError: # Python <2.7 - from cassandra.util import OrderedDict # NOQA - - from cassandra.cqltypes import lookup_casstype +from cassandra.util import OrderedDict marshalled_value_pairs = ( # binary form, type, python native type diff --git a/tests/unit/test_parameter_binding.py b/tests/unit/test_parameter_binding.py index 3ed66647..4fa0d205 100644 --- a/tests/unit/test_parameter_binding.py +++ b/tests/unit/test_parameter_binding.py @@ -6,11 +6,7 @@ except ImportError: from cassandra.query import bind_params, ValueSequence from cassandra.query import PreparedStatement, BoundStatement from cassandra.cqltypes import Int32Type - -try: - from collections import OrderedDict -except ImportError: # Python <2.7 - from cassandra.util import OrderedDict # NOQA +from cassandra.util import OrderedDict class ParamBindingTest(unittest.TestCase):