From a1bbb11bd2fc98d5d2c505e4c9546888c6fc954e Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Tue, 27 Aug 2024 03:38:50 +0000 Subject: [PATCH] style: format code with Autopep8, Black, ClangFormat, dotnet-format, Go fmt, Gofumpt, Google Java Format, isort, Ktlint, PHP CS Fixer, Prettier, RuboCop, Ruff Formatter, Rustfmt, Scalafmt, StandardJS, StandardRB, swift-format and Yapf This commit fixes the style issues introduced in 28da700 according to the output from Autopep8, Black, ClangFormat, dotnet-format, Go fmt, Gofumpt, Google Java Format, isort, Ktlint, PHP CS Fixer, Prettier, RuboCop, Ruff Formatter, Rustfmt, Scalafmt, StandardJS, StandardRB, swift-format and Yapf. Details: None --- .../microtransaction_handler.py | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/iot-integration/microtransactions/microtransaction_handler.py b/iot-integration/microtransactions/microtransaction_handler.py index 9a77b35d4..6ef63ed75 100644 --- a/iot-integration/microtransactions/microtransaction_handler.py +++ b/iot-integration/microtransactions/microtransaction_handler.py @@ -1,14 +1,17 @@ -import os import json +import os import socket + from cryptography.fernet import Fernet + class MicrotransactionHandler: + def __init__(self, bank_account, microtransaction_secret): self.bank_account = bank_account self.microtransaction_secret = microtransaction_secret self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.socket.bind(('localhost', 8081)) + self.socket.bind(("localhost", 8081)) self.socket.listen(5) self.threads = [] @@ -16,31 +19,37 @@ def handle_microtransaction(self, client_socket): data = client_socket.recv(1024) if data: transaction_data = json.loads(data.decode()) - if transaction_data['type'] == 'TRANSFER': - amount = float(transaction_data['amount']) - if amount <= self.bank_account['balance']: - self.bank_account['balance'] -= amount - print(f'Transfer successful. New balance: {self.bank_account["balance"]}') - client_socket.send('TRANSFER_SUCCESSFUL'.encode()) + if transaction_data["type"] == "TRANSFER": + amount = float(transaction_data["amount"]) + if amount <= self.bank_account["balance"]: + self.bank_account["balance"] -= amount + print( + f'Transfer successful. New balance: {self.bank_account["balance"]}' + ) + client_socket.send("TRANSFER_SUCCESSFUL".encode()) else: - print('Insufficient funds') - client_socket.send('INSUFFICIENT_FUNDS'.encode()) - elif transaction_data['type'] == 'BALANCE': - client_socket.send(str(self.bank_account['balance']).encode()) + print("Insufficient funds") + client_socket.send("INSUFFICIENT_FUNDS".encode()) + elif transaction_data["type"] == "BALANCE": + client_socket.send(str(self.bank_account["balance"]).encode()) else: client_socket.close() def start_listening(self): while True: client_socket, address = self.socket.accept() - thread = threading.Thread(target=self.handle_microtransaction, args=(client_socket,)) + thread = threading.Thread( + target=self.handle_microtransaction, args=(client_socket,) + ) thread.start() self.threads.append(thread) def authenticate_client(self, client_socket): cipher_suite = Fernet(self.microtransaction_secret) encrypted_client_secret = client_socket.recv(1024) - if encrypted_client_secret == cipher_suite.encrypt(b'microtransaction_client_secret'): + if encrypted_client_secret == cipher_suite.encrypt( + b"microtransaction_client_secret" + ): return True else: return False @@ -54,8 +63,11 @@ def stop(self): for thread in self.threads: thread.join() -if __name__ == '__main__': - bank_account = {'balance': 1000.0} - microtransaction_secret = 'microtransaction_secret123' - microtransaction_handler = MicrotransactionHandler(bank_account, microtransaction_secret) + +if __name__ == "__main__": + bank_account = {"balance": 1000.0} + microtransaction_secret = "microtransaction_secret123" + microtransaction_handler = MicrotransactionHandler( + bank_account, microtransaction_secret + ) microtransaction_handler.start()