def gen_iplum(words=None, paragraphs=None): """Return a lorem ipsum string. If no arguments are passed, then return the entire default lorem ipsum string. :param int words: The number of words to return. :param int paragraphs: The number of paragraphs to return. :raises: ``ValueError`` if ``words`` is not a valid positive integer. :returns: A ``lorem ipsum`` string containing either the number of ``words`` or ``paragraphs``, extending and wrapping around the text as needed to make sure that it has the specified length. :rtype: str """ # Check parameters if words is None or words == 0: words = len(LOREM_IPSUM_TEXT.split()) if paragraphs is None: paragraphs = 1 if not isinstance(words, int) or words < 0: raise ValueError( 'Cannot generate a string with negative number of words.') is_positive_int(paragraphs) # Original Lorem Ipsum string all_words = LOREM_IPSUM_TEXT.split() # How many words do we need? total_words_needed = words * paragraphs quotient = int(total_words_needed / len(all_words)) modulus = total_words_needed % len(all_words) # Pool of words to use all_words = all_words * (quotient + modulus) result = u'' start_pos = 0 for _ in range(0, paragraphs): sentence = u' '.join(all_words[start_pos:start_pos + words]) # Remove comma from the end, if it exists if sentence.endswith(','): sentence = sentence.rstrip(',') # Remove period from the end, if it exists if sentence.endswith('.'): sentence = sentence.rstrip('.') # Each sentence should be properly capitalized cap_sentence = [ frag.capitalize() + u'.' for frag in sentence.split('. ') ] # Add newline at the end result += ' '.join(cap_sentence) + u'\n' # Increment positional counter start_pos += words return result.rstrip()
def gen_iplum(words=None, paragraphs=None): """Returns a lorem ipsum string. If no arguments are passed, then return the entire default lorem ipsum string. :param int words: The number of words to return. :param int paragraphs: The number of paragraphs to return. :raises: ``ValueError`` if ``words`` is not a valid positive integer. :returns: A ``lorem ipsum`` string containing either the number of ``words`` or ``paragraphs``, extending and wrapping around the text as needed to make sure that it has the specified length. :rtype: str """ # Check parameters if words is None or words == 0: words = len(LOREM_IPSUM_TEXT.split()) if paragraphs is None: paragraphs = 1 if not isinstance(words, int) or words < 0: raise ValueError( "Cannot generate a string with negative number of words.") _is_positive_int(paragraphs) # Original Lorem Ipsum string all_words = LOREM_IPSUM_TEXT.split() # How many words do we need? total_words_needed = words * paragraphs quotient = int(total_words_needed / len(all_words)) modulus = total_words_needed % len(all_words) # Pool of words to use all_words = all_words * (quotient + modulus) result = u"" start_pos = 0 for _ in range(0, paragraphs): sentence = u" ".join( all_words[start_pos:start_pos + words]) # Remove comma from the end, if it exists if sentence.endswith(','): sentence = sentence.rstrip(',') # Remove period from the end, if it exists if sentence.endswith('.'): sentence = sentence.rstrip('.') # Each sentence should be properly capitalized cap_sentence = [ frag.capitalize() + u'.' for frag in sentence.split('. ')] # Add newline at the end result += " ".join(cap_sentence) + u"\n" # Increment positional counter start_pos += words return _make_unicode(result.rstrip())
def generate_iplum(cls, words=None, paragraphs=None): """ Returns a lorem ipsum string. If no arguments are passed, then return the entire default lorem ipsum string. @rtype words: int @param words: How many words to return. @rtype paragraphs: int @param paragraphs: How many paragraphs to return. @rtype string @return: A lorem ipsum string. """ # Check parameters if words is None or words == 0: words = len(LOREM_IPSUM_TEXT.split()) if paragraphs is None: paragraphs = 1 if not isinstance(words, int) or words < 0: raise ValueError( "Cannot generate a string with negative number of words.") cls._is_positive_int(paragraphs) # Original Lorem Ipsum string all_words = LOREM_IPSUM_TEXT.split() # How many words do we need? total_words_needed = words * paragraphs quotient = int(total_words_needed / len(all_words)) modulus = total_words_needed % len(all_words) # Pool of words to use all_words = all_words * (quotient + modulus) result = u"" start_pos = 0 for idx in range(0, paragraphs): sentence = u" ".join( all_words[start_pos:start_pos + words]) # Remove comma from the end, if it exists if sentence.endswith(','): sentence = sentence.rstrip(',') # Remove period from the end, if it exists if sentence.endswith('.'): sentence = sentence.rstrip('.') # Each sentence should be properly capitalized cap_sentence = [ frag.capitalize() + u'.' for frag in sentence.split('. ')] # Add newline at the end result += " ".join(cap_sentence) + u"\n" # Increment positional counter start_pos += words return codify(result.rstrip())