''' Sample code to convert a module on import to snake_case from camelCase. I wouldn't use this in production, but its kind of interesting to play with. (C) 2023 - MIT License - Charles Machalow ''' import inflection # pip install inflection import inspect import importlib from typing import Any def to_underscore(thang: Any): dir_thang = dir(thang) for name in dir_thang: if not name.startswith('_'): thing = getattr(thang, name) if hasattr(thing, '__mro__'): # a class if inflection.underscore(name) not in dir(thing): setattr(thang, name, to_underscore(thing)) elif inspect.isfunction(thing): # a function if inflection.underscore(name) != name: if inflection.underscore(name) not in dir_thang: # print(f"{thing}.{name} -> {inflection.underscore(name)}") setattr(thang, inflection.underscore(name), thing) # todo: could add support for instances, other modules, etc. return thang def snakeify(mod: str): module = importlib.import_module(mod) return to_underscore(module) if __name__ == '__main__': logging = snakeify('logging') log = logging.get_logger(__name__) log.set_level(logging.DEBUG) sh = logging.StreamHandler() sh.set_level(logging.DEBUG) sh.set_formatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) log.add_handler(sh) log.info('hi')