# Chapter 6 Page 71 subprocess Module import datetime, subprocess class Orders: # instance method() uses self as param1 def __init__(self, orderID, productSKU, productPrice): self.orderID = orderID self.productSKU = productSKU self.productPrice = productPrice self.orderDate = datetime.datetime.now() # instance method() def calculate_total(self): qty = int( input('Enter quantity: ')) total_price = self.productPrice * qty return total_price # instance method() with a subprocess logging def log_order(self): log_message_main = f"Order ID: {self.orderID} | Order Date: {self.orderDate}\n" log_message_subprocess = f"Order ID: {self.orderID} Order Date: {self.orderDate}" # method 1 from chapter 4 open() using the main process with open("log_order_mainprocess.txt", "a") as logfile: logfile.write(log_message_main) # method 2 subprocess with powershell to logging subprocess.run(["powershell", "-Command", f"Add-Content -Path log_order_subprocess.txt -Value \"{log_message_subprocess}\""], shell=True) # Program Class # Process with a subprocess if __name__ == "__main__": # Create an instance of Orders Class order = Orders("1234", "SKU1234", 29.90) # Line Item Total total = order.calculate_total() # 1 * 29.99 Asset 29.9 # format for txt file print(f'${total:.2f}') # $125.25 # Log the order order.log_order() # both method 1 process and method 2 subprocess