Hello everyone!
I am very glad to announce the first release of ppx_pyformat
!
ppx_pyformat
is a ppxlib based rewriter inspired by Python string format()
. This rewriter allows the user to do complex variable substitutions and value formatting. The format string syntax of ppx_pyformat
is closely modeled after that of Python3.10 (with several enhancements). Most Python format strings should be compatible with ppx_pyformat
with minor modifications. And the behavior is also very similar. So Python users should be able to quickly pick up this rewriter.
The rewriter takes a required format string following with optional input arguments. The rewriter will parse the format string, substitute/format the replacement fields and finally return everything as a single string
.
For more details about this package, please check the README.md.
Here is some sample code
let _ =
let hello = "Hello" in
let world = "world" in
print_endline [%pyformat "{hello} {world}!"]
(*Hello world!*)
let _ =
print_endline [%pyformat "pi = {Float.pi:.10f}"]
(*pi = 3.141592653*)
let _ =
let string_of_tuple (a, b) = "("^a^", "^b^")" in
print_endline [%pyformat "{!string_of_tuple}"; ("foo", "bar")]
(*(foo, bar)*)
let _ =
print_endline [%pyformat "the answer to life the universe and everything {0:#020_b}"; 42]
(*the answer to life the universe and everything 0b000_0000_0010_1010*)
Hope this plugin can make logging/debugging in OCaml a bit easier. Feel free to leave any feedback! Thanks.