Show More
@@ -39,22 +39,18 b' datetime.strptime("1", "%d")' | |||
|
39 | 39 | def rekey(dikt): |
|
40 | 40 | """Rekey a dict that has been forced to use str keys where there should be |
|
41 | 41 | ints by json.""" |
|
42 | for k in dikt: | |
|
42 | for k in list(dikt): | |
|
43 | 43 | if isinstance(k, string_types): |
|
44 |
|
|
|
44 | nk = None | |
|
45 | 45 | try: |
|
46 |
|
|
|
46 | nk = int(k) | |
|
47 | 47 | except ValueError: |
|
48 | 48 | try: |
|
49 |
|
|
|
49 | nk = float(k) | |
|
50 | 50 | except ValueError: |
|
51 | 51 | continue |
|
52 | if ik is not None: | |
|
53 | nk = ik | |
|
54 | else: | |
|
55 | nk = fk | |
|
56 | 52 | if nk in dikt: |
|
57 | raise KeyError("already have key %r"%nk) | |
|
53 | raise KeyError("already have key %r" % nk) | |
|
58 | 54 | dikt[nk] = dikt.pop(k) |
|
59 | 55 | return dikt |
|
60 | 56 |
@@ -53,6 +53,13 b' def test():' | |||
|
53 | 53 | json.loads(json.dumps(out)) |
|
54 | 54 | |
|
55 | 55 | |
|
56 | def test_rekey(): | |
|
57 | # This could fail due to modifying the dict keys in-place on Python 3 | |
|
58 | d = { i:i for i in map(str, range(128)) } | |
|
59 | d = jsonutil.rekey(d) | |
|
60 | for key in d: | |
|
61 | nt.assert_is_instance(key, int) | |
|
62 | ||
|
56 | 63 | |
|
57 | 64 | def test_encode_images(): |
|
58 | 65 | # invalid data, but the header and footer are from real files |
General Comments 0
You need to be logged in to leave comments.
Login now