##// END OF EJS Templates
don’t modify dict keys while iterating through them...
MinRK -
Show More
@@ -47,22 +47,18 b' datetime.strptime("1", "%d")'
47 def rekey(dikt):
47 def rekey(dikt):
48 """Rekey a dict that has been forced to use str keys where there should be
48 """Rekey a dict that has been forced to use str keys where there should be
49 ints by json."""
49 ints by json."""
50 for k in dikt:
50 for k in list(dikt):
51 if isinstance(k, string_types):
51 if isinstance(k, string_types):
52 ik=fk=None
52 nk = None
53 try:
53 try:
54 ik = int(k)
54 nk = int(k)
55 except ValueError:
55 except ValueError:
56 try:
56 try:
57 fk = float(k)
57 nk = float(k)
58 except ValueError:
58 except ValueError:
59 continue
59 continue
60 if ik is not None:
61 nk = ik
62 else:
63 nk = fk
64 if nk in dikt:
60 if nk in dikt:
65 raise KeyError("already have key %r"%nk)
61 raise KeyError("already have key %r" % nk)
66 dikt[nk] = dikt.pop(k)
62 dikt[nk] = dikt.pop(k)
67 return dikt
63 return dikt
68
64
General Comments 0
You need to be logged in to leave comments. Login now