textworld.utils

class textworld.utils.RandomGenerator(seed=None)[source]

Bases: object

Random generator controlling the games generation.

next()[source]

Start a new random generator using a new seed.

set_seed(seed)[source]
property seed
class textworld.utils.RegexDict[source]

Bases: OrderedDict

Ordered dictionary that supports querying with regex.

References

Adapted from https://stackoverflow.com/questions/21024822/python-accessing-dictionary-with-wildcards.

get_matching(*regexes, exclude=[])[source]

Query the dictionary using one or several regular expressions.

Parameters
  • *regexes – List of regular expressions determining which keys of this dictionary are relevant to this query.

  • exclude (List[str]) – List of regular expressions determining which keys of this dictionary should be excluded from this query.

Return type

List[Any]

Returns

The value associated to each relevant (and not excluded) keys.

textworld.utils.check_modules(required_modules, missing_modules)[source]

Check whether some required modules are missing.

textworld.utils.chunk(iterable, n, fct=<function <lambda>>)[source]
Return type

Iterable[Iterable]

textworld.utils.encode_seeds(seeds)[source]

Generate UID from a list of seeds.

textworld.utils.make_temp_directory(suffix='', prefix='tw_', dir=None)[source]

Create temporary folder to used in a with statement.

textworld.utils.maybe_mkdir(dirpath)[source]

Create all parent folders if needed.

textworld.utils.save_graph_to_svg(G, labels, filename, backward=False)[source]

Generate a figure of a networkx’s graph object and save it.

textworld.utils.str2bool(v)[source]

Convert string to a boolean value. .. rubric:: References

https://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python/715468#715468

textworld.utils.take(n, iterable)[source]

Return first n items of the iterable as a list.

References

https://docs.python.org/3/library/itertools.html#itertools-recipes

Return type

Iterable

textworld.utils.unique_product(*iterables)[source]

Cartesian product of input iterables with pruning.

This method prunes any product tuple with duplicate elements in it.

Example

unique_product(‘ABC’, ‘Ax’, ‘xy’) –> Axy BAx BAy Bxy CAx CAy Cxy

Notes

This method is faster than the following equivalent code:

>>> for result in itertools.product(*args):
>>>     if len(set(result)) == len(result):
>>>         yield result
textworld.utils.uniquify(seq)[source]

Order preserving uniquify.

References

Made by Dave Kirby https://www.peterbe.com/plog/uniqifiers-benchmark

textworld.utils.g_rng = <textworld.utils.RandomGenerator object>

Global random generator.