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

keyword lookup on NamedTuple #202

Closed
shaunlebron opened this issue Apr 22, 2022 · 8 comments
Closed

keyword lookup on NamedTuple #202

shaunlebron opened this issue Apr 22, 2022 · 8 comments

Comments

@shaunlebron
Copy link
Contributor

shaunlebron commented Apr 22, 2022

I believe a NamedTuple in python supports two types of lookups: attribute name or index.

Right now, I have to use (py.- nt :foo) whereas (:foo nt) does not work and returns nil. I believe this is because the jvm bridge is not able to treat a namedtuple as both a dict and a tuple. Is this possible?

Thank you!

(I deleted a followup comment about (py. nt _asdict) not working, but it was my error in not requiring "py.")

@jjtolton
Copy link
Contributor

jjtolton commented Apr 22, 2022 via email

@jjtolton
Copy link
Contributor

jjtolton commented Apr 22, 2022 via email

@shaunlebron
Copy link
Contributor Author

Thanks. Is there a way to make keywords polymorphic to getattr from the user side?

@jjtolton
Copy link
Contributor

jjtolton commented Apr 22, 2022 via email

@jjtolton
Copy link
Contributor

jjtolton commented Apr 22, 2022

Do you mean extending libpython-clj itself? We do have hooks into the
datafy/nav paradigm, which would be one option. Checkout the code here:
https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/require.clj#L352

You could probably do something like:

(defmethod pydafy 'builtins.NamedTuple [nt] 
;; code to convert named tuple -> clojure, possibly in conjunction 
 ;; with a Clojure NamedTuple protocol that extends `get` 
... ) 

note that once you've extended with pydafy,
you can then use standard Clojure datafy function. This would result in
code that might look like, at a low level,

(:foo (datafy nt))

There are lower level hooks available for performant marshalling of
data types from Python, but that would be a broader feature request
discussion.

@shaunlebron
Copy link
Contributor Author

Thanks so much for the example, I was not familiar with datafy. Glad to know this.

And just to share here, I missed the following distinction in Python (since they’re both equivalent in JS):

  • __getitem__ is for foo['bar'] = foo.get('bar')
  • __getattr__ is for foo.bar = getattr(foo, 'bar')

So I understand your original response now, that keywords map to getitem, whereas py.- is to be used for getattr. I’ll just use it that way to not hide what's happening in python. Thank you!

@jjtolton
Copy link
Contributor

jjtolton commented Apr 22, 2022 via email

@shaunlebron
Copy link
Contributor Author

Thanks. I don't really know what I'm doing yet, but I can't believe this project exists AND works flawlessly so far. Thanks for all your work 🙏

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