Source code for discoursegraphs.util

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Arne Neumann <discoursegraphs.programming@arne.cl>

"""
This module contains a number of helper functions.
"""

import re

INTEGER_RE = re.compile('([0-9]+)')


[docs]def natural_sort_key(s): """ returns a key that can be used in sort functions. Example: >>> items = ['A99', 'a1', 'a2', 'a10', 'a24', 'a12', 'a100'] The normal sort function will ignore the natural order of the integers in the string: >>> print sorted(items) ['A99', 'a1', 'a10', 'a100', 'a12', 'a2', 'a24'] When we use this function as a key to the sort function, the natural order of the integer is considered. >>> print sorted(items, key=natural_sort_key) ['A99', 'a1', 'a2', 'a10', 'a12', 'a24', 'a100'] """ return [int(text) if text.isdigit() else text for text in re.split(INTEGER_RE, s)]
[docs]def ensure_unicode(str_or_unicode): """ tests, if the input is ``str`` or ``unicode``. if it is ``str``, it will be decoded from ``UTF-8`` to unicode. """ if isinstance(str_or_unicode, str): return str_or_unicode.decode('utf-8') elif isinstance(str_or_unicode, unicode): return str_or_unicode else: raise ValueError("Input '{0}' should be a string or unicode, " "but its of type {1}".format(str_or_unicode, type(str_or_unicode)))