Code Monkey home page Code Monkey logo

Comments (10)

zendevil avatar zendevil commented on July 19, 2024 1

How about

(py/get-slice my-array [py/... [1 nil] [2 nil]])

where py/... acts like the builtins.Ellipsis or ... object.

In Python, that would be equivalent to:

my_array[..., 1:, 2:]

If I could, I'd just have the following:

(py[] my-array [py/... [1 nil] [2 nil]])

But I don't know if py[] is a valid function or macro name. I don't think clojure allows that name. My Spacemacs config automatically puts a space in between the py and the [], so probably get-slice is okay.

from libpython-clj.

zendevil avatar zendevil commented on July 19, 2024

There aren't any docs on np slicing and indexing syntax. Would you please share how you wrote this example in libpython-clj?

from libpython-clj.

cnuernber avatar cnuernber commented on July 19, 2024

@zendevil - Now there is one very short document:

https://github.com/clj-python/libpython-clj/blob/master/docs/slicing.md

from libpython-clj.

zendevil avatar zendevil commented on July 19, 2024

There ought to be a concise way of indexing lists and numpy arrays. So far I haven't found any.

from libpython-clj.

jjtolton avatar jjtolton commented on July 19, 2024

from libpython-clj.

zendevil avatar zendevil commented on July 19, 2024

How about

(py/get-item ary [(builtins/slice 1 nil) (builtins/slice 1 nil)])
;; becomes

(py/get-item ary [[1 nil] [1 nil]])

Slicing is very common in python, and my guess is that most people don't know that it works with builtins.slice behind the scenes. We can keep it that way in this great library.
The slicing functionality can be built into py/get-item or py/get-slice can be added. If this functionality had already existed, you'd not want to go back to not having it, so how about adding it?
We can always have an experimental branch with things like these and the kebab case that I'd re-ignited interest in a few weeks ago.
Another pain point is having to do:

(do
(require-python 'x)
(require-python '[y :as foo])
(require-python 'z)
)

It would be better if we could do:

(require-python 'x '[y :as foo] 'z)

Or something along those lines, although I admit that talking is way easier than implementing all this.

from libpython-clj.

cnuernber avatar cnuernber commented on July 19, 2024

I agree that better support of slicing would be very cool. Potentially this would just a be a macro that took input that looked exactly like the python slice syntax and then produced a vector of slice objects.

from libpython-clj.

sogaiu avatar sogaiu commented on July 19, 2024

Regarding the legailty of py[] as a symbol name, I believe the claim that it is not valid is correct -- at least it's not likely to work in a straight-forward way for use with def, defn, and friends.

Accorrding to: https://clojure.org/reference/reader#_symbols:

Symbols begin with a non-numeric character and can contain alphanumeric characters and *, +, !, -, _, ', ?, <, > and = (other characters may be allowed eventually).

Although these docs can be slightly off in some cases, I think they are correct in this case.

Also a repl started via clj:

user=> (def py[] nil)
Syntax error compiling def at (REPL:1:1).
Too many arguments to def

def doesn't succeed, and:

user=> (defn py[] [] nil)
#'user/py

defn ends up defining a function with a different name.

On a side note, there is this:

user=> (symbol "py[]")
py[]
user=> (type (symbol "py[]"))
clojure.lang.Symbol

But it's probably better to pretend we didn't see that :)

from libpython-clj.

jjtolton avatar jjtolton commented on July 19, 2024

But it's probably better to pretend we didn't see that :)
🤣

from libpython-clj.

jjtolton avatar jjtolton commented on July 19, 2024

Are we sure a recipe wouldn't be better? Otherwise I suppose it could go in the sugar section.

(defn get-slice [x & args]
  (letfn [(slice
            ([] (python/slice nil nil))
            ([x] (if (vector? x)
                   (apply python/slice x)
                   x))
            ([x y & xs]
             (let [xs (into [x y] xs)]
               (if (every? int? xs)
                 (apply python/slice xs)
                 (mapv slice xs)))))]
    (py/get-item x (apply slice args))))

(get-slice (python/list [1 2 3]) 1 2) 
   => [2]
(require-python '[numpy :as np])

(get-slice (np/array [[1 2] [2 3] [3 4] [4 5] [6 5]]) [1 2] python/Ellipsis)
  => [[2 3]]

(get-slice (np/array [[1 2] [2 3] [3 4] [4 5] [6 5]]) [1 2] [1])
  => [[2]]

I don't use numpy that often so I don't know if this is super useful but it seems to work syntactically.

from libpython-clj.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.