diff --git a/fauxfactory/__init__.py b/fauxfactory/__init__.py index f79e246..348dcc9 100644 --- a/fauxfactory/__init__.py +++ b/fauxfactory/__init__.py @@ -45,6 +45,22 @@ class FauxFactory(object): Generate random data for your tests. """ + @classmethod + def _is_positive_int(cls, length): + """ + Check that ``length`` argument is a valid integer, greater than zero. + + @type length: int + @param length: The desired length of the string + + @rtype: Exception + @return: A ``ValueError`` exception + """ + + # Validate length argument + if not isinstance(length, int) or length <= 0: + raise ValueError("%s is an invalid \'length\'." % length) + @classmethod def generate_string(cls, str_type, length): """ @@ -99,8 +115,7 @@ def generate_alpha(cls, length=5): """ # Validate length argument - if not isinstance(length, int) or length <= 0: - raise ValueError("%s is an invalid \'length\'." % length) + cls._is_positive_int(length) output_string = u''.join( random.choice(string.ascii_letters) for i in range(length) @@ -121,8 +136,7 @@ def generate_alphanumeric(cls, length=5): """ # Validate length argument - if not isinstance(length, int) or length <= 0: - raise ValueError("%s is an invalid \'length\'." % length) + cls._is_positive_int(length) output_string = u''.join( random.choice( @@ -181,8 +195,7 @@ def generate_cjk(cls, length=5): """ # Validate length argument - if not isinstance(length, int) or length <= 0: - raise ValueError("%s is an invalid \'length\'." % length) + cls._is_positive_int(length) # Generate codepoints, then convert the codepoints to a string. The # valid range of CJK codepoints is 0x4E00 - 0x9FCC, inclusive. Python 2 @@ -360,10 +373,7 @@ def generate_iplum(cls, words=None, paragraphs=None): if not isinstance(words, int) or words < 0: raise ValueError( "Cannot generate a string with negative number of words.") - - if not isinstance(paragraphs, int) or paragraphs <= 0: - raise ValueError( - "Cannot generate a string with negative number of paragraphs.") + cls._is_positive_int(paragraphs) # Original Lorem Ipsum string all_words = LOREM_IPSUM_TEXT.split() @@ -414,8 +424,7 @@ def generate_latin1(cls, length=5): """ # Validate length argument - if not isinstance(length, int) or length <= 0: - raise ValueError("%s is an invalid \'length\'." % length) + cls._is_positive_int(length) range0 = range1 = range2 = [] range0 = ['00C0', '00D6'] @@ -518,8 +527,7 @@ def generate_numeric_string(cls, length=5): """ # Validate length argument - if not isinstance(length, int) or length <= 0: - raise ValueError("%s is an invalid \'length\'." % length) + cls._is_positive_int(length) output_string = u''.join( random.choice(string.digits) for i in range(length) @@ -614,8 +622,7 @@ def generate_utf8(cls, length=5): """ # Validate length argument - if not isinstance(length, int) or length <= 0: - raise ValueError("%s is an invalid \'length\'." % length) + cls._is_positive_int(length) # Generate codepoints. The valid range of UTF-8 codepoints is # 0x0-0x10FFFF, minus the following: 0xC0-0xC1, 0xF5-0xFF and @@ -625,9 +632,10 @@ def generate_utf8(cls, length=5): codepoints = [] while len(codepoints) < length: codepoint = random.randint(0x0, 0x10FFFF) - if codepoint not in range(0xC0, 0xC1 + 1) \ - and codepoint not in range(0xF5, 0xFF + 1) \ - and codepoint not in range(0xD800, 0xDFFF + 1): + if ( + codepoint not in range(0xC0, 0xC1 + 1) + and codepoint not in range(0xF5, 0xFF + 1) + and codepoint not in range(0xD800, 0xDFFF + 1)): codepoints.append(codepoint) # Convert codepoints to characters. Python 2 and 3 support the `unichr`