Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello, this PR is a refractor of your code. That means it won't change the behaviour of the code, but the code quality will be improved: writing nice code is important to collaborate with other people :)
I suppose you are new to python: for this reason, I am writing here some "mistakes" you've made with the solution & explanation. Hit me up for any question!
This part
status['status'] == 0
is a boolean value already! You can simply return it. learn moreIMPORTANT: dictionary as default parameter
Dictionary as default values in functions might not work as you except. This is a serious issue!

If you change the dictionary, it stays like that also in the following calls.
I highly suggest to check out the "Default Parameter Values in Python" link you find on this question on stackoverflow.
open()
has a context manager: it's suggested to perform file operations using it. In other words, I've replaced your old code......with the same thing, using context manager
Some lines could have be inserted in a for loop.
You should comment the meaning of the code, not the actual line! A quick example of comments I've deleted:
About
if
: Instead of using just theelse
part, you can reverse the condition, quick exampleold:
new:
It's really a common practice (even in its documentation) to
import pandas as pd
andimport numpy as np
In functions, you should totally avoid nested conditions & returns!
Old code:
Refractored, more readable, new code:
If there is some code at the end that will get excecuted in any scenario, you can move it ouside. Example from your (old) code:
new:
Tests in python should be like
tests/test_trade_capture.py
file, please move all the tests (functions) in aclass Something(unittest.TestCase)
pythonic ways to do things
Replace
with
(or upgrade to python 3.10 and use the match)
You don't have to create SQL string manually to handble database-related stuffs, there are more pythonic ways to do that.
Check PEP8 style guide
There is a lot of duplicated code you can improve, maybe move it in a function and call it every time you need it. One function call is better then the same 5 lines everytime!
test and "real" API_KEYS (& info) should be changed in an .env file or something like that. The script shouldn't even know which is the current enviroment. You could have a PROD_ENV and a TEST_ENV files and just select what env file to use. There are a lot of options tho! .env library
I hope everything makes sense to you, feel free to comment on this PR and we will discuss :)
See you soon,
Lorenzo