Skip to content
This repository has been archived by the owner on Feb 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4 from Kroisse/python3
Browse files Browse the repository at this point in the history
Experimental supports for Python 3.3 and higher
  • Loading branch information
dahlia committed Aug 4, 2013
2 parents b037aeb + 0db5458 commit 1e7d7b2
Show file tree
Hide file tree
Showing 21 changed files with 192 additions and 116 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -2,6 +2,7 @@ language: python
python:
- 2.6
- 2.7
- 3.3
- pypy
services:
- redis-server
Expand Down
2 changes: 0 additions & 2 deletions pytest.ini

This file was deleted.

12 changes: 6 additions & 6 deletions sider/hash.py
Expand Up @@ -10,7 +10,7 @@
"""
import collections
from .session import Session
from .types import Bulk, ByteString
from .types import Bulk, String
from .transaction import query, manipulative
from . import utils

Expand Down Expand Up @@ -54,7 +54,7 @@ class Hash(collections.MutableMapping):
value_type = None

def __init__(self, session, key,
key_type=ByteString, value_type=ByteString):
key_type=String, value_type=String):
if not isinstance(session, Session):
raise TypeError('session must be a sider.session.Session '
'instance, not ' + repr(session))
Expand Down Expand Up @@ -238,8 +238,8 @@ def items(self):
items = self.session.client.hgetall(self.key)
decode_key = self.key_type.decode
decode_value = self.value_type.decode
return frozenset((decode_key(k), decode_value(v))
for k, v in items.iteritems())
return frozenset((decode_key(k), decode_value(items[k]))
for k in items)

@manipulative
def clear(self):
Expand Down Expand Up @@ -373,8 +373,8 @@ def _raw_update(self, value, pipe, encoded=False):

def __repr__(self):
cls = type(self)
items = list(self.iteritems())
items.sort(key=lambda (key, _): key)
items = list(self.items())
items.sort(key=lambda elem: elem[0])
elements = ', '.join('{0!r}: {1!r}'.format(*pair) for pair in items)
return '<{0}.{1} ({2!r}) {{{3}}}>'.format(cls.__module__, cls.__name__,
self.key, elements)
Expand Down
5 changes: 3 additions & 2 deletions sider/lazyimport.py
Expand Up @@ -7,6 +7,7 @@
"""
from __future__ import absolute_import
import types
import functools


class DeferredModule(types.ModuleType):
Expand All @@ -23,7 +24,7 @@ def __getattr__(self, name):
mod = self.__actual_module__
if mod is None:
mod = __import__(self.__name__)
mod = reduce(getattr, self.__name__.split('.')[1:], mod)
mod = functools.reduce(getattr, self.__name__.split('.')[1:], mod)
self.__actual_module__ = mod
return getattr(mod, name)

Expand Down Expand Up @@ -65,6 +66,6 @@ def __repr__(self):
version = DeferredModule('sider.version')


