Skip to content

Commit

Permalink
Translate elisp hash to python dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
813gan committed Aug 26, 2024
1 parent 8ed35c5 commit ba04df4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
10 changes: 10 additions & 0 deletions emacspy.el
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@

;;; Code:

(eval-when-compile (require 'subr-x))

(defun emacspy--hash-table-to-lists (hash)
"Utility function that convert `HASH' into (list keys values)."
(let ((keys (hash-table-keys hash))
(values nil))
(setq values
(mapcar (lambda (key) (gethash key hash)) keys))
(list keys values) ))

(require 'emacspy-module)

;;; emacspy.el ends here
15 changes: 15 additions & 0 deletions emacspy.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ cdef class EmacsValue:
break
return ret

def as_dict(self): # aka hash table
cdef emacs_env* env = get_env()
hash_table_to_lists = unwrap(sym("emacspy--hash-table-to-lists"))
is_hash = unwrap(sym("hash-table-p"))
cdef emacs_value hash_src = self.v

if not EmacsValue.wrap(env.funcall(env, is_hash, 1, &hash_src)).to_python_type():
raise ValueError("Attepted hash translation on non hash")

keys, values = \
EmacsValue.wrap(env.funcall(env, hash_table_to_lists, 1, &hash_src)).to_python_type()
return dict(zip(keys, values))

def to_python_type(self):
my_type = self.type()
if my_type == "string":
Expand All @@ -207,6 +220,8 @@ cdef class EmacsValue:
return True
elif my_type == "cons": # list
return self.as_list()
elif my_type == "hash-table":
return self.as_dict()
raise ValueError("Unable to export emacs value")

def __str__(self):
Expand Down
20 changes: 19 additions & 1 deletion tests/test.el
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
:type 'python-exception)
(should (py-run-string "test" "ospath.realpath('/')" "run_string_test_var"))
(should (string= "/" (py-get-global-variable "test" "run_string_test_var")))
)
(should (py-run-string "test" "1==1"))
(should-not (py-run-string "test" "1==2")) )

(ert-deftest ert-test-emacspy-data-bool ()
(should (eq 't (py-run-string "test" "True")))
Expand Down Expand Up @@ -147,3 +148,20 @@
(should (listp nested-lst))
(should (eq 3 (length nested-lst)))
(should (eq 3 (nth 2 nested-lst))) ) ))

(ert-deftest ert-test-emacspy-data-hash ()
(should (functionp 'emacspy--hash-table-to-lists))
(let ((hash (make-hash-table))
(nhash (make-hash-table)))
(puthash 1 "test" hash)
(puthash 2 nil hash)
(puthash "list" '(1) hash)

(puthash "key" 1 nhash)
(puthash "hash" nhash hash)

(should (py-set-global "test" hash "test_hash"))
(should (py-run-string "test" "test_hash[1]=='test'"))
(should (py-run-string "test" "test_hash[2]==False"))
(should (py-run-string "test" "test_hash['list'][0]==1"))
(should (py-run-string "test" "test_hash['hash']['key']==1")) ))

0 comments on commit ba04df4

Please sign in to comment.