Skip to content

Commit

Permalink
Merge pull request anatoly-scherbakov#26 from RomanZorkin/Issue#23_ou…
Browse files Browse the repository at this point in the history
…rselves_random_phrase

Issue anatoly-scherbakov#23 Our own text generator
  • Loading branch information
anatoly-scherbakov authored Sep 9, 2021
2 parents d3f755e + d91aa57 commit 51650cb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions geopatterns_demo/dictionary/adjectives.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
funny
green
hard
nerdy
wild
4 changes: 4 additions & 0 deletions geopatterns_demo/dictionary/nouns.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
programming
Python
clouds
code
11 changes: 11 additions & 0 deletions geopatterns_demo/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
from pathlib import Path

from pydantic import BaseModel

Expand Down Expand Up @@ -35,3 +36,13 @@ class PhraseDescription(BaseModel):
"""

text: str


class Thesaurus(Enum):
"""List of dictionaries with words.
The order of variables determines the order of words in the outgoing phrase.
"""

ADJECTIVES = Path(__file__).parent / 'dictionary/adjectives.txt'
NOUNS = Path(__file__).parent / 'dictionary/nouns.txt'
28 changes: 25 additions & 3 deletions geopatterns_demo/random_phrase.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import loremipsum
import random

from geopatterns_demo.models import Thesaurus


class MakeWord():
"""Class forms a random word from the transmitted file."""

def __init__(self, text_file):
self.text_file = text_file

def random_word(self):
"""Find random word in text file."""
with open(self.text_file, 'r') as text:
lines = text.readlines()
# random.choice() is considered to be insecure by flake8-bandit,
# but we're not using this for any kind of cryptographic
# or security related purpose.
return random.choice(lines).rstrip('\n') # noqa: S311


def make_random_text():
"""Method create random phrase using loremipsum library."""
return loremipsum.generate(1, 'short')
"""Glues and return a phrase from random words."""
words_list = [
MakeWord(file_path.value).random_word()
for file_path in Thesaurus
]
return str(' '.join(words_list))

0 comments on commit 51650cb

Please # to comment.