Creating a random secret key in Raku
This post might seem a bit silly, because I’ve already posted this tip on Twitter about a year ago. Nevertheless, I needed exactly this piece of information recently and it took me a while to find it again. Once I realised it was on Twitter, I thought it’s probably a good idea to mention the tip on my own site as well. Also, I’m not restricted to 140 characters here, so I can be a bit more explicit .
So, the problem is that I needed to create a 50-character random string. In
my particular case this was to set the value of the SECRET_KEY
variable in
a Django configuration at work. Of
course, it’s possible to create the required key using Python from within
Django; the standard solution looks like this:
However, being a Perl/Raku developer at heart, this seemed to be one hell of a lot of typing and much more complicated than such a simple task should be.
Now, I know that with Raku you can pick a number of random elements from a list with the pick routine.1
Therefore, the solution to my problem using the Raku REPL was:
Put simply, this says to take the list of characters between 0
and
z
2, select 50 items at random, join the resulting list
together, and then say the answer. 3 Which is just another
way of saying “give me a string of 50 random characters”. Easy.
This also makes a really simple one-liner:
What a beautifully elegant language!
If you really want the single quotes surrounding the generated string, then something like this should suffice:
Now, I’m sure there must be some way to write the Python code as a one-liner, but I’m also sure that such frivolity would be frowned upon by Python programmers as being “unpythonic”. Oh well.
-
If that seems like a Captain Obvious comment, that’s good, because such things should be obvious. Easy things should be easy; hard things should be possible. Note that this is a language designed by a linguist. ↩
-
The list from
0
toz
contains all digits, upper and lower case ASCII letters as well as various special characters, i.e.:0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz
. ↩ -
Strictly speaking, the
say
isn’t necessary, because the REPL “says” the output by default. Thesay
is, however, necessary in a one-liner. ↩
Support
If you liked this post and want to see more like this, please buy me a coffee!