GithubHelp home page GithubHelp logo

ptzagk / inline-python Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fusion-engineering/inline-python

0.0 0.0 0.0 36 KB

Inline Python code directly in your Rust code

License: BSD 2-Clause "Simplified" License

Rust 100.00%

inline-python's Introduction

inline-python

Inline Python code directly in your Rust code.

Example

#![feature(proc_macro_hygiene)]
use inline_python::python;

fn main() {
    let who = "world";
    let n = 5;
    python! {
        for i in range('n):
            print(i, "Hello", 'who)
        print("Goodbye")
    }
}

How to use

Use the python!{..} macro to write Python code directly in your Rust code. You'll need to add #![feature(proc_macro_hygiene)], and use a nightly version of the compiler that supports this feature.

Using Rust variables

To reference Rust variables, use 'var, as shown in the example above. var needs to implement pyo3::ToPyObject.

Re-using a Python context

It is possible to create a Context object ahead of time and use it for running the Python code. The context can be re-used for multiple invocations to share global variables across macro calls.

let c = inline_python::Context::new();
python! {
  #![context = &c]
  foo = 5
}
python! {
  #![context = &c]
  assert foo == 5
}

Getting information back

A Context object could also be used to pass information back to Rust, as you can retrieve the global Python variables from the context through Context::get_global.

Syntax issues

Since the Rust tokenizer will tokenize the Python code, some valid Python code is rejected. The two main things to remember are:

  • Use double quoted strings ("") instead of single quoted strings ('').

    (Single quoted strings only work if they contain a single character, since in Rust, 'a' is a character literal.)

  • Use //-comments instead of #-comments.

    (If you use # comments, the Rust tokenizer will try to tokenize your comment, and complain if your comment doesn't tokenize properly.)

Other minor things that don't work are:

  • Certain escape codes in string literals. (Specifically: \a, \b, \f, \v, \N{..}, \123 (octal escape codes), \u, and \U.)

    These, however, are accepted just fine: \\, \n, \t, \r, \xAB (hex escape codes), and \0

  • Raw string literals with escaped double quotes. (E.g. r"...\"...".)

  • Triple-quoted byte- and raw-strings with content that would not be valid as a regular string. And the same for raw-byte and raw-format strings. (E.g. b"""\xFF""", r"""\z""", fr"\z", br"\xFF".)

  • The // and //= operators are unusable, as they start a comment.

    Workaround: you can write ## instead, which is automatically converted to //.

Everything else should work fine.

License: BSD-2-Clause

inline-python's People

Contributors

de-vri-es avatar m-ou-se avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.