Skip to content

Commit b846cf3

Browse files
committed
Supporting Python 2.6
1 parent f32d97f commit b846cf3

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

source_py2/python_toolbox/monkeypatching_tools.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import collections
77
import inspect
88
import types
9+
import sys
910

1011
from python_toolbox.third_party import funcsigs
1112

@@ -75,7 +76,16 @@ def decorator(function):
7576
raise NotImplementedError
7677
name_ = function.getter.__name__
7778
elif isinstance(function, (classmethod, staticmethod)):
78-
name_ = function.__func__.__name__
79+
try:
80+
name_ = function.__func__.__name__
81+
except AttributeError:
82+
assert sys.version_info[:2] == (2, 6)
83+
raise NotImplementedError(
84+
"`monkeypatch` can't deal with `staticmethod` "
85+
"and `classmethod` objects in Python 2.6. It "
86+
"works in Python 2.7 and above."
87+
)
88+
7989
elif isinstance(function, property):
8090
name_ = function.fget.__name__
8191
else:
@@ -107,14 +117,16 @@ def change_defaults(function=None, new_defaults={}):
107117
Can be used both as a straight function and as a decorater to a function to
108118
be changed.
109119
'''
120+
from python_toolbox import nifty_collections
121+
110122
def change_defaults_(function_, new_defaults_):
111123
signature = funcsigs.Signature.from_function(function_)
112124
defaults = list(function_.__defaults__ or ())
113125
non_keyword_only_defaultful_parameters = defaultful_parameters = \
114126
dict_tools.filter_items(
115127
signature.parameters,
116128
lambda name, parameter: parameter.default != funcsigs._empty,
117-
force_dict_type=collections.OrderedDict
129+
force_dict_type=nifty_collections.OrderedDict
118130
)
119131

120132
non_existing_arguments = set(new_defaults) - set(defaultful_parameters)

source_py2/test_python_toolbox/test_logic_tools/test_get_equivalence_classes.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@
1010
def test():
1111
assert get_equivalence_classes([1, 2, 3, 1j, 2j, 3j, 1+1j, 2+2j, 3+3j],
1212
abs) == {
13-
1: {1, 1j},
14-
2: {2, 2j},
15-
3: {3, 3j},
16-
abs(1 + 1j): {1 + 1j},
17-
abs(2 + 2j): {2 + 2j},
18-
abs(3 + 3j): {3 + 3j},
13+
1: set((1, 1j,)),
14+
2: set((2, 2j,)),
15+
3: set((3, 3j,)),
16+
abs(1 + 1j): set((1 + 1j,)),
17+
abs(2 + 2j): set((2 + 2j,)),
18+
abs(3 + 3j): set((3 + 3j,)),
1919
}
2020

2121
assert get_equivalence_classes({1: 2, 3: 4, 'meow': 2}) == \
22-
{2: {1, 'meow'}, 4: {3}}
22+
{2: set((1, 'meow')), 4: set((3,))}
2323

2424
def test_iterable_input():
2525
assert get_equivalence_classes(range(1, 5), str) == \
26-
{'1': {1}, '2': {2}, '3': {3}, '4': {4},}
26+
{'1': set((1,)), '2': set((2,)), '3': set((3,)), '4': set((4,)),}
2727

2828
assert get_equivalence_classes([1, 2+3j, 4, 5-6j], 'imag') \
29-
== {0: {1, 4}, 3: {2+3j}, -6: {5-6j}}
29+
== {0: set((1, 4)), 3: set((2+3j,)), -6: set((5-6j,))}
3030

3131

3232
def test_ordered_dict_output():
@@ -35,17 +35,17 @@ def test_ordered_dict_output():
3535
assert get_equivalence_classes(
3636
nifty_collections.OrderedDict(((1, 2), (3, 4), ('meow', 2))),
3737
use_ordered_dict=True) == \
38-
nifty_collections.OrderedDict([(2, {1, 'meow'}), (4, {3})])
38+
nifty_collections.OrderedDict([(2, set((1, 'meow'))), (4, set((3,)))])
3939

4040
assert get_equivalence_classes(
4141
nifty_collections.OrderedDict((('meow', 2), (1, 2), (3, 4))),
4242
use_ordered_dict=True) == \
43-
nifty_collections.OrderedDict([(2, {1, 'meow'}), (4, {3})])
43+
nifty_collections.OrderedDict([(2, set((1, 'meow'))), (4, set((3,)))])
4444

4545
assert get_equivalence_classes(
4646
nifty_collections.OrderedDict(((3, 4), (1, 2), ('meow', 2))),
4747
use_ordered_dict=True) == \
48-
nifty_collections.OrderedDict([(4, {3}), (2, {1, 'meow'})])
48+
nifty_collections.OrderedDict([(4, set((3,))), (2, set((1, 'meow',)))])
4949

5050
assert get_equivalence_classes(
5151
nifty_collections.OrderedDict(((1, 2), (3, 4), ('meow', 2))),
@@ -63,9 +63,9 @@ def test_ordered_dict_output():
6363

6464
assert get_equivalence_classes({1: 2, 3: 4, 'meow': 2},
6565
sort_ordered_dict=True) == \
66-
nifty_collections.OrderedDict([(2, {1, 'meow'}), (4, {3})])
66+
nifty_collections.OrderedDict([(2, set((1, 'meow'))), (4, set((3,)))])
6767

6868
assert get_equivalence_classes({1: 2, 3: 4, 'meow': 2},
6969
sort_ordered_dict=lambda x: -x) == \
70-
nifty_collections.OrderedDict([(4, {3}), (2, {1, 'meow'})])
70+
nifty_collections.OrderedDict([(4, set((3,))), (2, set((1, 'meow')))])
7171

source_py2/test_python_toolbox/test_monkeypatching_tools/test_monkeypatch.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def f(): pass
136136

137137

138138
def test_monkeypatch_staticmethod():
139-
139+
if sys.version_info[:2] == (2, 6):
140+
raise nose.SkipTest
140141
class A(EqualByIdentity):
141142
@staticmethod
142143
def my_static_method(x):
@@ -158,6 +159,8 @@ def my_static_method(x):
158159

159160

160161
def test_monkeypatch_classmethod():
162+
if sys.version_info[:2] == (2, 6):
163+
raise nose.SkipTest
161164

162165
class A(EqualByIdentity):
163166
@classmethod
@@ -186,6 +189,9 @@ def test_monkeypatch_classmethod_subclass():
186189
187190
This is useful in Django, that uses its own `classmethod` subclass.
188191
'''
192+
if sys.version_info[:2] == (2, 6):
193+
raise nose.SkipTest
194+
189195
class FunkyClassMethod(classmethod):
190196
is_funky = True
191197

0 commit comments

Comments
 (0)