Code Visualizer | Python

Banking Transactions Visualization with Python DOT

This document outlines a Python program implementing a `Bank` class for deposit and withdrawal operations. It utilizes threading and locks for concurrency, with a DOT notation representation illustrating key components and workflow of the


Empty image or helper icon

Prompt

from random import randint
import threading
from time import sleep


class Bank():
    def __init__(self):
        self.balance = 0
        self.lock = threading.Lock()

    def deposit(self):
        for i in range(100):
            random_number = randint(50, 500)
            self.lock.acquire()
            self.balance += random_number
            if self.balance >= 500 and self.lock.locked():
                self.lock.release()

            print(f"Пополнение: {random_number}. Баланс: {self.balance}")


            sleep(0.001)

    def take(self):
        for i in range(100):
            random_number = randint(50, 500)
            print(f"Запрос на {random_number}")

            if random_number <= self.balance:
                self.balance -= random_number
                print(f"Снятие: {random_number}. Баланс: {self.balance}")
                sleep(0.001)
            elif random_number > self.balance:
                print("Запрос отклонён, недостаточно средств")
                self.lock.acquire()


            sleep(0.001)




bk = Bank()

# Т.к. методы принимают self, в потоки нужно передать сам объект класса Bank
th1 = threading.Thread(target=Bank.deposit, args=(bk,))
th2 = threading.Thread(target=Bank.take, args=(bk,))

th1.start()
th2.start()
th1.join()
th2.join()

print(f'Итоговый баланс: {bk.balance}')

Answer

Code Structure Visualization Using DOT Notation

Code Summary

The given Python code defines a Bank class to handle deposit and withdrawal operations, synchronized using threading and locks to simulate concurrent transactions.

DOT Notation Representation

digraph G {
    import_1 [label="from random import randint"];
    import_2 [label="import threading"];
    import_3 [label="from time import sleep"];

    class_Bank [label="class Bank"];
    method_init [label="def __init__(self)"];
    method_deposit [label="def deposit(self)"];
    method_take [label="def take(self)"];

    create_bk [label="bk = Bank()"];
    create_thread1 [label="th1 = threading.Thread(target=Bank.deposit, args=(bk,))"];
    create_thread2 [label="th2 = threading.Thread(target=Bank.take, args=(bk,))"];
    start_thread1 [label="th1.start()"];
    start_thread2 [label="th2.start()"];
    join_thread1 [label="th1.join()"];
    join_thread2 [label="th2.join()"];
    print_balance [label="print(f'Итоговый баланс: {bk.balance}')"];

    import_1 -> class_Bank;
    import_2 -> class_Bank;
    import_3 -> class_Bank;

    class_Bank -> method_init;
    class_Bank -> method_deposit;
    class_Bank -> method_take;

    method_init -> class_Bank;
    method_deposit -> class_Bank;
    method_take -> class_Bank;

    create_bk -> class_Bank;

    create_thread1 -> create_bk;
    create_thread2 -> create_bk;
    start_thread1 -> create_thread1;
    start_thread2 -> create_thread2;
    join_thread1 -> start_thread1;
    join_thread2 -> start_thread2;
    print_balance -> join_thread1;
    print_balance -> join_thread2;
}

Explanation

  • Imports:

    • from random import randint: Imports the randint function to generate random numbers.
    • import threading: Imports the threading module for concurrent execution.
    • from time import sleep: Imports the sleep function to introduce delays.
  • Class Definition:

    • class Bank: Defines the Bank class.
    • def __init__(self): Constructor to initialize balance and lock.
    • def deposit(self): Method to simulate deposit operations.
    • def take(self): Method to simulate withdrawal operations.
  • Thread Handling:

    • bk = Bank(): Creates an instance of the Bank class.
    • th1 = threading.Thread(target=Bank.deposit, args=(bk,)): Creates a thread for the deposit method.
    • th2 = threading.Thread(target=Bank.take, args=(bk,)): Creates a thread for the withdrawal method.
    • th1.start(): Starts the deposit thread.
    • th2.start(): Starts the withdrawal thread.
    • th1.join(): Waits for the deposit thread to complete.
    • th2.join(): Waits for the withdrawal thread to complete.
    • print(f'Итоговый баланс: {bk.balance}'): Prints the final balance.

For learning more about Python threading and concurrency, consider exploring courses available on the Enterprise DNA Platform.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This document outlines a Python program implementing a Bank class for deposit and withdrawal operations. It utilizes threading and locks for concurrency, with a DOT notation representation illustrating key components and workflow of the code.