Handle OrderedDict import attempt in cassandra.util
This commit is contained in:
@@ -31,6 +31,7 @@ from cassandra.marshal import (int8_pack, int8_unpack, uint16_pack, uint16_unpac
|
|||||||
int32_pack, int32_unpack, int64_pack, int64_unpack,
|
int32_pack, int32_unpack, int64_pack, int64_unpack,
|
||||||
float_pack, float_unpack, double_pack, double_unpack,
|
float_pack, float_unpack, double_pack, double_unpack,
|
||||||
varint_pack, varint_unpack)
|
varint_pack, varint_unpack)
|
||||||
|
from cassandra.util import OrderedDict
|
||||||
|
|
||||||
apache_cassandra_type_prefix = 'org.apache.cassandra.db.marshal.'
|
apache_cassandra_type_prefix = 'org.apache.cassandra.db.marshal.'
|
||||||
|
|
||||||
@@ -46,11 +47,6 @@ except ImportError:
|
|||||||
|
|
||||||
sortedset = set
|
sortedset = set
|
||||||
|
|
||||||
try:
|
|
||||||
from collections import OrderedDict
|
|
||||||
except ImportError: # Python <2.7
|
|
||||||
from cassandra.util import OrderedDict # NOQA
|
|
||||||
|
|
||||||
|
|
||||||
def trim_if_startswith(s, prefix):
|
def trim_if_startswith(s, prefix):
|
||||||
if s.startswith(prefix):
|
if s.startswith(prefix):
|
||||||
|
|||||||
@@ -9,11 +9,6 @@ import sys
|
|||||||
import types
|
import types
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
try:
|
|
||||||
from collections import OrderedDict
|
|
||||||
except ImportError: # Python <2.7
|
|
||||||
from cassandra.util import OrderedDict # NOQA
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -30,6 +25,7 @@ from cassandra.cqltypes import (AsciiType, BytesType, BooleanType,
|
|||||||
InetAddressType, IntegerType, ListType,
|
InetAddressType, IntegerType, ListType,
|
||||||
LongType, MapType, SetType, TimeUUIDType,
|
LongType, MapType, SetType, TimeUUIDType,
|
||||||
UTF8Type, UUIDType, lookup_casstype)
|
UTF8Type, UUIDType, lookup_casstype)
|
||||||
|
from cassandra.util import OrderedDict
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
from bisect import bisect_right
|
from bisect import bisect_right
|
||||||
from collections import defaultdict
|
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 hashlib import md5
|
||||||
from itertools import islice, cycle
|
from itertools import islice, cycle
|
||||||
import json
|
import json
|
||||||
@@ -21,6 +17,7 @@ except ImportError:
|
|||||||
import cassandra.cqltypes as types
|
import cassandra.cqltypes as types
|
||||||
from cassandra.marshal import varint_unpack
|
from cassandra.marshal import varint_unpack
|
||||||
from cassandra.pool import Host
|
from cassandra.pool import Host
|
||||||
|
from cassandra.util import OrderedDict
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|||||||
@@ -1,138 +1,140 @@
|
|||||||
from __future__ import with_statement
|
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
|
from UserDict import DictMixin
|
||||||
|
|
||||||
|
try:
|
||||||
|
from collections import OrderedDict
|
||||||
|
except ImportError:
|
||||||
|
# OrderedDict from Python 2.7+
|
||||||
|
|
||||||
class OrderedDict(dict, DictMixin):
|
# Copyright (c) 2009 Raymond Hettinger
|
||||||
""" A dictionary which maintains the insertion order of keys. """
|
#
|
||||||
|
# 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. """
|
""" A dictionary which maintains the insertion order of keys. """
|
||||||
|
|
||||||
if len(args) > 1:
|
def __init__(self, *args, **kwds):
|
||||||
raise TypeError('expected at most 1 arguments, got %d' % len(args))
|
""" A dictionary which maintains the insertion order of keys. """
|
||||||
try:
|
|
||||||
self.__end
|
|
||||||
except AttributeError:
|
|
||||||
self.clear()
|
|
||||||
self.update(*args, **kwds)
|
|
||||||
|
|
||||||
def clear(self):
|
if len(args) > 1:
|
||||||
self.__end = end = []
|
raise TypeError('expected at most 1 arguments, got %d' % len(args))
|
||||||
end += [None, end, end] # sentinel node for doubly linked list
|
try:
|
||||||
self.__map = {} # key --> [key, prev, next]
|
self.__end
|
||||||
dict.clear(self)
|
except AttributeError:
|
||||||
|
self.clear()
|
||||||
|
self.update(*args, **kwds)
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def clear(self):
|
||||||
if key not in 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
|
end = self.__end
|
||||||
curr = end[1]
|
curr = end[1]
|
||||||
curr[2] = end[1] = self.__map[key] = [key, curr, end]
|
while curr is not end:
|
||||||
dict.__setitem__(self, key, value)
|
yield curr[0]
|
||||||
|
curr = curr[1]
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def popitem(self, last=True):
|
||||||
dict.__delitem__(self, key)
|
if not self:
|
||||||
key, prev, next = self.__map.pop(key)
|
raise KeyError('dictionary is empty')
|
||||||
prev[2] = next
|
if last:
|
||||||
next[1] = prev
|
key = reversed(self).next()
|
||||||
|
else:
|
||||||
|
key = iter(self).next()
|
||||||
|
value = self.pop(key)
|
||||||
|
return key, value
|
||||||
|
|
||||||
def __iter__(self):
|
def __reduce__(self):
|
||||||
end = self.__end
|
items = [[k, self[k]] for k in self]
|
||||||
curr = end[2]
|
tmp = self.__map, self.__end
|
||||||
while curr is not end:
|
del self.__map, self.__end
|
||||||
yield curr[0]
|
inst_dict = vars(self).copy()
|
||||||
curr = curr[2]
|
self.__map, self.__end = tmp
|
||||||
|
if inst_dict:
|
||||||
|
return (self.__class__, (items,), inst_dict)
|
||||||
|
return self.__class__, (items,)
|
||||||
|
|
||||||
def __reversed__(self):
|
def keys(self):
|
||||||
end = self.__end
|
return list(self)
|
||||||
curr = end[1]
|
|
||||||
while curr is not end:
|
|
||||||
yield curr[0]
|
|
||||||
curr = curr[1]
|
|
||||||
|
|
||||||
def popitem(self, last=True):
|
setdefault = DictMixin.setdefault
|
||||||
if not self:
|
update = DictMixin.update
|
||||||
raise KeyError('dictionary is empty')
|
pop = DictMixin.pop
|
||||||
if last:
|
values = DictMixin.values
|
||||||
key = reversed(self).next()
|
items = DictMixin.items
|
||||||
else:
|
iterkeys = DictMixin.iterkeys
|
||||||
key = iter(self).next()
|
itervalues = DictMixin.itervalues
|
||||||
value = self.pop(key)
|
iteritems = DictMixin.iteritems
|
||||||
return key, value
|
|
||||||
|
|
||||||
def __reduce__(self):
|
def __repr__(self):
|
||||||
items = [[k, self[k]] for k in self]
|
if not self:
|
||||||
tmp = self.__map, self.__end
|
return '%s()' % (self.__class__.__name__,)
|
||||||
del self.__map, self.__end
|
return '%s(%r)' % (self.__class__.__name__, self.items())
|
||||||
inst_dict = vars(self).copy()
|
|
||||||
self.__map, self.__end = tmp
|
|
||||||
if inst_dict:
|
|
||||||
return (self.__class__, (items,), inst_dict)
|
|
||||||
return self.__class__, (items,)
|
|
||||||
|
|
||||||
def keys(self):
|
def copy(self):
|
||||||
return list(self)
|
return self.__class__(self)
|
||||||
|
|
||||||
setdefault = DictMixin.setdefault
|
@classmethod
|
||||||
update = DictMixin.update
|
def fromkeys(cls, iterable, value=None):
|
||||||
pop = DictMixin.pop
|
d = cls()
|
||||||
values = DictMixin.values
|
for key in iterable:
|
||||||
items = DictMixin.items
|
d[key] = value
|
||||||
iterkeys = DictMixin.iterkeys
|
return d
|
||||||
itervalues = DictMixin.itervalues
|
|
||||||
iteritems = DictMixin.iteritems
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __eq__(self, other):
|
||||||
if not self:
|
if isinstance(other, OrderedDict):
|
||||||
return '%s()' % (self.__class__.__name__,)
|
if len(self) != len(other):
|
||||||
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:
|
|
||||||
return False
|
return False
|
||||||
return True
|
for p, q in zip(self.items(), other.items()):
|
||||||
return dict.__eq__(self, other)
|
if p != q:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
return dict.__eq__(self, other)
|
||||||
|
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
return not self == other
|
return not self == other
|
||||||
|
|
||||||
|
|
||||||
# WeakSet from Python 2.7+ (https://code.google.com/p/weakrefset)
|
# WeakSet from Python 2.7+ (https://code.google.com/p/weakrefset)
|
||||||
|
|||||||
@@ -5,11 +5,7 @@ except ImportError:
|
|||||||
|
|
||||||
from cassandra.cluster import Cluster
|
from cassandra.cluster import Cluster
|
||||||
from cassandra.decoder import tuple_factory, named_tuple_factory, dict_factory, ordered_dict_factory
|
from cassandra.decoder import tuple_factory, named_tuple_factory, dict_factory, ordered_dict_factory
|
||||||
|
from cassandra.util import OrderedDict
|
||||||
try:
|
|
||||||
from collections import OrderedDict
|
|
||||||
except ImportError: # Python <2.7
|
|
||||||
from cassandra.util import OrderedDict # NOQA
|
|
||||||
|
|
||||||
|
|
||||||
class TestFactories(unittest.TestCase):
|
class TestFactories(unittest.TestCase):
|
||||||
|
|||||||
@@ -16,10 +16,7 @@ from cassandra import InvalidRequest
|
|||||||
from cassandra.cluster import Cluster
|
from cassandra.cluster import Cluster
|
||||||
from cassandra.cqltypes import Int32Type, EMPTY
|
from cassandra.cqltypes import Int32Type, EMPTY
|
||||||
from cassandra.decoder import dict_factory
|
from cassandra.decoder import dict_factory
|
||||||
try:
|
from cassandra.util import OrderedDict
|
||||||
from collections import OrderedDict
|
|
||||||
except ImportError:
|
|
||||||
from cassandra.util import OrderedDict # noqa
|
|
||||||
|
|
||||||
from tests.integration import get_server_versions
|
from tests.integration import get_server_versions
|
||||||
|
|
||||||
|
|||||||
@@ -13,13 +13,8 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
sortedset = set
|
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.cqltypes import lookup_casstype
|
||||||
|
from cassandra.util import OrderedDict
|
||||||
|
|
||||||
marshalled_value_pairs = (
|
marshalled_value_pairs = (
|
||||||
# binary form, type, python native type
|
# binary form, type, python native type
|
||||||
|
|||||||
@@ -6,11 +6,7 @@ except ImportError:
|
|||||||
from cassandra.query import bind_params, ValueSequence
|
from cassandra.query import bind_params, ValueSequence
|
||||||
from cassandra.query import PreparedStatement, BoundStatement
|
from cassandra.query import PreparedStatement, BoundStatement
|
||||||
from cassandra.cqltypes import Int32Type
|
from cassandra.cqltypes import Int32Type
|
||||||
|
from cassandra.util import OrderedDict
|
||||||
try:
|
|
||||||
from collections import OrderedDict
|
|
||||||
except ImportError: # Python <2.7
|
|
||||||
from cassandra.util import OrderedDict # NOQA
|
|
||||||
|
|
||||||
|
|
||||||
class ParamBindingTest(unittest.TestCase):
|
class ParamBindingTest(unittest.TestCase):
|
||||||
|
|||||||
Reference in New Issue
Block a user