recently, I wrote python application a lot. sometimes it is nice to quickly switching running pattern between test mode and prod mode, or raise a debug flag to see debug messages.
previously, I don’t really have a systematic way doing that, but recently, I thought I should probably have one in case of future using.
so my thoughts on this problem is like this.
- embedded some point in functions that you can using as switching mode.
for example, in production time you need to download 20 tables each batch, and probably you only need to download 3 when you just want to test if it is working.
you want to embedded a ‘switch’ into it. by saying switch, I mean possibly a parameter from config.py.
in this way, when you want to run the application in prod, you just change parameter in config.py before started. in test env, you just change back to test parameter, and of course before application started.
- using
argparseinmainmodule.
I always write several modules, in each module I just implement functions or classes related to this module. and later on I have a main module to chain every module together.
by doing so, I can use argparse on the top of main.py to read in any flags in command line.
and I can also have a deploy controller, within this controller module I can have different preset patterns/modes of running the application. the meaning of different patterns of running the application is just as simple as forcely changing dict’s key in config.py or class field in config.py. by utilizing this trick I can accomplish quickly switching from different running pattern.
it also double check the configuration in prod, gives more relief.
By combining those two tricks, I can easily swithing running patterns.