Ethereum: Passing data from within a class into def
Ethereum Websocket Data Data Problem: data loading from Class A Def
When using the “Binance.py” file, one of the errors you can meet is related to the delivery of data between classes and functionality in Python. In this article, we will take care of how to solve the problem of sending data using the binance bees via websocket.
Problem:
Suppose you have a class called “Myclass” with methods that process websocket connections and send/receive data. When they are called these methods from other classes or functionality, it is possible to find an error relating to the transmission variables between them. This can be caused by the way Python manages the arguments of the characteristics.
Solution:
To solve this problem, it is necessary to provide variables as keyword topics when called “Myclass” methods. In this way you can change the code:
`Python
It matters the website
from binance.client import client
Myclass class:
Def __init __ (self):
self.data_to_send = none
Async Def Handle_Connection (Self, Websocket):
Upload the data to be sent here as a topic of keywords
Self.Data_to_Send = “Hello from Myclass!”
Async Defle_data_received (self, data):
PRINT (F “received: {Data}”)
Async Def Main ():
Client = client ()
Async with websocket
My_class = Myclass ()
Create a Myclass instance
Expect my_class.handle_connection (websocket)
While it is true:
Data = Abveat Websocket.Recv ()
my_class.handle_data_received (data)
Upload the data to be sent here
If __name__ == “__Main__”:
Principal ()
`
Explanation:
In a modified code, we create an application for "Myclass in the” main function () is calling its methods directly. When trying to pass the variables between classes or functions without using keyword topics, Python raises Typeeerror. Delivering variables as keyword topics (Self
The best procedures:
To avoid potential problems with the variable transition, take into consideration the use of functional and class methods instead of attributes of an application. For example::
Python
Async Def My_Method (self -(self):
Self.Send_data (“Hello from Myclass”)
Def Main ():
Client = client ()
Async with websocket
Expect My_Method (Websocket)
Load Websocket Method
Instead
My_class = Myclass ()
Create a Myclass instance
Then call the Hander_Connection Metors directly and handler methods
`
According to these steps, you should be able to solve the problem of delivering class ADef ‘data when using websockets with the Binanne API interface.