mirror of
https://github.com/evennia/evennia.git
synced 2026-03-18 22:06:30 +01:00
Update the generator module with new method names
This commit is contained in:
parent
8c7acba151
commit
0fdcf89c32
2 changed files with 101 additions and 101 deletions
|
|
@ -15,12 +15,12 @@ from evennia.contrib.generator import Generator
|
|||
# Create a generator for phone numbers
|
||||
phone_generator = Generator("phone number", r"555-\d{3}-\d{4}")
|
||||
# Generate a phone number (555-XXX-XXXX with X as numbers)
|
||||
number = phone_generator.generate()
|
||||
number = phone_generator.get()
|
||||
# `number` will contain something like: "555-981-2207"
|
||||
# If you call `phone_generator.generate`, it won't give the same anymore.
|
||||
# If you call `phone_generator.get`, it won't give the same anymore.
|
||||
phone_generator.all()
|
||||
# Will return a list of all currently-used phone numbers
|
||||
phone_generator.free("555-981-2207")
|
||||
phone_generator.remove("555-981-2207")
|
||||
# The number can be generated again
|
||||
```
|
||||
|
||||
|
|
@ -30,7 +30,7 @@ To use it, you will need to:
|
|||
2. Create an instance of this class taking two arguments:
|
||||
- Tje name of tje gemerator (like "phone number", "license plate"...).
|
||||
- The regular expression representing the expected results.
|
||||
3. Use the generator's `all`, `generate` and `free` methods as shown above.
|
||||
3. Use the generator's `all`, `get` and `remove` methods as shown above.
|
||||
|
||||
Some examples of regular expressions you could use:
|
||||
|
||||
|
|
@ -103,7 +103,7 @@ class Generator(object):
|
|||
The "rule" defining what the generator should provide in terms of
|
||||
string is given as a regular expression when creating instances of
|
||||
this class. You can use the `all` method to get all generated strings,
|
||||
the `generate` method to generate a new string, the `free` method
|
||||
the `get` method to generate a new string, the `remove` method
|
||||
to remove a generated string, or the `clear` method to remove all
|
||||
generated strings.
|
||||
|
||||
|
|
@ -155,100 +155,6 @@ class Generator(object):
|
|||
def __repr__(self):
|
||||
return "<evennia.contrib.generator.Generator for {}>".format(self.name)
|
||||
|
||||
def all(self):
|
||||
"""
|
||||
Return all generated strings for this generator.
|
||||
|
||||
Returns:
|
||||
strings (list of strr): the list of strings that are already
|
||||
used. The strings that were generated first come first in the list.
|
||||
|
||||
"""
|
||||
script = self._get_script()
|
||||
generated = list(script.db.generated.get(self.name, []))
|
||||
return generated
|
||||
|
||||
def generate(self, store=True, keep_trying=True):
|
||||
"""
|
||||
Generate a pseudo-random string according to the regular expression.
|
||||
|
||||
Args:
|
||||
store (bool, optional): store the generated string in the script.
|
||||
keep_trying (bool, optional): keep on trying if the string is already used.
|
||||
|
||||
Returns:
|
||||
The newly-generated string.
|
||||
|
||||
Raises:
|
||||
ExhaustedGenerator: if there's no available string in this generator.
|
||||
|
||||
Note:
|
||||
Unless asked explicitly, the returned string can't repeat itself.
|
||||
|
||||
"""
|
||||
script = self._get_script()
|
||||
generated = script.db.generated.get(self.name)
|
||||
if generated is None:
|
||||
script.db.generated[self.name] = []
|
||||
generated = script.db.generated[self.name]
|
||||
|
||||
if len(generated) >= self.total:
|
||||
raise ExhaustedGenerator
|
||||
|
||||
# Generate a pseudo-random string that might be used already
|
||||
result = ""
|
||||
for element in self.elements:
|
||||
number = randint(element["min"], element["max"])
|
||||
chars = element["chars"]
|
||||
for index in range(number):
|
||||
char = choice(chars)
|
||||
result += char
|
||||
|
||||
# If the string has already been generated, try again
|
||||
if result in generated and keep_trying:
|
||||
# Change the random seed, incrementing it slowly
|
||||
epoch = time.time()
|
||||
while result in generated:
|
||||
epoch += 1
|
||||
seed(epoch)
|
||||
result = self.generate(store=False, keep_trying=False)
|
||||
|
||||
if store:
|
||||
generated.append(result)
|
||||
|
||||
return result
|
||||
|
||||
def free(self, element):
|
||||
"""
|
||||
Remove a generated string from the list of stored strings.
|
||||
|
||||
Args:
|
||||
element (str): the string to remove from the list of generated strings.
|
||||
|
||||
Note:
|
||||
The specified string has to be present in the script (so
|
||||
has to have been generated). It will remove this entry
|
||||
from the script, so this string could be generated again by
|
||||
calling the `generate` method.
|
||||
|
||||
"""
|
||||
script = self._get_script()
|
||||
generated = script.db.generated.get(self.name, [])
|
||||
if element not in generated:
|
||||
raise ValueError("the string {} isn't stored as generated by the generator {}".format(
|
||||
element, self.name))
|
||||
|
||||
generated.remove(element)
|
||||
|
||||
def clear(self):
|
||||
"""
|
||||
Clear the generator of all generated strings.
|
||||
|
||||
"""
|
||||
script = self._get_script()
|
||||
generated = script.db.generated.get(self.name, [])
|
||||
generated[:] = []
|
||||
|
||||
def _get_script(self):
|
||||
"""Get or create the script."""
|
||||
if type(self).script:
|
||||
|
|
@ -338,3 +244,97 @@ class Generator(object):
|
|||
raise RejectedRegex("cannot find the literal: {}".format(element[0]))
|
||||
|
||||
return chars
|
||||
|
||||
def all(self):
|
||||
"""
|
||||
Return all generated strings for this generator.
|
||||
|
||||
Returns:
|
||||
strings (list of strr): the list of strings that are already
|
||||
used. The strings that were generated first come first in the list.
|
||||
|
||||
"""
|
||||
script = self._get_script()
|
||||
generated = list(script.db.generated.get(self.name, []))
|
||||
return generated
|
||||
|
||||
def get(self, store=True, keep_trying=True):
|
||||
"""
|
||||
Generate a pseudo-random string according to the regular expression.
|
||||
|
||||
Args:
|
||||
store (bool, optional): store the generated string in the script.
|
||||
keep_trying (bool, optional): keep on trying if the string is already used.
|
||||
|
||||
Returns:
|
||||
The newly-generated string.
|
||||
|
||||
Raises:
|
||||
ExhaustedGenerator: if there's no available string in this generator.
|
||||
|
||||
Note:
|
||||
Unless asked explicitly, the returned string can't repeat itself.
|
||||
|
||||
"""
|
||||
script = self._get_script()
|
||||
generated = script.db.generated.get(self.name)
|
||||
if generated is None:
|
||||
script.db.generated[self.name] = []
|
||||
generated = script.db.generated[self.name]
|
||||
|
||||
if len(generated) >= self.total:
|
||||
raise ExhaustedGenerator
|
||||
|
||||
# Generate a pseudo-random string that might be used already
|
||||
result = ""
|
||||
for element in self.elements:
|
||||
number = randint(element["min"], element["max"])
|
||||
chars = element["chars"]
|
||||
for index in range(number):
|
||||
char = choice(chars)
|
||||
result += char
|
||||
|
||||
# If the string has already been generated, try again
|
||||
if result in generated and keep_trying:
|
||||
# Change the random seed, incrementing it slowly
|
||||
epoch = time.time()
|
||||
while result in generated:
|
||||
epoch += 1
|
||||
seed(epoch)
|
||||
result = self.get(store=False, keep_trying=False)
|
||||
|
||||
if store:
|
||||
generated.append(result)
|
||||
|
||||
return result
|
||||
|
||||
def remove(self, element):
|
||||
"""
|
||||
Remove a generated string from the list of stored strings.
|
||||
|
||||
Args:
|
||||
element (str): the string to remove from the list of generated strings.
|
||||
|
||||
Note:
|
||||
The specified string has to be present in the script (so
|
||||
has to have been generated). It will remove this entry
|
||||
from the script, so this string could be generated again by
|
||||
calling the `get` method.
|
||||
|
||||
"""
|
||||
script = self._get_script()
|
||||
generated = script.db.generated.get(self.name, [])
|
||||
if element not in generated:
|
||||
raise ValueError("the string {} isn't stored as generated by the generator {}".format(
|
||||
element, self.name))
|
||||
|
||||
generated.remove(element)
|
||||
|
||||
def clear(self):
|
||||
"""
|
||||
Clear the generator of all generated strings.
|
||||
|
||||
"""
|
||||
script = self._get_script()
|
||||
generated = script.db.generated.get(self.name, [])
|
||||
generated[:] = []
|
||||
|
|
|
|||
|
|
@ -997,7 +997,7 @@ class TestGenerator(EvenniaTest):
|
|||
"""Generate and fail when exhausted."""
|
||||
generated = []
|
||||
for i in range(4):
|
||||
generated.append(SIMPLE_GENERATOR.generate())
|
||||
generated.append(SIMPLE_GENERATOR.get())
|
||||
|
||||
generated.sort()
|
||||
self.assertEqual(generated, ["00", "01", "10", "11"])
|
||||
|
|
@ -1005,4 +1005,4 @@ class TestGenerator(EvenniaTest):
|
|||
# At this point, we have generated 4 strings.
|
||||
# We can't generate one more
|
||||
with self.assertRaises(generator.ExhaustedGenerator):
|
||||
SIMPLE_GENERATOR.generate()
|
||||
SIMPLE_GENERATOR.get()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue