Skip to content

Instantly share code, notes, and snippets.

@smeech
Last active January 31, 2025 17:32
Show Gist options
  • Save smeech/933f2330ba0af401b440a65770398703 to your computer and use it in GitHub Desktop.
Save smeech/933f2330ba0af401b440a65770398703 to your computer and use it in GitHub Desktop.
[Password generator] A simple password generator written while reflecting on an Espanso Hub submission. Called by `:genpass()` for a twelve-character password, or `:genpass(n)` for an n-length password. #espanso #python #regex
# A simple password generator written while reflecting on an Espanso Hub submission
# Called by `:genpass()` for a twelve-character password, or
# `:genpass(n)` for an n-length password.
# The script will add the result to the clipboard for pasting elsewhere,
# if `pyperclip` is installed
matches:
- regex: :genpass\((?P<myvar>\d*)\)
replace: "{{output}}"
vars:
- name: output
type: script
params:
args:
- python
- -c
- |
import random, string
characters = string.ascii_letters + string.punctuation + string.digits
n = max(4, int('{{myvar}}') if '{{myvar}}'.isdigit() else 12)
while True:
password = ''.join(random.choice(characters) for _ in range(n))
if (any(c.islower() for c in password) and any(c.isupper() for c in password) and any(c in string.punctuation for c in password) and any(c.isdigit() for c in password)):
break
try:
import pyperclip
pyperclip.copy(password)
except:
pass
print(password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment