Which part should the package configuration in Python script?
Question:
I have few code which set the configuration for logging and package setting.
pd.set_option('display.max_columns', None)
logging.getLogger("urllib3").setLevel(logging.ERROR)
Where should I put these into the script (under import, under if __name__ == "__main__"
, etc) or can I create a configuration file for setting configuration?
import pybit
import pandas as pd
def get_data():
# get data from bybit package
def data_cleaning(data):
# using pandas for cleaning the data
if __name__ == "__main__":
data = get_data()
cleaned_data = data_cleaning(data)
Answers:
You should do it in the if __name__ == '__main__':
clause – you can either do it inline or via a separate function that e.g. reads a configuration file.
Re. the comment by Yoav Sheetrit – note that side effects should be avoided when doing imports, so I would recommend not doing these after the import
statements.
I have few code which set the configuration for logging and package setting.
pd.set_option('display.max_columns', None)
logging.getLogger("urllib3").setLevel(logging.ERROR)
Where should I put these into the script (under import, under if __name__ == "__main__"
, etc) or can I create a configuration file for setting configuration?
import pybit
import pandas as pd
def get_data():
# get data from bybit package
def data_cleaning(data):
# using pandas for cleaning the data
if __name__ == "__main__":
data = get_data()
cleaned_data = data_cleaning(data)
You should do it in the if __name__ == '__main__':
clause – you can either do it inline or via a separate function that e.g. reads a configuration file.
Re. the comment by Yoav Sheetrit – note that side effects should be avoided when doing imports, so I would recommend not doing these after the import
statements.