-
Notifications
You must be signed in to change notification settings - Fork 0
/
underscore_dict.py
66 lines (51 loc) · 1.79 KB
/
underscore_dict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class underscore_dict(dict):
"""
a dict like object that forces key to be pythonic and follow the underscore
convention.
eg::
>>> d = underscore_dict(dict(myKey='my_value'))
>>> print d
'my_key': 'my_value'}
>>> d['my_key'] = 'my_value_2'
>>> print d
{'my_key': 'my_value_2'}
>>> d.update(myKey='my_value_3')
>>> print d
{'my_key': 'my_value_3'}
>>> del d['my_key']
>>> print d
{}
And this is what happens when java code gets too close to python code.
"""
def __init__(self, *args, **kwargs):
self.update(*args, **kwargs)
def __setitem__(self, key, value):
# translate java's camelcase name convention to python's
# underscore convention from:
# http://github.com/mw44118/pyohio-metaclasses-talk/blob/master/listing1.py#21
pythonic_key = ''
for c in key:
if c.isupper():
pythonic_key += '_%c' % c.lower()
else:
pythonic_key += c
super(underscore_dict, self).__setitem__(pythonic_key, value)
def update(self, *args, **kwargs):
if args:
if len(args) > 1:
raise TypeError("update expected at most 1 arguments, got %d" % len(args))
other = dict(args[0])
for key in other:
self[key] = other[key]
for key in kwargs:
self[key] = kwargs[key]
def setdefault(self, key, value=None):
if key not in self:
self[key] = value
return self[key]
import json
class JSONUnderscoreDecoder(json.JSONDecoder):
def __init__(self, *args, **kwargs):
json.JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs)
def object_hook(self, obj):
return underscore_dict(obj)