Skip to content

Instantly share code, notes, and snippets.

View rswift's full-sized avatar
👨‍💻

Robert Swift rswift

👨‍💻
View GitHub Profile
@rswift
rswift / email-regex.txt
Last active September 13, 2023 08:54
an imperfect but simple email regex for CloudFormation AllowedPattern
^\b([a-zA-Z0-9][\.\-_a-zA-Z0-9]+)\b@\b([a-zA-Z0-9][\-_a-zA-Z0-9]+)\b\.{1}\b(.*)\b$
#
# not massively well tested, or spectacular, but it works for generally agreeable, mostly typical user@domain.tld style addresses
#
#
# passing
#
user@domain.co.uk
@rswift
rswift / day_ordinal.py
Created August 19, 2022 09:13
Determine a day of the month ordinal...
def day_ordinal(day: int) -> str:
"""For a given day of the month, return its ordinal 📅"""
if 4 <= day <= 20 or 24 <= day <= 30:
return "th"
else:
return ["st", "nd", "rd"][day % 10 - 1]
for day in range(1, 31+1):
print(f"{day}{day_ordinal(day)}")
#
# Specifically linked to eu-west-2 so tweak as necessary
#
^arn:aws:sns:eu-west-2:\d{12}:([\w\-]{1,256}|[\w\-]{1,251}\.fifo)$
#
# passing
#
arn:aws:sns:eu-west-2:123456789012:MyTopic
arn:aws:sns:eu-west-2:123456789012:mytopic
@rswift
rswift / plural.py
Created October 9, 2021 12:33
simple plurals in python...
#
# hat tip: https://stackoverflow.com/users/173003/aristide via https://stackoverflow.com/a/65063284/259122
#
# shamelessly copied and included here for easier self-location!
#
for i in range(5):
print(f"{i} bottle{'s'[:i^1]} of beer.")
@rswift
rswift / window_size_info.html
Last active September 2, 2021 08:36
A trivial web page that can be loaded as file:///window_size_info.html to dynamically display the screen height and width, useful for making a window a specific size for screen grabs etc. without needing to add an extension to ones browser... Has this been Testing'd? In a word. No. It works fine on Firefox & Safari, not tried it with other brows…
<!DOCTYPE html>
<html lang="en-GB">
<head>
<title>🖖</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- favicon idea stolen! https://twitter.com/LeaVerou/status/1241619866475474946 -->
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📺</text></svg>">
<style>
html, body {
#
# Specifically linked to eu-west-2 so tweak as necessary
#
arn:aws:s3:eu-west-2:\d{12}:(?=.{3,63}$)(?!(\d+\.)+\d+$)((([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])$)
#
# passing
#
arn:aws:s3:eu-west-2:123456789012:abc
arn:aws:s3:eu-west-2:123456789012:abcd
@rswift
rswift / lambda_function.py
Last active September 16, 2022 19:50
AWS Lambda function to log salient details then exit...
import logging
import os
log_level = os.environ['LOG_LEVEL'] if 'LOG_LEVEL' in os.environ else 'INFO'
logging_levels = {"DEBUG": logging.DEBUG, "INFO": logging.INFO, "WARNING": logging.WARNING, "ERROR": logging.ERROR, "CRITICAL": logging.CRITICAL} # https://docs.python.org/3/library/logging.html#levels
logging_level = logging_levels[log_level] if log_level in logging_levels else logging.INFO
logger = logging.getLogger()
logger.setLevel(logging_level)
@rswift
rswift / unkown_root_ca_tinkering.py
Last active October 10, 2023 13:20
Trivial script to explore only using built-in Python 3 modules to establish a connection to a host that issues certs signed by a CA that isn't well known...
#
# Trivial script to explore only using built-in Python 3 modules to establish a connection to
# a host that issues certs signed by a CA that isn't well known...
#
# It does require that the root cert is available, so primarily an enterprise that has an
# internal or private CA and signs server certs with that...
#
import urllib.request
import ssl