Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Boosting Python With Rust 🚀

Boosting Python With Rust 🚀

RIIR makes much sense in the Python world. This session explores the development and distribution of Python apps written in Rust.

This point in time has seen the development of great toolings and major libraries switching to Rust runtimes. This provides concrete validation for adopting this approach.

The session also includes a case study of projects written in Rust as well as past approaches to the topic.

Abdur-Rahmaan Janhangeer

July 20, 2023
Tweet

More Decks by Abdur-Rahmaan Janhangeer

Other Decks in Programming

Transcript

  1. 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
  2. 11

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

    2 >>> next(x) 3 >>> dir([1]) [..., '__iter__', ...] 16
  4. let nums = vec![1, 2, 3]; for num in nums.iter()

    { println!("{}", num); } loop { match range.next() { Some(x) => { println!("{}", x); }, None => { break } } } 17
  5. for i, item in enumerate(bytes): if item == b' ':

    return i for (i, &item) in bytes.iter().enumerate() { if item == b' ' { return i; } } 21
  6. -> return type def x() -> None: ... fn area(&self)

    -> u32 { self.width * self.height } 23
  7. 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
  8. from typing import Dict, Generic, TypeVar T = TypeVar("T") class

    Registry(Generic[T]): def __init__(self) -> None: self._store: Dict[str, T] = {} fn largest<T>(list: &[T]) -> &T 27
  9. ./ 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
  10. $ python >>> import rustimport.import_hook >>> import termprint # This

    will pause for a moment to compile the module >>> termprint.boxaround('abc') ------- | abc | ------- 37
  11. Poor docs most people no longer use it fit for

    binaries as well as modules see demo 42
  12. https://pyo3.rs/v0.19.1/python_from_rust.html Python::with_gil(|py| { let fun: Py<PyAny> = 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
  13. 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<i64> = result.extract().unwrap(); assert_eq!(res, vec![0, 10, 20, 30, 40]); Ok(()) }) 50
  14. 56