pystrings for easy global scope evaluation
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.
@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.
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?
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()
"""
Ok, if you would be amenable, I might PR some documentation showing this (and #290) to close them?
Yeah PRs for improving the docs are always welcome.
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.
This issue has been closed because it has been stale for 7 days. You can re-open it if it is still relevant.
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)
"""
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))