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']
def get_last_line(in_file, block_size=128, ignore_ending_newline=False):
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)
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))
def get_file_version(log, filename=None):
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?