clj-python / libpython-clj

Python bindings for Clojure
Eclipse Public License 2.0
1.05k stars 68 forks source link

att-type-map vs. dir #215

Closed behrica closed 1 year ago

behrica commented 1 year ago

The att-type-map function is not there anymore. We have now the "dir", but it returns less information. (only names, not types)

Is there a way to get the below list as well ?

user> (att-type-map ones-ary)
{"T" :ndarray,
 "__abs__" :method-wrapper,
 "__add__" :method-wrapper,
 "__and__" :method-wrapper,
 "__array__" :builtin-function-or-method,
 "__array_finalize__" :none-type,
 "__array_function__" :builtin-function-or-method,
 "__array_interface__" :dict,
 "__array_prepare__" :builtin-function-or-method,
 "__array_priority__" :float,
jjtolton commented 1 year ago

In python, the vars function accomplishes this. During exploratory development I will often do

(import-python)
(python/vars some-py-obj)
jjtolton commented 1 year ago

To be fair it does not do the types, it's a KV map of names to values (not types), though you could valmap with python/type

behrica commented 1 year ago

Hmm, not sure I undertsnad the usage of python/vars

(import-python)
(def np (py/import-module "numpy"))
ser=> (-> (py/call-attr np "ones" [2 3]) (python/vars))
Execution error at libpython-clj2.python.ffi/check-error-throw (ffi.clj:708).
TypeError: vars() argument must have __dict__ attribute
jjtolton commented 1 year ago

In this case you would have to

(python/vars np/ndarray)

How do I know this?

In [1]: import numpy as np                                                                              

In [2]: np.ones([2, 3])                                                                                 
Out[2]:                                                                                                 
array([[1., 1., 1.],                                                                                    
       [1., 1., 1.]])                                                                                   

In [3]: a = np.ones([2, 3])                                                                             

In [4]: vars(a)                                                                                         
---------------------------------------------------------------------------                             
TypeError                                 Traceback (most recent call last)                             
<ipython-input-4-ed1385268fe9> in <module>                                                              
----> 1 vars(a)                                                                                         

TypeError: vars() argument must have __dict__ attribute                                                 

In [5]: type(a)                                                                                         
Out[5]: numpy.ndarray                                                                                   

In [6]: vars(np.ndarray)                                                                                
Out[6]:                                                                                                 
mappingproxy({'__repr__': <slot wrapper '__repr__' of 'numpy.ndarray' objects>,                         
              '__str__': <slot wrapper '__str__' of 'numpy.ndarray' objects>,       
...snip...
behrica commented 1 year ago

Yes, I see. I can get the att-amp from something like:

(-> (py/call-attr np "ones" [2 3]) (python/type) (python/vars)  ) )

Thanks.