Skip to content

Instantly share code, notes, and snippets.

@knmkr
Last active December 10, 2015 11:28
Show Gist options
  • Save knmkr/4427440 to your computer and use it in GitHub Desktop.
Save knmkr/4427440 to your computer and use it in GitHub Desktop.
elisp / Quote selected region as python-dict.
(eval-when-compile (require 'cl))
(defun quote-as-dict ()
"Quote selected region as python-dict.
a t g c
-> {'a': 't', 'g': 'c'}
"
(interactive)
;; cut region as `seq`
(setq seq (buffer-substring (region-beginning) (region-end)))
(delete-region (region-beginning) (region-end))
;; split `seq` as `subseqs`
(setq subseqs (split-string seq))
;; print quoted subseqs as python-dict: {'a': 't', 'g': 'c'}
(insert-string "{")
(let ((i 0))
(setq n (length subseqs))
(dolist (subseq subseqs)
(insert-string (format "'%s'" subseq))
(incf i)
(cond ((equal (mod i 2) 1)
(insert-string ": "))
(t
(if (not (equal i n))
(insert-string ", "))))
)
)
(insert-string "}")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment