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

cannot get key from dictionary #183

Open
behrica opened this issue Nov 13, 2021 · 4 comments
Open

cannot get key from dictionary #183

behrica opened this issue Nov 13, 2021 · 4 comments

Comments

@behrica
Copy link
Contributor

behrica commented Nov 13, 2021

The below code seems to be inconsistent.
Converting a Clojure map to a python dictionary shows presence of key "a".
But then I can not get the key "a" out of it.

(->  {:a 1}
     py/as-python
     (py/py. keys)
     seq)
;; => ("a")

(->  {:a 1}
     py/as-python
     (py/py. get "a")
     seq)
;; => nil
@cnuernber
Copy link
Collaborator

Hmm. as-python here means the dictionary is bridged and you used a keyword argument. There is no way to pass a keyword through python and back to clojure,however, so when querying it fails. Keywords are marshalled to strings when passing back to python so keys works.

I think the issue is don't use java-specific types as keys when using bridged dictionaries. I don't see a great way around this.

@behrica
Copy link
Contributor Author

behrica commented Dec 13, 2021

Maybe writing the code like this, shows the issue better:

(def py-dict (py/as-python {:a 1}))
(seq (py/py. py-dict keys))
-> ("a")
(py/py. py-dict get "a")
->nil

I am able to create a "dictionary", which

  • seems to have a key "a" (as told by keys function)
  • but asking it for "a" does not work

This seems to break the contract of a dictionary.

and exactly the same code, does work, if I give a string initially:

(def py-dict (py/as-python {"a" 1}))
(seq (py/py. py-dict keys))
-> ("a")
(py/py. py-dict get "a")
->1

In my view, the object created by (py/as-python {:a 1}) is invalid, as it does not behave like a dictionary should behave. (so maybe the as-python call should fail getting keywords)

@behrica
Copy link
Contributor Author

behrica commented Dec 13, 2021

The created python object behaves very strange...

(def py-dict (py/as-python {:a 1}))
(seq (py/py. py-dict items))
(('a', None))    ;; value of "a" is None ....
(seq (py/py. py-dict values))    
(1)     ;; list of values is: (1)

I am not expecting to get a python-object having a "keyword", as keyword are not supported in python.
But it seems that I do not get a dictionar of:

dict(a=1)

neither.

@cnuernber
Copy link
Collaborator

That is a good point and definitely one issue here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants