Please disable your adblock and script blockers to view this page

Writing Python inside your Rust code ? Part 1


Rust
Python

needed:(See
PyErr
bc).So
derive(Stuff
TokenStream
Time
Group
zero’


Python
We’ll
the Python C API

123’
effort’
Whelp
Span
Fingers
Mara Bos

No matching tags

No matching tags

No matching tags


details.)Macros
macros’
Nice

No matching tags

Positivity     48.00%   
   Negativity   52.00%
The New York Times
SOURCE: https://blog.m-ou.se/writing-python-inside-rust-1/
Write a review: Hacker News
Summary

It even allows you to use your Rust variables inside the Python code.We’ll start with a much simpler case, and slowly work our way up to this result (and more!).First, let’s take a look at how we can run Python code from Rust. Macros allow us to allow custom syntax within Rust, so let’s try to use one:Macros are normally defined using using macro_rules!, which lets you define a macro using advanced ‘find and replace’ rules based on things like tokens and expressions. Since ‘valid Python code’ is not an option, we’ll just make our macro accept anything: raw tokens, as many as needed:(See the resources linked above for details on how macro_rules! called stringify!.Let’s try!Success!But wait, what happens if we have more than one line of Python code?Oof, that’s unfortunate.To debug this, let’s properly print the PyErr, and also show the exact Python code we’re feeding to Python::run:Apparently both lines of Python code ended up on the same line, Maybe a procedural macro gives us more flexibility to hack our way around any problems.Because procedural macros run Rust code as part of the compilation process, which is compiled before you can compile anything that uses it.In python-macro/Cargo.toml:In Cargo.toml:Let’s start with an implementation that just panics (todo!()), Tokens inside parentheses will be children of a single Group token.Let’s modify our procedural macro to recursively go over all the tokens inside groups as well (and improve the output a bit):Wonderful!Now to reconstruct the white-space, Otherwise Python will complain about invalid indentation.Let’s subtract the column number of the first token from all column numbers:Awesome!Now we only have to turn this string into a string literal token macro from the quote crate:Okay, that’s better.Now to test it using our original run_python function:Success!🎉Now to turn this into a reusable library, we:There’s probably tons of things to improve and plenty of bugs to discover, but at least we can now run snippets of Python inbetween our lines of Rust code.The biggest problem for now is that this isn’t very useful yet,

As said here by Mara Bos