Skip to content

Instantly share code, notes, and snippets.

View r-leyshon's full-sized avatar

Richard Leyshon r-leyshon

  • Ministry of Justice
  • Wales, United Kingdom
View GitHub Profile
@r-leyshon
r-leyshon / poem.py
Last active November 4, 2024 06:36
Mock with fixture
from datetime import datetime
def get_poem_line_for_day():
"""Returns the line of the poem based on the current day of the week."""
day_of_week = datetime.today().strftime('%A')
POEM = {
"Monday": "Monday's child is fair of face",
"Tuesday": "Tuesday's child is full of grace",
"Wednesday": "Wednesday's child is full of woe",
"Thursday": "Thursday's child has far to go",
@r-leyshon
r-leyshon / strip-unicode-tags.py
Created October 21, 2024 09:28
Strip hidden unicode tags from python strings in order to avoid malicious prompt injection attacks
# in consideration of
# https://arstechnica.com/security/2024/10/ai-chatbots-can-read-and-write-invisible-text-creating-an-ideal-covert-channel/
# strip hidden unicode tags from prompts and model outputs.
safe = "https://wuzzi.net/copirate/"
malicious = "https://wuzzi.net/copirate/󠀁󠁔󠁨󠁥󠀠󠁳󠁡󠁬󠁥󠁳󠀠󠁦󠁯󠁲󠀠󠁓󠁥󠁡󠁴󠁴󠁬󠁥󠀠󠁷󠁥󠁲󠁥󠀠󠁕󠁓󠁄󠀠󠀱󠀲󠀰󠀰󠀰󠀰󠁿"
safe.encode().decode("unicode_escape")
malicious.encode().decode("unicode_escape")
import re
@r-leyshon
r-leyshon / actions-workflow.yml
Created April 19, 2024 12:36
Breaking changes to codecov-action means CODECOV_TOKEN needs to be set on open repos.
name: API workflow
on: [push, pull_request]
jobs:
build:
runs-on: macos-latest
name: Test python API
steps:
- name: Checkout
@r-leyshon
r-leyshon / batch-add.py
Created April 16, 2024 06:00
Add a group of collaborators to a repository
import warnings
import requests
import toml
from pyprojroot import here
CONFIG = toml.load(here("secrets.toml"))
AGENT = CONFIG["USER"]["AGENT"]
USERS = CONFIG["GITHUB"]["USERNAMES"]
PAT = CONFIG["GITHUB"]["PAT"]
@r-leyshon
r-leyshon / get_data.py
Created December 15, 2023 10:11
Ingest all UK LSOA21 boundaries from ONS Geoportal
import requests
import geopandas as gpd
import pandas as pd
ENDPOINT = "https://services1.arcgis.com/ESMARspQHYMw9BZ9/arcgis/rest/services/"
"Lower_layer_Super_Output_Areas_2021_EW_BFC_V8/FeatureServer/0/query"
params = {
"where": "1=1", # SQL clauses can go here
"outSR": 4326, # CRS that you want
@r-leyshon
r-leyshon / render_toc.R
Created August 10, 2021 06:16 — forked from gadenbuie/render_toc.R
Generate Manual Table of Contents in (R)Markdown Documents
#' Render Table of Contents
#'
#' A simple function to extract headers from an RMarkdown or Markdown document
#' and build a table of contents. Returns a markdown list with links to the
#' headers using
#' [pandoc header identifiers](http://pandoc.org/MANUAL.html#header-identifiers).
#'
#' WARNING: This function only works with hash-tag headers.
#'
#' Because this function returns only the markdown list, the header for the