From c1e90ca993a0ef1977fbf65a58129e88bddd8025 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Sat, 9 Sep 2017 16:08:26 +0200 Subject: [PATCH] Here is are the functions that do more general purpose things. --- seasonMover/utils.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 seasonMover/utils.py diff --git a/seasonMover/utils.py b/seasonMover/utils.py new file mode 100644 index 0000000..87e3599 --- /dev/null +++ b/seasonMover/utils.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +from datetime import datetime +import hashlib +import os +import re +import struct + +def sanitize(string, ignore_characters=None): + """Sanitize a string to strip special characters. + + :param str string: the string to sanitize. + :param set ignore_characters: characters to ignore. + :return: the sanitized string. + :rtype: str + + """ + # only deal with strings + if string is None: + return + + ignore_characters = ignore_characters or set() + + # replace some characters with one space + characters = {'-', ':', '(', ')', '.'} - ignore_characters + if characters: + string = re.sub(r'[%s]' % re.escape(''.join(characters)), ' ', string) + + # remove some characters + characters = {'\''} - ignore_characters + if characters: + string = re.sub(r'[%s]' % re.escape(''.join(characters)), '', string) + + # replace multiple spaces with one + string = re.sub(r'\s+', ' ', string) + + # strip and lower case + return string.strip().lower() +