FormatInterp.jl: Python-like formatted interpolation

Several months ago I asked here (Formatted string interpolation) about string interpolation with formatting options, and didn’t find any solutions. So I developed a small package for this, which uses Python f-string syntax, e.g. f"result = {1.23^5:.2f}" gives "result = 2.82". I’ve been using this package myself for quite a while already and didn’t have any issues - so decided to make it available: https://github.com/aplavin/FormatInterp.jl. I really don’t get how one can live (and generate strings) without such a thing :slight_smile:
Currently it has a single string macro, which parses a given string and generates an expression which uses Formatting.jl to format individual values, and concatenates them afterwards.

Bug reports are welcome! As for adding something new - I have no plans on that, but am open to PRs.

6 Likes

A README.md file would be nice.

3 Likes

I’m sure people with a python background will appreciate your macro as it is, but would you consider extending it to also allow a more julian syntax for the rest of us? Something like this:

f"result = $(1.23^5:.2f)"

For me at least that would definitely be worth typing an extra character.

2 Likes

I personally hate using $ as the interpolation character, because it complicates generating latex strings (which I do quite often) more than { does. However, I’m open to PRs in this regard. Changing { to some other single character is almost trivial, but replacing it with a pair like $( requires some care.

1 Like

A trivial comment, but the package name doesn’t really conform to the package naming guidelines (see especially #4)

Why not FormattedInterpolation.jl or FStringInterpolation.jl?

3 Likes

A related package is StrLiterals.jl. For string-related issues, I recommend to watch JuliaString and @ScottPJones.

1 Like

Oh, indeed. I didn’t notice that section in the docs.

1 Like

Yes, I know about StrLiterals, but it’s not maintained at all - last commit and activity was 1.5 years ago, and now this package doesn’t work on reasonably recent Julia. Also StrLiterals doesn’t have python-like string syntax as I understand (cannot try because it doesn’t work). So for me it was easier to just roll out my own package to parse the input string and pass its parts to Formatting.jl than to fix and modify StrLiterals.

StrLiterals works for me on julia 1.2 (passes all tests) and StrFormat supports python-like string syntax. I don’t think these packages are unmaintained. What is not working for you?

Ok, I stand corrected - the package itself works. And even formatting of expressions works after installing StrFormat.jl. I confused StrLiterals and https://github.com/JuliaString/StringLiterals.jl thinking they are the same package - and it is the latter one that does not work.
So, taking this into account, StrLiterals + StrFormat have only one disadvantage I can see - what these packages call “python-like” syntax is not the syntax python or any other language actually uses, and this is done for no apparent reason.

So for me it was easier to just roll out my own package…

That is totally fine, and I did not want to disparage your work. The lack of format modifiers in Base julia string interpolation is an eternal gripe of mine, and I thought that you might not have been aware of the JuliaStrings “shadow ecosystem” for strings. This group of packages seems to be less well known than deserved.

Even when rolling my own, it is always nice to have working code and API design for related functionality to learn from.

StringLiterals.jl was written back for Julia v0.4
StrLiterals.jl is part of the whole set of string related packages (you can just do using Strs to get pretty much everything), and it most definitely is maintained for Julia v1.0 and above

I am not a python programmer, and most of the python-like formatting came from the Format.jl package I used under the hood.
I documented in the README that I wanted to have it allow a truly python like syntax, however I only use the C printf style myself.
If you wish to contribute to make it actually work more closely to the Python syntax (where the argument is part of the string, not positional), I would greatly appreciate it.

1 Like

Yes, now I see. It was just really confusing for me that there are two packages with almost exactly the same name (StrLiterals vs StringLiterals), and I didn’t notice they are in fact different.

I also use Formatting.jl (almost the same as Format.jl, as I understand), my package just parses the string and generates a call to Formatting.jl for each interpolated argument.