Skip to content

Instantly share code, notes, and snippets.

@schaunwheeler
Last active March 8, 2019 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schaunwheeler/554a358314b64148360c29af8bb67bc4 to your computer and use it in GitHub Desktop.
Save schaunwheeler/554a358314b64148360c29af8bb67bc4 to your computer and use it in GitHub Desktop.
Data science productionization: maintenance - example 1
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# create a file handler
handler = logging.FileHandler('error.log')
handler.setLevel(logging.ERROR)
# create a logging format
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
def normalize_word(word, stops=None):
'''
Processes a string by stripping whitespace, lowercasing, and
optionally removing stop characters.
Args:
word (str): The word to be processed.
stops (list): A list of characters to remove from the string.
Returns:
str: The processed string. May be zero length.
'''
assert isinstance(word, str)
try:
word = word.strip().lower()
if stops:
word = ''.join([char for char in word if char not in stops])
return word
except (SystemExit, KeyboardInterrupt):
raise
except Exception, e:
logger.error('Failed to process string', exc_info=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment