Skip to content

Commit 8c2a1bf

Browse files
committed
Merge branch 'virtusize-master'
2 parents 230e8d9 + 0d407d7 commit 8c2a1bf

File tree

3 files changed

+143
-31
lines changed

3 files changed

+143
-31
lines changed
Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,79 @@
1-
from collections import OrderedDict
21
import re
32

4-
first_cap_re = re.compile('(.)([A-Z][a-z]+)')
5-
all_cap_re = re.compile('([a-z0-9])([A-Z])')
3+
from collections import OrderedDict
4+
5+
6+
def camelize_key(string, uppercase_first_letter=True):
7+
"""
8+
From: https://github.com/jpvanhal/inflection
9+
10+
Convert strings to CamelCase.
11+
Examples::
12+
>>> camelize("device_type")
13+
"DeviceType"
14+
>>> camelize("device_type", False)
15+
"deviceType"
16+
:func:`camelize` can be though as a inverse of :func:`underscore`, although
17+
there are some cases where that does not hold::
18+
>>> camelize(underscore("IOError"))
19+
"IoError"
20+
:param uppercase_first_letter: if set to `True` :func:`camelize` converts
21+
strings to UpperCamelCase. If set to `False` :func:`camelize` produces
22+
lowerCamelCase. Defaults to `True`.
23+
"""
24+
if uppercase_first_letter:
25+
return re.sub(r"(?:^|_)(.)", lambda m: m.group(1).upper(), string)
26+
else:
27+
return string[0].lower() + camelize_key(string)[1:]
628

729

8-
def underscoreToCamel(match):
9-
return match.group()[0] + match.group()[2].upper()
30+
def underscore_key(string):
31+
"""
32+
From: https://github.com/jpvanhal/inflection
33+
34+
Make an underscored, lowercase form from the expression in the string.
35+
Example::
36+
>>> underscore("DeviceType")
37+
"device_type"
38+
As a rule of thumb you can think of :func:`underscore` as the inverse of
39+
:func:`camelize`, though there are cases where that does not hold::
40+
>>> camelize(underscore("IOError"))
41+
"IoError"
42+
"""
43+
string = re.sub(r"([A-Z]+)([A-Z][a-z])", r'\1_\2', string)
44+
string = re.sub(r"([a-z\d])([A-Z])", r'\1_\2', string)
45+
string = string.replace("-", "_")
46+
return string.lower()
1047

1148

1249
def camelize(data):
1350
if isinstance(data, dict):
1451
new_dict = OrderedDict()
15-
for key, value in data.items():
16-
new_key = re.sub(r"[a-z0-9]_[a-z]", underscoreToCamel, key)
17-
new_dict[new_key] = camelize(value)
52+
for k, v in data.items():
53+
new_dict[camelize_key(k, False)] = camelize(v)
54+
1855
return new_dict
56+
1957
if isinstance(data, list):
20-
for i in range(len(data)):
21-
data[i] = camelize(data[i])
22-
return data
23-
if isinstance(data, tuple):
24-
camelized_data = []
25-
for i in range(len(data)):
26-
camelized_data.append(camelize(data[i]))
27-
return camelized_data
28-
return data
58+
return [camelize(x) for x in data]
2959

60+
if isinstance(data, tuple):
61+
return tuple(camelize(x) for x in data)
3062

31-
def camel_to_underscore(name):
32-
s1 = first_cap_re.sub(r'\1_\2', name)
33-
return all_cap_re.sub(r'\1_\2', s1).lower()
63+
return data
3464

3565

3666
def underscoreize(data):
3767
if isinstance(data, dict):
38-
new_dict = {}
68+
new_dict = dict()
3969
for key, value in data.items():
40-
new_key = camel_to_underscore(key)
41-
new_dict[new_key] = underscoreize(value)
70+
new_dict[underscore_key(key)] = underscoreize(value)
4271
return new_dict
43-
if isinstance(data, (list, tuple)):
44-
for i in range(len(data)):
45-
data[i] = underscoreize(data[i])
46-
return data
72+
73+
if isinstance(data, list):
74+
return [underscoreize(x) for x in data]
75+
76+
if isinstance(data, tuple):
77+
return tuple(underscoreize(x) for x in data)
78+
4779
return data

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
djangorestframework>=2.0.0
1+
djangorestframework>=2.0.0

