def main(): print("Main start") my_module.foo() bar = my_module.Bar() bar.bar() logger.info('Hi, world') logger.debug('Hi, world')
def test_foo(self): client = mock.MagicMock() client.query = mock.MagicMock(return_value=[]) query = "SELECT 1" y = my_module.foo(client, query) self.assertEqual(y, [3]) client.query.assert_called_with(query) # will raise an error if false
def main(): # logger = logging.getLogger() util.setup_logging() # logger = logging.getLogger(__name__) # send some messages # logger.debug('debug message') # logger.info('info message') logging.debug('debug message') logging.info('info message') logging.warning('warn message') logging.error('error message') logging.critical('critical message') # inner fnc inner_fnc() # run func1 lib1.func1() # run func1 my_module.foo() bar = my_module.Bar() bar.bar()
import os import yaml import logging import logging.config # load my module import my_module # init logging config my_module.setup_logging() my_module.foo() bar = my_module.Bar() bar.bar() bar.barError() logger = logging.getLogger(__name__) logger.info('Some random info message from main.py') try: open('/path/to/does/not/exist', 'rb') except (SystemExit, KeyboardInterrupt): raise except Exception as e: logger.error('Failed to open file', exc_info=True) logger.debug('Debugging something in main')
# -*- coding:utf-8 -*- """ You can see a lots of example out there (including this article, I did it just for giving example in short) get logger at module level. They looks harmless, but actually, there is a pitfall – Python logging module respects all created logger before you load the configuration from a file. And you expect to see the records appear in log, but you will see nothing. Why? Because you create the logger at module level, you then import the module before you load the logging configuration from a file. The logging.fileConfig and logging.dictConfig disables existing loggers by default. So, those setting in file will not be applied to your logger. It’s better to get the logger when you need it. It’s cheap to create or get a logger. """ import logging import logging.config # load my module import my_module # load the logging configuration logging.config.fileConfig('logging.ini') my_module.foo() bar = my_module.Bar() bar.bar()
import numba def foo(x): for k in x: print("k:", k) for a, b in x[k]: print(a, b) if sys.argv[-1] == "-c": # numba compile print("compiling") from numba.pycc import CC cc = CC('my_module') cc.export( 'foo', 'void(DictType(int64,ListType(UniTuple(int64,2))))')(foo) cc.compile() exit() else: x = numba.typed.Dict() x[1] = numba.typed.List([(1, 2), (3, 4)]) x[100] = numba.typed.List([(100, 200)]) if sys.argv[-1] == "-p": print(numba.typeof(x)) # => DictType[int64,ListType[UniTuple(int64 x 2)]] else: from my_module import foo foo(x)