7

Say that I evaluate the following Python code:

import inspect
import scipy.ndimage.filters

list (inspect.getargspec (scipy.ndimage.filters.gaussian_filter1d))
# => [['input', 'sigma', 'axis', 'order', 'output', 'mode', 'cval'],
#      None, None, (-1, 0, None, 'reflect', 0.0)]

I'd like to transform the output string, which is simply Python's print output, to an Elisp data structure. So I need a function foo that does this:

(foo "[['input', 'sigma', 'axis', 'order', 'output', 'mode', 'cval'], None, None, (-1, 0, None, 'reflect', 0.0)]")
;; =>
;; (("input"
;;   "sigma"
;;   "axis"
;;   "order"
;;   "output"
;;   "mode"
;;   "cval")
;;  nil
;;  nil
;;  (-1 0 nil "reflect" 0.0))

While it's not hard to write a version of foo that works for this specific instance, I wonder if there's a library that does this in a generic way, i.e. it is able to handle any complex Python structure.

8

Use JSON, not Python syntax

What you are looking for is json.el which is a part of Emacs.

Note that it will not read Python format OOTB; there are at least 3 problems:

  1. 'foo' is not recognized as a string
  2. keywords true/false/null are recognized instead of True/False/None
  3. tuples (1,"bar") are not recognized

Full solution using json.dumps in Python

Python can easily print its data structures as JSON:

(require 'json)
(require 'python)
(json-read-from-string
 (substring
  (python-shell-send-string-no-output
   "import inspect, json; json.dumps (inspect.getargspec (json.dumps))"
   (run-python))
  1 -1))
==>
[["obj" "skipkeys" "ensure_ascii" "check_circular" "allow_nan"
  "cls" "indent" "separators" "encoding" "default" "sort_keys"]
 nil "kw" [:json-false t t t nil nil nil "utf-8" nil :json-false]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.