<spanid="evennia-contrib-random-string-generator"></span><h1>evennia.contrib.random_string_generator<aclass="headerlink"href="#module-evennia.contrib.random_string_generator"title="Permalink to this headline">¶</a></h1>
<p>Pseudo-random generator and registry</p>
<p>Evennia contribution - Vincent Le Goff 2017</p>
<p>This contrib can be used to generate pseudo-random strings of information
with specific criteria. You could, for instance, use it to generate
<spanclass="c1"># The number can be generated again</span>
</pre></div>
</div>
<p>To use it, you will need to:</p>
<olclass="arabic simple">
<li><p>Import the <strong>RandomStringGenerator</strong> class from the contrib.</p></li>
<li><p>Create an instance of this class taking two arguments:
- The name of the gemerator (like “phone number”, “license plate”…).
- The regular expression representing the expected results.</p></li>
<li><p>Use the generator’s <strong>all</strong>, <strong>get</strong> and <strong>remove</strong> methods as shown above.</p></li>
</ol>
<p>To understand how to read and create regular expressions, you can refer to
[the documentation on the re module](<aclass="reference external"href="https://docs.python.org/2/library/re.html">https://docs.python.org/2/library/re.html</a>).
Some examples of regular expressions you could use:</p>
<ulclass="simple">
<li><p><strong>r”555-d{3}-d{4}”</strong>: 555, a dash, 3 digits, another dash, 4 digits.</p></li>
<li><p><strong>r”[0-9]{3}[A-Z][0-9]{3}”</strong>: 3 digits, a capital letter, 3 digits.</p></li>
<li><p><strong>r”[A-Za-z0-9]{8,15}”</strong>: between 8 and 15 letters and digits.</p></li>
<li><p>…</p></li>
</ul>
<p>Behind the scenes, a script is created to store the generated information
for a single generator. The <strong>RandomStringGenerator</strong> object will also
read the regular expression you give to it to see what information is
required (letters, digits, a more restricted class, simple characters…)…
More complex regular expressions (with branches for instance) might not be
<emclass="property">exception </em><codeclass="sig-prename descclassname">evennia.contrib.random_string_generator.</code><codeclass="sig-name descname">RejectedRegex</code><aclass="reference internal"href="../_modules/evennia/contrib/random_string_generator.html#RejectedRegex"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.random_string_generator.RejectedRegex"title="Permalink to this definition">¶</a></dt>
<emclass="property">exception </em><codeclass="sig-prename descclassname">evennia.contrib.random_string_generator.</code><codeclass="sig-name descname">ExhaustedGenerator</code><aclass="reference internal"href="../_modules/evennia/contrib/random_string_generator.html#ExhaustedGenerator"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.random_string_generator.ExhaustedGenerator"title="Permalink to this definition">¶</a></dt>
<emclass="property">class </em><codeclass="sig-prename descclassname">evennia.contrib.random_string_generator.</code><codeclass="sig-name descname">RandomStringGeneratorScript</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="o">*</span><spanclass="n">args</span></em>, <emclass="sig-param"><spanclass="o">**</span><spanclass="n">kwargs</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/contrib/random_string_generator.html#RandomStringGeneratorScript"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.random_string_generator.RandomStringGeneratorScript"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">at_script_creation</code><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/contrib/random_string_generator.html#RandomStringGeneratorScript.at_script_creation"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.random_string_generator.RandomStringGeneratorScript.at_script_creation"title="Permalink to this definition">¶</a></dt>
<dd><p>Hook called when the script is created.</p>
<emclass="property">exception </em><codeclass="sig-name descname">DoesNotExist</code><aclass="headerlink"href="#evennia.contrib.random_string_generator.RandomStringGeneratorScript.DoesNotExist"title="Permalink to this definition">¶</a></dt>
<emclass="property">exception </em><codeclass="sig-name descname">MultipleObjectsReturned</code><aclass="headerlink"href="#evennia.contrib.random_string_generator.RandomStringGeneratorScript.MultipleObjectsReturned"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">path</code><emclass="property"> = 'evennia.contrib.random_string_generator.RandomStringGeneratorScript'</em><aclass="headerlink"href="#evennia.contrib.random_string_generator.RandomStringGeneratorScript.path"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">typename</code><emclass="property"> = 'RandomStringGeneratorScript'</em><aclass="headerlink"href="#evennia.contrib.random_string_generator.RandomStringGeneratorScript.typename"title="Permalink to this definition">¶</a></dt>
<emclass="property">class </em><codeclass="sig-prename descclassname">evennia.contrib.random_string_generator.</code><codeclass="sig-name descname">RandomStringGenerator</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n">name</span></em>, <emclass="sig-param"><spanclass="n">regex</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/contrib/random_string_generator.html#RandomStringGenerator"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.random_string_generator.RandomStringGenerator"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">script</code><emclass="property"> = None</em><aclass="headerlink"href="#evennia.contrib.random_string_generator.RandomStringGenerator.script"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">__init__</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n">name</span></em>, <emclass="sig-param"><spanclass="n">regex</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/contrib/random_string_generator.html#RandomStringGenerator.__init__"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.random_string_generator.RandomStringGenerator.__init__"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">all</code><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/contrib/random_string_generator.html#RandomStringGenerator.all"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.random_string_generator.RandomStringGenerator.all"title="Permalink to this definition">¶</a></dt>
<dd><p>Return all generated strings for this generator.</p>
<dlclass="field-list simple">
<dtclass="field-odd">Returns</dt>
<ddclass="field-odd"><p><em>strings (list of strr)</em>– the list of strings that are already
used. The strings that were generated first come first in the list.</p>
<codeclass="sig-name descname">get</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n">store</span><spanclass="o">=</span><spanclass="default_value">True</span></em>, <emclass="sig-param"><spanclass="n">unique</span><spanclass="o">=</span><spanclass="default_value">True</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/contrib/random_string_generator.html#RandomStringGenerator.get"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.random_string_generator.RandomStringGenerator.get"title="Permalink to this definition">¶</a></dt>
<ddclass="field-odd"><p><aclass="reference internal"href="#evennia.contrib.random_string_generator.ExhaustedGenerator"title="evennia.contrib.random_string_generator.ExhaustedGenerator"><strong>ExhaustedGenerator</strong></a>– if there’s no available string in this generator.</p>
</dd>
</dl>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>Unless asked explicitly, the returned string can’t repeat itself.</p>
<codeclass="sig-name descname">remove</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n">element</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/contrib/random_string_generator.html#RandomStringGenerator.remove"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.random_string_generator.RandomStringGenerator.remove"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">clear</code><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/contrib/random_string_generator.html#RandomStringGenerator.clear"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.random_string_generator.RandomStringGenerator.clear"title="Permalink to this definition">¶</a></dt>
<dd><p>Clear the generator of all generated strings.</p>