__all__ = tuple(name for name, value in globals().iteritems()
__all__ = tuple(name for name, value in globals().items()
if isinstance(value, DeferredModule))

8 changes: 4 additions & 4 deletions sider/list.py
Expand Up @@ -13,7 +13,7 @@
import numbers
import warnings
from redis.exceptions import ResponseError
from .types import Bulk, ByteString
from .types import Bulk, String
from .session import Session
from .transaction import manipulative, query
from .warnings import PerformanceWarning
Expand Down Expand Up @@ -62,7 +62,7 @@ class List(collections.MutableSequence):
#: (:class:`sider.types.Bulk`) The type of list values.
value_type = None

def __init__(self, session, key, value_type=ByteString):
def __init__(self, session, key, value_type=String):
if not isinstance(session, Session):
raise TypeError('session must be a sider.session.Session '
'instance, not ' + repr(session))
Expand Down Expand Up @@ -132,7 +132,7 @@ def __getitem__(self, index):
result = self.session.client.lrange(self.key, start, stop)
if index.step is not None:
result = result[::index.step]
return map(decode, result)
return list(map(decode, result))
raise TypeError('indices must be integers, not ' + repr(index))

def __setitem__(self, index, value):
Expand All @@ -157,7 +157,7 @@ def __setitem__(self, index, value):
raise ValueError('slice with step is not supported for '
'assignment')
elif index.start in (0, None) and index.stop == 1:
seq = map(encode, value)
seq = list(map(encode, value))
seq.reverse()
self.session.mark_manipulative([self.key])
if self.session.server_version_info < (2, 4, 0):
Expand Down
7 changes: 4 additions & 3 deletions sider/set.py
Expand Up @@ -11,7 +11,7 @@
from __future__ import absolute_import
import collections
from .session import Session
from .types import Bulk, ByteString
from .types import Bulk, String
from .transaction import manipulative, query
from . import utils

Expand Down Expand Up @@ -62,7 +62,7 @@ class Set(collections.MutableSet):
"""

def __init__(self, session, key, value_type=ByteString):
def __init__(self, session, key, value_type=String):
if not isinstance(session, Session):
raise TypeError('session must be a sider.session.Session '
'instance, not ' + repr(session))
Expand Down Expand Up @@ -602,7 +602,8 @@ def union(self, *sets):
else:
offline_sets.append(operand)
union = set()
for value_type, group in online_sets.iteritems():
for value_type in online_sets:
group = online_sets[value_type]
keys = (s.key for s in group)
self.session.mark_query([self.key])
subset = self.session.client.sunion(*keys)
Expand Down
11 changes: 6 additions & 5 deletions sider/sortedset.py
Expand Up @@ -12,7 +12,7 @@
import collections
import itertools
from .session import Session
from .types import Bulk, ByteString
from .types import Bulk, String
from .transaction import query, manipulative


Expand Down Expand Up @@ -71,7 +71,7 @@ class SortedSet(collections.MutableMapping, collections.MutableSet):
#: (:class:`sider.types.Bulk`) The type of set elements.
value_type = None

def __init__(self, session, key, value_type=ByteString):
def __init__(self, session, key, value_type=String):
if not isinstance(session, Session):
raise TypeError('session must be a sider.session.Session '
'instance, not ' + repr(session))
Expand All @@ -98,7 +98,8 @@ def __len__(self):
@query
def __iter__(self):
result = self.session.client.zrange(self.key, 0, -1)
return itertools.imap(self.value_type.decode, result)
for i in result:
yield self.value_type.decode(i)

@query
def __contains__(self, member):
Expand Down Expand Up @@ -713,7 +714,7 @@ def block(trial, transaction):
zincrby(key, value=el, amount=1)
else:
raise TypeError('expected iterable, not ' + repr(set_))
for el, score in keywords.iteritems():
for el, score in getattr(keywords, 'iteritems', keywords.items)():
if not isinstance(score, numbers.Real):
raise TypeError('score must be a float, not ' +
repr(score))
Expand All @@ -728,7 +729,7 @@ def block(trial, transaction):
def __repr__(self):
cls = type(self)
pairs= list(self.items())
pairs.sort(key=lambda (element, score): (score, element))
pairs.sort(key=lambda pair: (pair[1], pair[0]))
elements = ', '.join(repr(v) if s == 1 else '{0!r}: {1!r}'.format(v, s)
for v, s in pairs)
return '<{0}.{1} ({2!r}) {{{3}}}>'.format(cls.__module__, cls.__name__,
Expand Down
15 changes: 12 additions & 3 deletions sider/threadlocal.py
Expand Up @@ -115,13 +115,22 @@ def items(self):
return self.current.items()

def iteritems(self):
return self.current.iteritems()
try:
return self.current.iteritems()
except AttributeError:
return self.items()

def iterkeys(self):
return self.current.keys()
try:
return self.current.iterkeys()
except AttributeError:
return self.keys()

def itervalues(self):
return self.current.itervalues()
try:
return self.current.itervalues()
except AttributeError:
return self.values()

def keys(self):
return self.current.keys()
Expand Down
8 changes: 7 additions & 1 deletion sider/transaction.py
Expand Up @@ -72,6 +72,12 @@ def block(trial, transaction):
from . import lazyimport


try:
_string_type = basestring
except NameError:
_string_type = str


class Transaction(object):
"""Transaction block.
Expand Down Expand Up @@ -222,7 +228,7 @@ def watch(self, keys, initialize=False):
:type initialize: :class:`bool`
"""
if isinstance(keys, basestring):
if isinstance(keys, _string_type):
warnings.warn('you could probably want to watch [{0!r}] instead of '
'{1!r}; do not directly pass a string but a list of '
'string to be explicit'.format(keys, list(keys)),
Expand Down

0 comments on commit 1e7d7b2

Please sign in to comment.