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

Proposal: from where import name as custom-name #1

Open
qzivli opened this issue Dec 13, 2018 · 2 comments
Open

Proposal: from where import name as custom-name #1

qzivli opened this issue Dec 13, 2018 · 2 comments

Comments

@qzivli
Copy link

qzivli commented Dec 13, 2018

https://github.com/guenchi/fli/blob/bee3468b366665067cc947609450208d4a42f0e2/pycall.sc#L87-L90

I tested, it woks. This is example code:

(define-syntax py:from
  (syntax-rules (import as)
    [(_ where import name)
     (py:run-simple-string (format "from ~a import ~a" where name))]
    [(_ where import name as custom-name)
     (py:run-simple-string (format "from ~a import ~a as ~a" where name custom-name))]))

(py:from "requests" import "get" as "GET")
(py:run-simple-string "print(type(GET))")

@guenchi
Copy link
Owner

guenchi commented Dec 16, 2018

use this instead of that:

(define np (py/import-import-module "numpy"))
(define array (py/object-get-attr-string np "array"))

@guenchi
Copy link
Owner

guenchi commented Dec 20, 2018

Look this:

(define pycall
    (lambda (lst)
        (define Sym
            (lambda (x)
                (match x
                    (,s (guard (symbol? s)) s))))
        (match lst
            ((import ,(Sym -> lib)) `(define ,lib (py/import-import-module (symbol->string ,lib))))
            ((import ,(Sym -> lib) as ,(Sym -> l)) `(define ,l (py/import-import-module (symbol->string ,lib)))))))

so you can use:

(pycall '(import numpy))
or
(pycall '(import numpy as np))

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