PythonCall.jl icon indicating copy to clipboard operation
PythonCall.jl copied to clipboard

pystrings for easy global scope evaluation

Open andrew-saydjari opened this issue 2 years ago • 9 comments

A feature request to enable support for functionality mirroring PyCall's pystrings. It is very helpful when people are transitioning from python to Julia to be able to execute and grab the results of a few simple lines of code in global python scope. Currently, my hack at reproducing this behavior is below, but making this functionality easier to access (maybe just by making py"""...""" implement the following wrapping) would be appreciated.

In PyCall py""" from dustmaps.config import config config.reset() config['data_dir'] = '../../dustmaps/' import dustmaps.sfd dustmaps.sfd.fetch() """

Hack-y PythonCall: pyexec("from dustmaps.config import config",Main) pyexec("config.reset()",Main) pyexec("config['data_dir'] = '../../dustmaps/'",Main) pyexec("import dustmaps.sfd",Main) pyexec("dustmaps.sfd.fetch()",Main)

I tried getting something like this working with @pyeval begin ... end, but It was not obvious how to make the scope persist etc... So something to make this easy (or well documented if it exists) would be lovely.

andrew-saydjari avatar May 15 '23 05:05 andrew-saydjari

@pyexec always runs in a local scope, but you can write to global scope as usual by adding global dustmaps, config to the top of your example code.

cjdoris avatar May 15 '23 08:05 cjdoris

Sorry, I guess my main complaint was having to put pyexec() on every line. Is there a way of doing this with a simple begin ... end?

andrew-saydjari avatar May 15 '23 15:05 andrew-saydjari

I meant you can do

@pyexec """
global config, dustmaps
from dustmaps.config import config
config.reset()
config['data_dir'] = '../../dustmaps/'
import dustmaps.sfd
dustmaps.sfd.fetch()
"""

cjdoris avatar May 16 '23 15:05 cjdoris

Ok, if you would be amenable, I might PR some documentation showing this (and #290) to close them?

andrew-saydjari avatar May 17 '23 05:05 andrew-saydjari

Yeah PRs for improving the docs are always welcome.

cjdoris avatar May 17 '23 08:05 cjdoris

This issue has been marked as stale because it has been open for 60 days with no activity. If the issue is still relevant then please leave a comment, or else it will be closed in 7 days.

github-actions[bot] avatar Aug 19 '23 17:08 github-actions[bot]

This issue has been closed because it has been stale for 7 days. You can re-open it if it is still relevant.

github-actions[bot] avatar Aug 27 '23 01:08 github-actions[bot]

Sorry, I know that I let this get stale. It is because passing variables in this syntax seems to fail. A good example of a use case that is failing is below. It fails with error "invalid code."

@pyexec """

from dustmaps.sfd import SFDWebQuery

sfd = SFDWebQuery()

l = $glonm
b = $glatm

coords = SkyCoord(l*units.deg, b*units.deg, frame='galactic')
sfd_reddening = sfd(coords)

"""

andrew-saydjari avatar Dec 18 '23 01:12 andrew-saydjari

I want to comment, that I appreciate the fact that in PythonCall we can do the above in the nice syntax below. The solution is performant, easy to read, and lovely to use. My "issue" is mostly meant to help people trying to port over from PyCall to not have to rewrite their code blocks.

SFDWebQuery = pyimport("dustmaps.sfd" => "SFDWebQuery")
SkyCoord = pyimport("astropy.coordinates" => "SkyCoord")
units = pyimport("astropy" => "units")
sfd = SFDWebQuery()
coords = SkyCoord(pylist(glonm)*units.deg, pylist(glatm)*units.deg, frame="galactic")
sfd_red = pyconvert(Vector,sfd(coords))

andrew-saydjari avatar Dec 18 '23 01:12 andrew-saydjari