I'm working on a small python project and have a general_code.py file where I have functions that I use throughout the project. Some examples:
def to_boolean(var):
return var in ['True', 'true', '1', 'y', 'Y' 'yes', 'Yes']
e.g 2
def get_last_line(in_file, block_size=128, ignore_ending_newline=False):
e.g 3
def signal_handler(log, self, signum, frame): # pragma: no cover
log.info("Ctrl + C pressed. Exiting application gracefully")
self.run = False
while threading.active_count() > 1:
time.sleep(1)
log.info("All threads closed. Exit.")
sys.exit(0)
e.g 4.
class Timer(object): # pragma: no cover
def __init__(self, name=None):
self.name = name
def __enter__(self):
self.tstart = time.time()
def __exit__(self, type, value, traceback):
if self.name:
print('[%s]' % self.name,)
print('Elapsed: %s' % (time.time() - self.tstart))
e.g 5
def get_file_version(log, filename=None):
e.g 6
def setup_logger(filename):
Is it good design to have such a central place for common functionality? It has served me well, but often ideas that might be great in a small scope can end up terrible in bigger projects or different scopes. So what would be a good way in python to group common functionality? Is having all functions simply in one file like I do fine or would it be better to put them all into an own class or something completely different?