TextGenerator now supports a single string as a part
This commit is contained in:
parent
d5163a846a
commit
483226d2ad
1 changed files with 7 additions and 2 deletions
|
@ -10,7 +10,6 @@ class TextGenerator(object):
|
||||||
self.default_templates = default_templates
|
self.default_templates = default_templates
|
||||||
self.parts = parts
|
self.parts = parts
|
||||||
self.variables = variables
|
self.variables = variables
|
||||||
print self.variables
|
|
||||||
|
|
||||||
def generate_string(self, template=None):
|
def generate_string(self, template=None):
|
||||||
"""
|
"""
|
||||||
|
@ -22,6 +21,7 @@ class TextGenerator(object):
|
||||||
else:
|
else:
|
||||||
text = random.choice(self.templates)
|
text = random.choice(self.templates)
|
||||||
|
|
||||||
|
# replace static variables in the template with provided values
|
||||||
if self.variables:
|
if self.variables:
|
||||||
for key, value in self.variables.items():
|
for key, value in self.variables.items():
|
||||||
text = text.replace("{%s}" % key, value)
|
text = text.replace("{%s}" % key, value)
|
||||||
|
@ -30,7 +30,12 @@ class TextGenerator(object):
|
||||||
required_parts = TEMPLATE_RE.findall(text)
|
required_parts = TEMPLATE_RE.findall(text)
|
||||||
|
|
||||||
for required_part in required_parts:
|
for required_part in required_parts:
|
||||||
part = random.choice(self.parts[required_part])
|
_part = self.parts[required_part]
|
||||||
|
# check if the part is a single string or a list
|
||||||
|
if not isinstance(_part, basestring):
|
||||||
|
part = random.choice(self.parts[_part])
|
||||||
|
else:
|
||||||
|
part = _part
|
||||||
text = text.replace("{%s}" % required_part, part)
|
text = text.replace("{%s}" % required_part, part)
|
||||||
|
|
||||||
return text
|
return text
|
||||||
|
|
Reference in a new issue