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
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:]
view raw 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:

import pytest
def f():
raise SystemExit(1)
def test_mytest():
with pytest.raises(SystemExit):
view raw hosted with ❤ by GitHub

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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s