Trying the Python Secrets Module and a Non-Secret Gist

I have been aware of GitHub Gists for a while, but haven’t used them until now. This post is mainly to see how they work in this WordPress blog. I created a public gist and embedded the URL below.

#!/usr/bin/env python3
# https://docs.python.org/3/library/secrets.html
import secrets
s = secrets.token_urlsafe(15)
n1 = secrets.randbelow(len(s))
n2 = secrets.randbelow(len(s))
a = '~!@#$%^&*()-_+=:;'
a1 = secrets.choice(a)
a2 = secrets.choice(a)
t = s[:n1] + a1 + s[n1:]
t = t[:n2] + a2 + t[n2:]
print(t)
view raw try_secrets.py hosted with ❤ by GitHub

For a code sample, I picked a short Python script from when I was looking at the secrets module. This script generates a random password with some hard-coded attributes. It starts by getting a 15 character string from token_urlsafe (though the final result is not URL-safe). It then inserts two special characters, chosen at random from a sequence, at random positions in the string. If I want different attributes for the password (such as a different length, or different token) I just edit the script.

Looks like a secret gist can be embedded too, but that sure doesn’t keep it secret:

# https://docs.pytest.org/en/stable/reference/reference.html#pytest-raises
import pytest
def f():
raise SystemExit(1)
def test_mytest():
with pytest.raises(SystemExit):
f()
view raw test_sysexit.py hosted with ❤ by GitHub

This code sample is from trying the pytest.raises method.

Hello (again)

I suppose this is a good time of year, with it being cold and gray and uninspiring outside, to spend most of a weekend working on a blog. By working on a blog I don’t mean writing content but rather the installation and configuration of WordPress and some exporting and importing to get content from an old blog. There are a couple posts dated prior to this one that I brought forward from my previous blog at www.bogusoft.com (which is now back to being just a static bit of computer geek satire). I hope to get to posting here soon and not just working on the plumbing of the blog itself. There is, however, more plumbing to do and a few loose ends to tidy up over the next week or so.  I hate to use the tired phrase “under construction” (what web site is not still under construction?) but I suppose that applies.

(To answer my own parenthetical question:  A web site that only exists in the WayBack Machine.)