Source code for sqla_inspect.ascii

# -*- coding: utf-8 -*-
# * Authors:
#       * Arezki Feth <f.a@majerti.fr>;
#       * Miotte Julien <j.m@majerti.fr>;
#       * TJEBBES Gaston <g.t@majerti.fr>
"""
    Provide common tools for string handling
"""
import re
import random
from string import lowercase


[docs]def force_ascii(datas): """ Return enforced ascii string éko=>ko """ return "".join((i for i in datas if ord(i) < 128))
[docs]def force_utf8(value): """ return a utf-8 string """ if isinstance(value, unicode): value = value.encode('utf-8') return value
[docs]def force_unicode(value): """ return an utf-8 unicode entry """ if isinstance(value, str): value = value.decode('utf-8') return value
[docs]def camel_case_to_name(name): """ Used to convert a classname to a lowercase name """ convert_func = lambda m:"_" + m.group(0).lower() return name[0].lower() + re.sub(r'([A-Z])', convert_func, name[1:])
[docs]def gen_random_string(size=15): """ Generate random string size size of the resulting string """ return ''.join(random.choice(lowercase) for _ in range(size))
[docs]def random_tag_id(size=15): """ Return a random string supposed to be used as tag id """ return gen_random_string(size)
[docs]def to_utf8(datas): """ Force utf8 string entries in the given datas """ res = datas if isinstance(datas, dict): res = {} for key, value in datas.items(): key = to_utf8(key) value = to_utf8(value) res[key] = value elif isinstance(datas, (list, tuple)): res = [] for data in datas: res.append(to_utf8(data)) elif isinstance(datas, unicode): res = datas.encode('utf-8') return res