tests.py

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,55 @@
77

88

99
class UnderscoreToCamelTestCase(TestCase):
10+
<<<<<<< HEAD
1011

1112
def test_under_to_camel(self):
13+
=======
14+
def test_under_to_camel_dict(self):
15+
>>>>>>> 28000e9d5be3b5de4e410ba773db8d4237a33ca5
1216
input = {
1317
"title_display": 1
1418
}
1519
output = {
1620
"titleDisplay": 1
1721
}
22+
result = camelize(input)
23+
self.assertEqual(result, output)
24+
self.assertIsNot(result, input, "should not change original dict")
25+
26+
def test_under_to_camel_list(self):
27+
input = [
28+
{"title_display": 1}
29+
]
30+
output = [
31+
{"titleDisplay": 1}
32+
]
33+
result = camelize(input)
34+
self.assertEqual(result, output)
35+
self.assertIsNot(result, input, "should not change original list")
36+
37+
def test_under_to_camel_tuple(self):
38+
input = (
39+
{"title_display": 1},
40+
)
41+
output = (
42+
{"titleDisplay": 1},
43+
)
44+
result = camelize(input)
45+
self.assertEqual(result, output)
46+
self.assertIsNot(result, input, "should not change original tuple")
47+
48+
def test_under_to_camel_nested(self):
49+
input = {
50+
"title_display": 1,
51+
"a_list": [1, "two_three", {"three_four": 5}],
52+
"a_tuple": ("one_two", 3)
53+
}
54+
output = {
55+
"titleDisplay": 1,
56+
"aList": [1, "two_three", {"threeFour": 5}],
57+
"aTuple": ("one_two", 3)
58+
}
1859
self.assertEqual(camelize(input), output)
1960

2061
def test_tuples(self):
@@ -27,19 +68,58 @@ def test_tuples(self):
2768
self.assertEqual(camelize(input), output)
2869

2970
class CamelToUnderscoreTestCase(TestCase):
30-
def test_under_to_camel(self):
71+
def test_camel_to_under_dict(self):
3172
input = {
3273
"titleDisplay": 1
3374
}
3475
output = {
3576
"title_display": 1
3677
}
78+
result = underscoreize(input)
79+
self.assertEqual(result, output)
80+
self.assertIsNot(result, input, "should not change original dict")
81+
82+
def test_camel_to_under_list(self):
83+
input = [
84+
{"titleDisplay": 1}
85+
]
86+
output = [
87+
{"title_display": 1}
88+
]
89+
result = underscoreize(input)
90+
self.assertEqual(result, output)
91+
self.assertIsNot(result, input, "should not change original list")
92+
93+
def test_camel_to_under_tuple(self):
94+
input = [
95+
{"titleDisplay": 1}
96+
]
97+
output = [
98+
{"title_display": 1}
99+
]
100+
result = underscoreize(input)
101+
self.assertEqual(result, output)
102+
self.assertIsNot(result, input, "should not change original tuple")
103+
104+
def test_camel_to_under_nested(self):
105+
input = {
106+
"titleDisplay": 1,
107+
"aList": [1, "two_three", {"threeFour": 5}],
108+
"aTuple": ("one_two", 3)
109+
}
110+
output = {
111+
"title_display": 1,
112+
"a_list": [1, "two_three", {"three_four": 5}],
113+
"a_tuple": ("one_two", 3)
114+
}
37115
self.assertEqual(underscoreize(input), output)
38116

39117

40118
class CompatibilityTest(TestCase):
41119
def test_compatibility(self):
42120
input = {
43-
"title_245a_display": 1
121+
"title_display": 1,
122+
"a_list": [1, "two_three", {"three_four": 5}],
123+
"a_tuple": ("one_two", 3)
44124
}
45-
self.assertEqual(underscoreize(camelize(input)), input)
125+
self.assertEqual(underscoreize(camelize(input)), input)

0 commit comments

Comments
 (0)