Skip to content
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

Merged
merged 2 commits into from Jul 27, 2012
Merged

clean nan/inf in json_clean #2194

merged 2 commits into from Jul 27, 2012

Conversation

minrk
Copy link
Member

@minrk minrk commented Jul 23, 2012

these values are not json-compliant

closes #2187

these values are not json-compliant
INF = float('inf')
NINF = float('-inf')
INFS = (INF, NINF)

Copy link
Contributor

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?

@jasongrout
Copy link
Member

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

@ellisonbg
Copy link
Member

Yes.

On Mon, Jul 23, 2012 at 7:55 PM, Jason Grout
reply@reply.github.com
wrote:

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?


Reply to this email directly or view it on GitHub:
#2194 (comment)

Brian E. Granger
Cal Poly State University, San Luis Obispo
bgranger@calpoly.edu and ellisonbg@gmail.com

@minrk
Copy link
Member Author

minrk commented Jul 24, 2012

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.

@jasongrout
Copy link
Member

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.

@jasongrout
Copy link
Member

(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 .isoformat() instead of doing your own iso formatting.)

@minrk
Copy link
Member Author

minrk commented Jul 24, 2012

Hm, I wonder how I missed .isoformat(). Other than that, I don't see anything preferable in the SO question.

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.

bfroehle added a commit that referenced this pull request Jul 27, 2012
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.
@bfroehle bfroehle merged commit 157d99a into ipython:master Jul 27, 2012
@bfroehle
Copy link
Contributor

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?)

minrk added a commit that referenced this pull request Jul 28, 2012
these values are not json-compliant

closes #2187
@minrk minrk deleted the clean_nan branch March 31, 2014 23:36
yarikoptic added a commit to yarikoptic/ipython that referenced this pull request May 2, 2014
* 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)
  ...
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NaN in object_info_reply JSON causes parse error
4 participants