Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Speed Up Your Python with Rust

You're reading from   Speed Up Your Python with Rust Optimize Python performance by creating Python pip modules in Rust with PyO3

Arrow left icon
Product type Paperback
Published in Jan 2022
Publisher Packt
ISBN-13 9781801811446
Length 384 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Maxwell Flitton Maxwell Flitton
Author Profile Icon Maxwell Flitton
Maxwell Flitton
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Section 1: Getting to Understand Rust
2. Chapter 1: An Introduction to Rust from a Python Perspective FREE CHAPTER 3. Chapter 2: Structuring Code in Rust 4. Chapter 3: Understanding Concurrency 5. Section 2: Fusing Rust with Python
6. Chapter 4: Building pip Modules in Python 7. Chapter 5: Creating a Rust Interface for Our pip Module 8. Chapter 6: Working with Python Objects in Rust 9. Chapter 7: Using Python Modules with Rust 10. Chapter 8: Structuring an End-to-End Python Package in Rust 11. Section 3: Infusing Rust into a Web Application
12. Chapter 9: Structuring a Python Flask App for Rust 13. Chapter 10: Injecting Rust into a Python Flask App 14. Chapter 11: Best Practices for Integrating Rust 15. Other Books You May Enjoy

Building structs instead of objects

In Python, we use a lot of objects. In fact, everything you work with in Python is an object. In Rust, the closest thing we can get to objects is structs. To demonstrate this, let's build an object in Python, and then replicate the behavior in Rust. For our example, we will build a basic stock object as seen in the following code:

class Stock:
    def __init__(self, name: str, open_price: float,\
      stop_loss: float = 0.0, take_profit: float = 0.0) \
        -> None:
        self.name: str = name
        self.open_price: float = open_price
        self.stop_loss: float = stop_loss
        self.take_profit: float = take_profit
        self.current_price: float = open_price
    def update_price(self, new_price: float) -> None:
        self.current_price = new_price

Here, we can see that we have two mandatory fields, which are the name and price of the stock. We can also have an optional stop loss and an optional take profit. This means that if the stock crosses one of these thresholds, it forces a sale, so we don't continue to lose more money or keep letting the stock rise to the point where it crashes. We then have a function that merely updates the current price of the stock. We could add extra logic here on the thresholds for it to return a bool for whether the stock should be sold or not if needed. For Rust, we define the fields with the following code:

struct Stock {
    name: String,
    open_price: f32,
    stop_loss: f32,
    take_profit: f32,
    current_price: f32
}

Now we have our fields for the struct, we need to build the constructor. We can build functions that belong to our struct with an impl block. We build our constructor with the following code:

impl Stock {
    fn new(stock_name: &str, price: f32) -> Stock {
        return Stock{
            name: String::from(stock_name), 
            open_price: price,
            stop_loss: 0.0,
            take_profit: 0.0,
            current_price: price
        }
    }
}

Here, we can see that we have defined some default values for some of the attributes. To build an instance, we use the following code:

let stock: Stock = Stock::new("MonolithAi", 95.0);

However, we have not exactly replicated our Python object. In the Python object __init__, there were some optional parameters. We can do this by adding the following functions to our impl block:

    fn with_stop_loss(mut self, value: f32) -> Stock {
        self.stop_loss = value;
        return self
    }
    fn with_take_profit(mut self, value: f32) -> Stock {
        self.take_profit = value;
        return self
    }

What we do here is take in our struct, mutate the field, and then return it. Building a new stock with a stop loss involves calling our constructor followed by the with_stop_loss function as seen here:

let stock_two: Stock = Stock::new("RIMES",\
    150.4).with_stop_loss(55.0);

With this, our RIMES stock will have an open price of 150.4, current price of 150.4, and a stop loss of 55.0. We can chain multiple functions as they return the stock struct. We can create a stock struct with a stop loss and a take profit with the following code:

let stock_three: Stock = Stock::new("BUMPER (former known \
  as ASF)", 120.0).with_take_profit(100.0).\
    with_stop_loss(50.0);

We can continue chaining with as many optional variables as we want. This also enables us to encapsulate the logic behind defining these attributes. Now that we have all our constructor needs sorted, we need to edit the update_price attribute. This can be done by implementing the following function in the impl block:

fn update_price(&mut self, value: f32) {
    self.current_price = value;
}

This can be implemented with the following code:

let mut stock: Stock = Stock::new("MonolithAi", 95.0);
stock.update_price(128.4);
println!("here is the stock: {}", stock.current_price);

It has to be noted that the stock needs to be mutable. The preceding code gives us the following printout:

here is the stock: 128.4

There is only one concept left to explore for structs and this is traits. As we have stated before, traits are like Python mixins. However, traits can also act as a data type because we know that a struct that has the trait has those functions housed in the trait. To demonstrate this, we can create a CanTransfer trait in the following code:

trait CanTransfer {
    fn transfer_stock(&self) -> ();
    
    fn print(&self) -> () {
        println!("a transfer is happening");
    }
}

If we implement the trait for a struct, the instance of the struct can utilize the print function. However, the transfer_stock function doesn't have a body. This means that we must define our own function if it has the same return value. We can implement the trait for our struct using the following code:

impl CanTransfer for Stock {
    fn transfer_stock(&self) -> () {
        println!("the stock {} is being transferred for \
          £{}", self.name, self.current_price);
    }
}

We can now use our trait with the following code:

let stock: Stock = Stock::new("MonolithAi", 95.0);
stock.print();
stock.transfer_stock();

This gives us the following output:

a transfer is happening
the stock MonolithAi is being transferred for £95

We can make our own function that will print and transfer the stock. It will accept all structs that implement our CanTransfer trait and we can use all the trait's functions in it, as seen here:

fn process_transfer(stock: impl CanTransfer) -> () {
    stock.print();
    stock.transfer_stock();
}

We can see that traits are a powerful alternative to object inheritance; they reduce the amount of repeated code for structs that fit in the same group. There is no limit to the number of traits that a struct can implement. This enables us to plug traits in and out, adding a lot of flexibility to our structs when maintaining code.

Traits are not the only way by which we can manage how structs interact with the rest of the program; we can achieve metaprogramming with macros, which we will explore in the next section.

You have been reading a chapter from
Speed Up Your Python with Rust
Published in: Jan 2022
Publisher: Packt
ISBN-13: 9781801811446
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime