TextGenerator now supports a single string as a part

This commit is contained in:
Luke Rogers 2013-09-16 13:26:51 +12:00
parent d5163a846a
commit 483226d2ad

View file

@ -10,7 +10,6 @@ class TextGenerator(object):
self.default_templates = default_templates
self.parts = parts
self.variables = variables
print self.variables
def generate_string(self, template=None):
"""
@ -22,6 +21,7 @@ class TextGenerator(object):
else:
text = random.choice(self.templates)
# replace static variables in the template with provided values
if self.variables:
for key, value in self.variables.items():
text = text.replace("{%s}" % key, value)
@ -30,7 +30,12 @@ class TextGenerator(object):
required_parts = TEMPLATE_RE.findall(text)
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)
return text