r/backtickbot • u/backtickbot • Mar 30 '21
https://np.reddit.com/r/learnpython/comments/mgcaal/how_are_you_planning_your_code/gssp8zc/
Over the years I've developed this procedure when starting with a new module or project. I call it the "Top-Down" approach or "The main first approach. This is very similar to what most of the other comments have suggested.
It helps to organize the code organically. Once you have the main written with essential setups or function call, I then implement the internal functions/classes. I avoid the temptation of implementing internal functions, classes initially. Just create an empty template
Always have the end result in your mind. A few question that you should ask yourself
- How is this module/project going to be used
- How is it going to executed/deployed
- What are the command line arguments, configurable parameters i want in my module
Start with a template setup.py and an empty start start script with just a main function in it. For e.g , If i am building an application that fetches some data from a website, processes it and then stores the processed data , the code would be
python data_processor.py --url www.reddit.com --interval 30
I've added an additional parameter just for demonstration purpose. If I want these parameter in a config file, I'll create a config file, say *config.json*
json
{
'url' : 'www.reddit.com,
'interval' : 30.0
}
My code would then run like
python data_processor.py --config config.py
This already gives me an idea of what the command line arguments would be. I then implement the main file . which could look something like this
python
from argparse import ArgumentParser
import json
def main(config):
....
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument('-c', '--config') # and some other params
args = parser.parser_args()
with open(args.config) as f:
config = json.load(f)
main(config)
I have not implemented the *main* function yet, but i already have a working code for running and testing my script. From here on, start implementing how the execution would proceed. For example you want to parse some data and store it in a file or database. The main function can thus look like
python
def main(config):
url = config['url']
db_connector = DataBaseHandler()
data = get_data(url)
processed_data = process_data(data)
db_connector.store(processed_data)
Even at this point, the functions are not implemented. But the design of the code is getting created on it's own with this "top-down" approach. I could have empty functions in a separate file which look something like this. Lets call it, data_handler.py
python
class DataBaseHandler:
def __init__(self):
...
def store(self, data):
...
def get_data(url):
...
def process_data(url):
...
I can still run my code and test it. Although nothing happens, the steps are becoming mode and more clear. If i want to just test a small function in a separate file, i add an if __name__ == "__main__" block in that file and call that function with some predefined parameters. for e.g the file data_handler.py can have something like this
if __name__ == "__main__":
data = "some data"
processed = process_data(data)
print(processed)
The top down approach build your code is a more systematic manner. You also avoid going back and forth between function calls and modifying parameters and other things. This is a very basic approach that i follow for my work