From fdbd58249858a6fa8cc4bfdd1a578b3ed70a2224 2011-06-20 23:40:24 From: MinRK Date: 2011-06-20 23:40:24 Subject: [PATCH] move rekey to jsonutil from parallel.util also make rekey accept basestring, not just str. closes gh-532 --- diff --git a/IPython/parallel/client/client.py b/IPython/parallel/client/client.py index 197036c..7dc5ca5 100644 --- a/IPython/parallel/client/client.py +++ b/IPython/parallel/client/client.py @@ -28,6 +28,7 @@ pjoin = os.path.join import zmq # from zmq.eventloop import ioloop, zmqstream +from IPython.utils.jsonutil import rekey from IPython.utils.path import get_ipython_dir from IPython.utils.traitlets import (HasTraits, Int, Instance, Unicode, Dict, List, Bool, Set) @@ -1247,7 +1248,7 @@ class Client(HasTraits): status = content.pop('status') if status != 'ok': raise self._unwrap_exception(content) - content = util.rekey(content) + content = rekey(content) if isinstance(targets, int): return content[targets] else: diff --git a/IPython/parallel/util.py b/IPython/parallel/util.py index 8d7ea8f..e366284 100644 --- a/IPython/parallel/util.py +++ b/IPython/parallel/util.py @@ -182,29 +182,6 @@ def disambiguate_url(url, location=None): return "%s://%s:%s"%(proto,ip,port) - -def rekey(dikt): - """Rekey a dict that has been forced to use str keys where there should be - ints by json. This belongs in the jsonutil added by fperez.""" - for k in dikt.iterkeys(): - if isinstance(k, str): - ik=fk=None - try: - ik = int(k) - except ValueError: - try: - fk = float(k) - except ValueError: - continue - if ik is not None: - nk = ik - else: - nk = fk - if nk in dikt: - raise KeyError("already have key %r"%nk) - dikt[nk] = dikt.pop(k) - return dikt - def serialize_object(obj, threshold=64e-6): """Serialize an object into a list of sendable buffers. diff --git a/IPython/utils/jsonutil.py b/IPython/utils/jsonutil.py index 344ff2c..ccae2ea 100644 --- a/IPython/utils/jsonutil.py +++ b/IPython/utils/jsonutil.py @@ -27,6 +27,29 @@ ISO8601_PAT=re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+$") # Classes and functions #----------------------------------------------------------------------------- +def rekey(dikt): + """Rekey a dict that has been forced to use str keys where there should be + ints by json.""" + for k in dikt.iterkeys(): + if isinstance(k, basestring): + ik=fk=None + try: + ik = int(k) + except ValueError: + try: + fk = float(k) + except ValueError: + continue + if ik is not None: + nk = ik + else: + nk = fk + if nk in dikt: + raise KeyError("already have key %r"%nk) + dikt[nk] = dikt.pop(k) + return dikt + + def extract_dates(obj): """extract ISO8601 dates from unpacked JSON""" if isinstance(obj, dict):