Is there any way to expand a python variable inside a backtick expression?

70 views
Skip to first unread message

Michael Torrie

unread,
Nov 5, 2019, 10:28:57 PM11/5/19
to xonsh
Recently I needed to grab a list of files in a directory that was in part defined by a python variable. I would have liked to do something like this:

mydir = 'somesubdir'
filelist = g`/path/to/@(mydir)/f*`

However I don't see a way of doing that at present, either with glob or regular expression back ticks.  Or did I miss something?  In the end I just used os.listdir which worked just as well for my purposes.  But I would think that placing variables into globs or regular expressions would be pretty common. I do it in BASH all the time.  For example, /path/to/some/dir/$BASHVAR/f*.txt

I had thought maybe I could use a custom search function, but that didn't quite work as I expected. I thought it would let me do a regular expression or glob search on the resulting list, but in reality it's more of just a special syntax for calling a python function. I'm not really sure what this special @customfunc`text` syntax is really intended for and how it's better than just calling @(customfunc('text')).

Gil Forsyth

unread,
Nov 6, 2019, 6:10:41 PM11/6/19
to Michael Torrie, xonsh
Hey Michael, 

So I don't think we do support expanding python variables inside of backticks -- and to be honest I'm kind of amazed it hasn't come up before.  Would you mind opening up an issue on github to track this?  I think this is something we should support.

Now, having said that, one existing method to do something like this is to make use of pf-strings -- it isn't quite as tidy as your desired syntax above, but this currently works:

mydir = "somesubdir"
filelist = pf"/path/to/{mydir}".glob("f*.txt")

Note that `filelist` above will be a generator with matching pathlib path objects.

Hope that helps!

- Gil

--
You received this message because you are subscribed to the Google Groups "xonsh" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xonsh+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xonsh/12cfefad-a2b5-46e4-99d9-72bc646fc69b%40googlegroups.com.

Michael Torrie

unread,
Nov 6, 2019, 9:31:17 PM11/6/19
to xonsh
Okay I can open a github issue to request this feature. I'm not really sure the best syntax for it.

Also the pf notation looks pretty good and would do the trick.  I like that it's more explicit. Might be better than adding variable expansion to regular expression and globs.
To unsubscribe from this group and stop receiving emails from it, send an email to xo...@googlegroups.com.

Mike Battaglia

unread,
Nov 6, 2019, 10:27:20 PM11/6/19
to Michael Torrie, xonsh
You can currently do this with $variables. As an example:

$mydir = 'somesubdir'
filelist = g`/path/to/$mydir/f*` 
 
Best,
Mike


To unsubscribe from this group and stop receiving emails from it, send an email to xonsh+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xonsh/dae02685-e95b-4d62-9b62-3f518889e85f%40googlegroups.com.

Michael Torrie

unread,
Nov 7, 2019, 10:22:18 AM11/7/19
to xonsh
Very true.  Using environment variables for this purposes is acceptable. That's how it would be done in traditional shell scripting languages.

Reply all
Reply to author
Forward
0 new messages