Ethereum: Try to carry out the Python code in the command prompt, but it does not work
Evaluated by a developer who prayed a time with Python and his ecosystem, I recently performed when I carried out my Python code on Ethereum Blockchain. In this article we will investigate that my test -API code is not violently around -prompt (or anaconda prompt) and how to repeat your test.
The problem: Run code to the command of the input
Tort with us, let’s assume that I have an example -Python script that connects a connection to Binance tests to the testnet -api:
`Python
Import suggestions
def get_data ():
url = ' symbol=bnb&interval=1min'
Response = Requests.Get (url)
Data = response.json ()
Return data ['symbols']
Data = get_data ()
Print (data)
If I try to decide this script from the command prompt (or Anaconda entry prompt), it throws an earror:
`
$ python test_apy.py
Traceback (the latter call of the last call):
File “test_api.py”, line 3, in
url = ‘ symbol=bnb&interval=1min’
File “/Home/User/Test_Apy”, line 2, not get_data
Response = Requests.Get (url)
Attribute terror: ‘Nonetype’ object is not unable.
`
There will be a different message that “no” variable “is” not “, which answers binance -api failed.
The solution: Use a test frameworks
To solve this problem, I decided to say goodbye to the framework weakness to test my code. Here is the updated version of the script:
`Python
Import suggestions
Import the Requests_Tests Testetwork
def get_data ():
url = ' symbol=bnb&interval=1min'
The report by testnetwork.get_Response (s)
Data = get_data ()
print (data ['symbols'])))
With “Pytest” you can test my code for manual writing the API request. The test framework automatically simulates the requirements and check whether it is self -impression.
The advantage: more reliability
By using a test framework, I can make sure that my code, on which I was able to set up the potential body in front of no productive servers. This has several advantages, including:
- Reduced downtime: If a problem occurs during the test, I can identify and fix a problem with the disruption of the system.
- Increased reliability: testing frameworks likes “pytest” help in isolation of Issuet and prevalence.
Diploma
In the flexible, the round code in the command prompt (or the Anaconda entry prompt) can be a challenging expert, especially if tests in the API. By using a test frameworks such as “Pytest”, I was properly used to fix the edition and the API code of Ensurgity. This approach has several advantages, including higher reliability and reduced downtime.
As a developer, it is important to consciously on the severe problems and take measures to alleviate them. In this case, the use of a test frameworks such as “Pytest” helped to solve the problem. By following the best practices for testing and debugging code, we can make sure that the application is reliable, efficient and safe.
Additional tip
- Always test your code like this before rounding it off in production.
- Test framework -like
- Keep your dependencies up to date to secure your latest version of the required libraries.
- Consider the Surection variable to save sensitive data such as API key and login information.
I wave in this article, a Ben is helpful in illustration of the problem and solving the Ethereum test -API. If you have any questions or comments, you feed them free!
Leave a Reply