Skip to content

Instantly share code, notes, and snippets.

@SatishGodaPearl
Forked from Kif11/basic_logging.py
Last active January 21, 2020 19:48
Show Gist options
  • Save SatishGodaPearl/710af13a210c7570c653dab2e3c29104 to your computer and use it in GitHub Desktop.
Save SatishGodaPearl/710af13a210c7570c653dab2e3c29104 to your computer and use it in GitHub Desktop.
Python basic logging boilerplate for console log
import logging
log = logging.getLogger()
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
log.addHandler(handler)
log.setLevel(logging.INFO)
log.info('Hello World!')
import logging
log = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter('%(levelname)s: %(message)s')
handler.setFormatter(formatter)
log.addHandler(handler)
log.setLevel(logging.INFO)
class Number(object):
def __init__(self, default=0):
self._value = default
def getValue(self):
return self._value
def setValue(self, value):
self._value = value
class Add(object):
number1 = Number()
number2 = Number()
output = Number()
_cache = {
}
@classmethod
def cache(cls, inputs):
if inputs in cls._cache:
return cls._cache[inputs]
log.debug("Cache miss: input {0}".format(inputs))
return None
@classmethod
def function(cls, number1, number2):
return number1 + number2
def execute(self):
number1 = self.number1.getValue()
number2 = self.number2.getValue()
result = self.cache((number1, number2))
if result is None:
result = self.function(number1, number2)
self._cache[(number1, number2)] = result
self.output.setValue(result)
add = Add()
add.execute()
print add._cache
add.number1.setValue(100)
log.setLevel(logging.DEBUG)
add.execute()
print add._cache
log.setLevel(logging.INFO)
add.number2.setValue(300)
add.execute()
print add._cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment