Template language for random string generation

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Aug 8 10:08:51 EDT 2014


Paul Wolf wrote:

> This is a proposal with a working implementation for a random string
> generation template syntax for Python. `strgen` is a module for generating
> random strings in Python using a regex-like template language. Example:
> 
>     >>> from strgen import StringGenerator as SG
>     >>> SG("[\l\d]{8:15}&[\d]&[\p]").render()
>     u'F0vghTjKalf4^mGLk'

Nice! Although very specialised :-)

I second what Ned and Chris have to say.

> If you look at various forums, like Stackoverflow, on how to generate
> random strings with Python, especially for passwords and other hopefully
> secure tokens, you will see dozens of variations of this:
[...]
> There is nothing wrong with this (it's the right answer and is very fast),
> but it leads developers to constantly:
> 
> * Use cryptographically weak methods
> * Forget that the above does not guarantee a result that includes the
>   different classes of characters 
> * Doesn't include variable length or minimum length output
> * It's a lot of typing and the resulting code is vastly different each
>   time making it hard to understand what features were 
>   implemented, especially for those new to the language 
> * You can extend the above to include whatever requirements you want,
>   but it's a constant exercise in wheel reinvention that is extremely
>   verbose, error prone and confusing for exactly the same purposes
>   each time 

So, there's nothing wrong with it, except for the five things you list which
are wrong with it :-)

Seriously, if you're going to compete with the Stackoverflow ad hoc
solutions, you have to be more assertive that there is a problem with the
ad hoc solutions.


-- 
Steven




More information about the Python-list mailing list