New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clean nan/inf in json_clean #2194
Conversation
these values are not json-compliant
INF = float('inf') | ||
NINF = float('-inf') | ||
INFS = (INF, NINF) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth defining all of these if we'll only use INFS in the end?
Can we add datetime objects to the list of things to clean? I believe json not handling python datetime objects is the sole reason for these lines that cut out some datetime objects from messages: https://github.com/ipython/ipython/blob/master/IPython/frontend/html/notebook/handlers.py#L381 @ellisonbg: am I correct in assuming that the reason for popping those dates is because of JSONing limitations? Update: never mind; I guess I didn't see the rest of the file. There already are functions there to clean dates. I'm curious why we don't just embed these sorts of things in our own json dumps/loads functions, so you can just call the custom dumps function and it will automatically take care of things like encoding for inf, encoding dates, etc. Python lets you define custom encoders and decoders: http://docs.python.org/library/json.html |
Yes. On Mon, Jul 23, 2012 at 7:55 PM, Jason Grout
Brian E. Granger |
Why would we ever pop date time objects? We already handle them just fine by using default=date_default (all messages have timestamps in headers). Handling them in json_clean would be redundant. |
So can we just make a custom JSON encoding class that does both the effects of json_clean and default=date_default automatically? Then it's as easy as calling our JSON encoding function with no arguments. |
(see http://stackoverflow.com/questions/455580/json-datetime-between-python-and-javascript for some more ideas how to do datetimes. Especially the suggestion there to use |
Hm, I wonder how I missed One advantage of decoupling cleaning / datetime-formatting: If a message gets to the Session object that is not JSON-serializable, that is a bug and should raise. Most datetime objects are actually created inside the Session object as the timestamp in every header. |
Clean nan/inf in json_clean. The floating point values NaN and Infinity are not part of the JSON specification and causes some parsers to throw errors. Since our usage is only for things like the display of function defaults, we can use a basic string representation ('NaN', 'inf', etc) instead.
I just committed this NaN/Inf JSON fix. If we want to pursue a custom JSON encoding class that can be done in another issue. (I have NOT opened another issue to track that idea. Perhaps @jasongrout can do it?) |
these values are not json-compliant closes #2187
* commit 'rel-0.13-33-gcfc5692': (33 commits) Backport PR ipython#2347: adjust division error message checking to account for Python 3 Backport PR ipython#2305: RemoteError._render_traceback_ calls self.render_traceback Backport PR ipython#2280: fix SSH passwordless check for OpenSSH Backport PR ipython#2270: SSHLauncher tweaks Backport PR ipython#2261: Fix: longest_substr([]) -> '' Backport PR ipython#2250: fix html in notebook example Backport PR ipython#2235: remove spurious print statement from setupbase.py fixup Backport PR ipython#2223: Custom tracebacks Backport PR ipython#2214: use KernelApp.exec_lines/files in IPEngineApp Backport PR ipython#2212: catch errors in markdown javascript Backport PR ipython#2194: clean nan/inf in json_clean Backport PR ipython#2177: remove numpy install from travis/tox scripts Backport PR ipython#2169: ipdb: pdef, pdoc, pinfo magics all broken Backport PR ipython#2186: removed references to h5py dependence in octave magic documentation Backport PR ipython#2185: added test for %store, fixed storemagic Backport PR ipython#2170: Fix tab completion with IPython.embed_kernel(). Backport PR ipython#2163: fix 'remote_profie_dir' typo in SSH launchers Backport PR ipython#2117: use explicit url in notebook example Backport PR ipython#2126: ipcluster broken with any batch (PBS/LSF/SGE) ...
Clean nan/inf in json_clean. The floating point values NaN and Infinity are not part of the JSON specification and causes some parsers to throw errors. Since our usage is only for things like the display of function defaults, we can use a basic string representation ('NaN', 'inf', etc) instead.
these values are not json-compliant
closes #2187