Slide 1

Slide 1 text

Boosting Python with Rust (TM)

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Python Mauritius Usergroup site fb linkedin mailing list 4

Slide 5

Slide 5 text

url pymug.com site 5

Slide 6

Slide 6 text

About me compileralchemy.com 6

Slide 7

Slide 7 text

slides 7

Slide 8

Slide 8 text

Boosting Python with Rust (TM) 8

Slide 9

Slide 9 text

My relationship with Rust 9

Slide 10

Slide 10 text

Read the Rust book twice Read the Google rust training guide Know only about releasing scope and returning ownership Still don't know 3/4 of rust 10

Slide 11

Slide 11 text

11

Slide 12

Slide 12 text

Python / Rust friendliness 12

Slide 13

Slide 13 text

convention_of_methods readability emulates english 13

Slide 14

Slide 14 text

uses toml as default - toml invented by pypa core dev 14

Slide 15

Slide 15 text

build tool in py 15

Slide 16

Slide 16 text

iterators >>> x = itertools.cycle([1,2,3]) >>> next(x) 1 >>> next(x) 2 >>> next(x) 3 >>> dir([1]) [..., '__iter__', ...] 16

Slide 17

Slide 17 text

let nums = vec![1, 2, 3]; for num in nums.iter() { println!("{}", num); } loop { match range.next() { Some(x) => { println!("{}", x); }, None => { break } } } 17

Slide 18

Slide 18 text

consumers py: yield rust: ex .collect 18

Slide 19

Slide 19 text

string types b' ' b' ' 19

Slide 20

Slide 20 text

for in loop 20

Slide 21

Slide 21 text

for i, item in enumerate(bytes): if item == b' ': return i for (i, &item) in bytes.iter().enumerate() { if item == b' ' { return i; } } 21

Slide 22

Slide 22 text

Methods len(nums) nums.len() 22

Slide 23

Slide 23 text

-> return type def x() -> None: ... fn area(&self) -> u32 { self.width * self.height } 23

Slide 24

Slide 24 text

Pattern matching 24

Slide 25

Slide 25 text

def where_is(point): match point: case Point(x=0, y=0): print("Origin") case Point(): print("Somewhere else") case _: print("Not a point") fn value_in_cents(coin: Coin) -> u8 { match coin { Coin::Penny => { println!("Lucky penny!"); 1 } Coin::Nickel => 5, Coin::Dime => 10, Coin::Quarter => 25, } } 25

Slide 26

Slide 26 text

generics 26

Slide 27

Slide 27 text

from typing import Dict, Generic, TypeVar T = TypeVar("T") class Registry(Generic[T]): def __init__(self) -> None: self._store: Dict[str, T] = {} fn largest(list: &[T]) -> &T 27

Slide 28

Slide 28 text

assert assert x == 1 assert!(larger.can_hold(&smaller)); 28

Slide 29

Slide 29 text

Why Rust? 29

Slide 30

Slide 30 text

performance 30

Slide 31

Slide 31 text

threading (no GIL) 31

Slide 32

Slide 32 text

use rust crates 32

Slide 33

Slide 33 text

recursion 33

Slide 34

Slide 34 text

Py-Rust projects 34

Slide 35

Slide 35 text

Rustimport 35

Slide 36

Slide 36 text

./ termprint.rs // rustimport:pyo3 use pyo3::prelude::*; #[pyfunction] pub fn boxaround(s: &str){ println!("{}", "-".repeat(s.chars().count()+4)); println!("| {} |", s); println!("{}", "-".repeat(s.chars().count()+4)); } 36

Slide 37

Slide 37 text

$ python >>> import rustimport.import_hook >>> import termprint # This will pause for a moment to compile the module >>> termprint.boxaround('abc') ------- | abc | ------- 37

Slide 38

Slide 38 text

Protypting 38

Slide 39

Slide 39 text

$ python3 -m rustimport new my_single_file_extension.rs $ python3 -m rustimport new my_crate import my_crate 39

Slide 40

Slide 40 text

https://github.com/mityax/rustimport#usage-in-production 40

Slide 41

Slide 41 text

setuptools-rust 41

Slide 42

Slide 42 text

Poor docs most people no longer use it fit for binaries as well as modules see demo 42

Slide 43

Slide 43 text

Maturin 43

Slide 44

Slide 44 text

. └── guessing-game ├── Cargo.lock ├── Cargo.toml ├── pyproject.toml └── src └── lib.rs 44

Slide 45

Slide 45 text

$ maturing develop $ maturin build $ maturin publish 45

Slide 46

Slide 46 text

https://github.com/indygreg/PyOxidizer Packaging python with Rust micro contrib 46

Slide 47

Slide 47 text

https://github.com/RustPython/RustPython Py interp in rust 47

Slide 48

Slide 48 text

https://github.com/PyO3/pyo3 Rust bindings for python 48

Slide 49

Slide 49 text

https://pyo3.rs/v0.19.1/python_from_rust.html Python::with_gil(|py| { let fun: Py = PyModule::from_code( py, "def example(*args, **kwargs): if args != (): print('called with args', args) if kwargs != {}: print('called with kwargs', kwargs) if args == () and kwargs == {}: print('called with no arguments')", "", "", )? .getattr("example")? .into(); ... }) 49

Slide 50

Slide 50 text

use pyo3::prelude::*; Python::with_gil(|py| { let result = py .eval("[i * 10 for i in range(5)]", None, None) .map_err(|e| { e.print_and_set_sys_last_vars(py); })?; let res: Vec = result.extract().unwrap(); assert_eq!(res, vec![0, 10, 20, 30, 40]); Ok(()) }) 50

Slide 51

Slide 51 text

Common Gotchas 51

Slide 52

Slide 52 text

slow dev mode, release wrong usage of rust 52

Slide 53

Slide 53 text

integer overflow 53

Slide 54

Slide 54 text

Python projects using Rust 54

Slide 55

Slide 55 text

Pydantic Rewrote v2 in Rust 55

Slide 56

Slide 56 text

56

Slide 57

Slide 57 text

Polars The fast Pandas alternative 57

Slide 58

Slide 58 text

Ruff The fastest py linter 58

Slide 59

Slide 59 text

Robyn async web server in Rust 59

Slide 60

Slide 60 text

compileralchemy.com [email protected] @osdotsystem github.com/abdur-rahmaanj Thank you 60