You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lorem_ipsum.py 4.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. Utility functions for generating "lorem ipsum" Latin text.
  3. """
  4. from __future__ import unicode_literals
  5. import random
  6. COMMON_P = (
  7. 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod '
  8. 'tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim '
  9. 'veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea '
  10. 'commodo consequat. Duis aute irure dolor in reprehenderit in voluptate '
  11. 'velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint '
  12. 'occaecat cupidatat non proident, sunt in culpa qui officia deserunt '
  13. 'mollit anim id est laborum.'
  14. )
  15. WORDS = ('exercitationem', 'perferendis', 'perspiciatis', 'laborum', 'eveniet',
  16. 'sunt', 'iure', 'nam', 'nobis', 'eum', 'cum', 'officiis', 'excepturi',
  17. 'odio', 'consectetur', 'quasi', 'aut', 'quisquam', 'vel', 'eligendi',
  18. 'itaque', 'non', 'odit', 'tempore', 'quaerat', 'dignissimos',
  19. 'facilis', 'neque', 'nihil', 'expedita', 'vitae', 'vero', 'ipsum',
  20. 'nisi', 'animi', 'cumque', 'pariatur', 'velit', 'modi', 'natus',
  21. 'iusto', 'eaque', 'sequi', 'illo', 'sed', 'ex', 'et', 'voluptatibus',
  22. 'tempora', 'veritatis', 'ratione', 'assumenda', 'incidunt', 'nostrum',
  23. 'placeat', 'aliquid', 'fuga', 'provident', 'praesentium', 'rem',
  24. 'necessitatibus', 'suscipit', 'adipisci', 'quidem', 'possimus',
  25. 'voluptas', 'debitis', 'sint', 'accusantium', 'unde', 'sapiente',
  26. 'voluptate', 'qui', 'aspernatur', 'laudantium', 'soluta', 'amet',
  27. 'quo', 'aliquam', 'saepe', 'culpa', 'libero', 'ipsa', 'dicta',
  28. 'reiciendis', 'nesciunt', 'doloribus', 'autem', 'impedit', 'minima',
  29. 'maiores', 'repudiandae', 'ipsam', 'obcaecati', 'ullam', 'enim',
  30. 'totam', 'delectus', 'ducimus', 'quis', 'voluptates', 'dolores',
  31. 'molestiae', 'harum', 'dolorem', 'quia', 'voluptatem', 'molestias',
  32. 'magni', 'distinctio', 'omnis', 'illum', 'dolorum', 'voluptatum', 'ea',
  33. 'quas', 'quam', 'corporis', 'quae', 'blanditiis', 'atque', 'deserunt',
  34. 'laboriosam', 'earum', 'consequuntur', 'hic', 'cupiditate',
  35. 'quibusdam', 'accusamus', 'ut', 'rerum', 'error', 'minus', 'eius',
  36. 'ab', 'ad', 'nemo', 'fugit', 'officia', 'at', 'in', 'id', 'quos',
  37. 'reprehenderit', 'numquam', 'iste', 'fugiat', 'sit', 'inventore',
  38. 'beatae', 'repellendus', 'magnam', 'recusandae', 'quod', 'explicabo',
  39. 'doloremque', 'aperiam', 'consequatur', 'asperiores', 'commodi',
  40. 'optio', 'dolor', 'labore', 'temporibus', 'repellat', 'veniam',
  41. 'architecto', 'est', 'esse', 'mollitia', 'nulla', 'a', 'similique',
  42. 'eos', 'alias', 'dolore', 'tenetur', 'deleniti', 'porro', 'facere',
  43. 'maxime', 'corrupti')
  44. COMMON_WORDS = ('lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur',
  45. 'adipisicing', 'elit', 'sed', 'do', 'eiusmod', 'tempor', 'incididunt',
  46. 'ut', 'labore', 'et', 'dolore', 'magna', 'aliqua')
  47. def sentence():
  48. """
  49. Returns a randomly generated sentence of lorem ipsum text.
  50. The first word is capitalized, and the sentence ends in either a period or
  51. question mark. Commas are added at random.
  52. """
  53. # Determine the number of comma-separated sections and number of words in
  54. # each section for this sentence.
  55. sections = [' '.join(random.sample(WORDS, random.randint(3, 12))) for i in range(random.randint(1, 5))]
  56. s = ', '.join(sections)
  57. # Convert to sentence case and add end punctuation.
  58. return '%s%s%s' % (s[0].upper(), s[1:], random.choice('?.'))
  59. def paragraph():
  60. """
  61. Returns a randomly generated paragraph of lorem ipsum text.
  62. The paragraph consists of between 1 and 4 sentences, inclusive.
  63. """
  64. return ' '.join(sentence() for i in range(random.randint(1, 4)))
  65. def paragraphs(count, common=True):
  66. """
  67. Returns a list of paragraphs as returned by paragraph().
  68. If `common` is True, then the first paragraph will be the standard
  69. 'lorem ipsum' paragraph. Otherwise, the first paragraph will be random
  70. Latin text. Either way, subsequent paragraphs will be random Latin text.
  71. """
  72. paras = []
  73. for i in range(count):
  74. if common and i == 0:
  75. paras.append(COMMON_P)
  76. else:
  77. paras.append(paragraph())
  78. return paras
  79. def words(count, common=True):
  80. """
  81. Returns a string of `count` lorem ipsum words separated by a single space.
  82. If `common` is True, then the first 19 words will be the standard
  83. 'lorem ipsum' words. Otherwise, all words will be selected randomly.
  84. """
  85. if common:
  86. word_list = list(COMMON_WORDS)
  87. else:
  88. word_list = []
  89. c = len(word_list)
  90. if count > c:
  91. count -= c
  92. while count > 0:
  93. c = min(count, len(WORDS))
  94. count -= c
  95. word_list += random.sample(WORDS, c)
  96. else:
  97. word_list = word_list[:count]
  98. return ' '.join(word_list)