示例#1
0
def gen_utf8(length=10, smp=True, start=None, separator=''):
    """Return a random string made up of UTF-8 letters characters.

    Follows `RFC 3629`_.

    :param int length: Length for random data.
    :param str start: Random data start with.
    :param str separator: Separator character for start and random data.
    :param bool smp: Include Supplementary Multilingual Plane (SMP)
        characters
    :returns: A random string made up of ``UTF-8`` letters characters.
    :rtype: str

    .. _`RFC 3629`: http://www.rfc-editor.org/rfc/rfc3629.txt

    """
    UNICODE_LETTERS = [c for c in unicode_letters_generator(smp)]
    random.seed()
    output_string = ''.join(
        [random.choice(UNICODE_LETTERS) for _ in range(length)])

    if start:
        output_string = '{0}{1}{2}'.format(start, separator,
                                           output_string)[0:length]
    return output_string
示例#2
0
def gen_utf8(length=10, smp=True):
    """Return a random string made up of UTF-8 letters characters.

    Follows `RFC 3629`_.

    :param int length: Length for random data.
    :param bool smp: Include Supplementary Multilingual Plane (SMP)
        characters
    :returns: A random string made up of ``UTF-8`` letters characters.
    :rtype: str

    .. _`RFC 3629`: http://www.rfc-editor.org/rfc/rfc3629.txt

    """
    UNICODE_LETTERS = [c for c in unicode_letters_generator(smp)]
    random.seed()
    return ''.join([random.choice(UNICODE_LETTERS) for _ in range(length)])
示例#3
0
def test_chars_in_letters_category():
    """Unicode letters generator generates only unicode letters."""
    # Categories extracted from section 5.5.1 of
    # http://www.unicode.org/reports/tr44/tr44-4.html
    for char in unicode_letters_generator():
        assert unicodedata.category(char) in ('Lu', 'Ll', 'Lt', 'Lm', 'Lo')
示例#4
0
"""Collection of string generating functions."""
import random
import string

from fauxfactory.constants import HTML_TAGS, LOREM_IPSUM_TEXT
from fauxfactory.helpers import (
    check_len,
    check_validation,
    is_positive_int,
    unicode_letters_generator,
)

UNICODE_LETTERS = [c for c in unicode_letters_generator()]


def gen_string(str_type, length=None, validator=None, default=None, tries=10):
    """A simple wrapper that calls other string generation methods.

    :param str str_type: The type of string which should be generated.
    :param int length: The length of the generated string. Must be 1 or
        greater.
    :param validator: Function or regex (str).
        If a function it must receive one parameter and return True if value
        can be used and False of another value need to be generated.
        If str it will be used as regex to validate the generated value.
        Default is None which will not validate the value.
    :param tries: number of times validator must be called before returning
        `default`. Default is 10.
    :param default: If validator returns false a number of `tries` times, this
        value is returned instead. Must be defined if validator is not None
    :raises: ``ValueError`` if an invalid ``str_type`` is specified.