Skip to content

Instantly share code, notes, and snippets.

@dogweather
dogweather / redirect-sample.rb
Created April 12, 2024 20:10
Example of redirect code in Ruby, plus tests
elsif ors?
case param_id
when 'or', 'ors', 'ors_', 'ors_about'
return ORS_ROOT
when /^nrs_/
return NRS_ROOT + '/' + param_id
when /^tex\._/
return TEXAS_STATUTES_ROOT + '/' + param_id
@dogweather
dogweather / fixed.py
Created April 5, 2024 19:17
Fixed and Refactored Python code
board = ["R", "N", "B", "Q", "K", "B", "N", "R","P", "P", "P", "P", "P", "P", "P", "P"," ", " ", " ", " ", " ", " ", " ", " "," ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "," ", " ", " ", " ", " ", " ", " ", " ","p", "p", "p", "p", "p", "p", "p", "p","r", "n", "b", "q", "k", "b", "n", "r"]
def Print_board():
x = 0
y = 0
while y < 8:
print("\n")
while x < 8:
print(board[x] + " ", end="")
@dogweather
dogweather / parse_title.py
Last active December 31, 2023 21:31
My best Elixir in Python
def parse_title(dom: XmlResponse) -> Title:
name = pipe(
dom
, html.xpath("//TITLE-TEXT")
, text.titleize
)
number = pipe(
dom
, html.xpath("//TITLE-NUM")
, text.split(" ")
@dogweather
dogweather / worker.js
Last active December 1, 2023 15:52
CF Worker example: KV lookup and adding a customer header to a request
async function handleRequest(request) {
// Is the visitor in the VIP list?
let data = request.cf
let visitor_asn = data.asn
// Query the KV database
let network_name = await VIP_API_ASN.get(visitor_asn)
// Create the HTTP header value
let is_vip = (network_name !== null)
@dogweather
dogweather / request_helpers.rb
Last active October 12, 2023 22:16
Use CSS Selectors in RSpec Request Specs
def css(selector)
html.css(selector).text
end
def html
Nokogiri::HTML(response.body)
end
#
# Original
#
dl_lists: list[Any] = html.css("main dl")
if len(dl_lists) > 0 and isinstance(dl_lists[0], Selector):
first_dl_list = dl_lists[0]
else:
raise ParseException("Expected a <dl>")
# Original
def first(node, css, expected):
result = str(node.css(css).get())
if result is None:
raise ParseException(f"Could not parse the {expected}")
return result
# Refactored
def first(node, css, expected) -> str:
#
# Original
#
error: Argument 1 to "assert_never" has incompatible type "Literal[Color.BLUE]";
expected "NoReturn"
#
# Refactored
#
from enum import Enum
class Color(Enum):
RED = "RED"
GREEN = "GREEN"
BLUE = "BLUE" # I just added this
def handle_color(color: Color):
@dogweather
dogweather / simple_exhaustiveness_example.py
Last active October 4, 2022 01:39
Minimal Python Exhaustiveness Check Demo
def get_float(num: str | float):
match (num):
case str(num):
return float(num)