Skip to content

Instantly share code, notes, and snippets.

@nwjlyons
nwjlyons / back lit keyboard linux
Last active February 24, 2024 16:18
Turn Mac Book Pro backlit keyboard on and off under linux
# First su in as root
sudo su
# Then cd to backlight directory
cd /sys/class/leds/smc::kbd_backlight/
# Turn the backlight on
cat max_brightness > brightness
# Set to zero to turn off the backlight
@nwjlyons
nwjlyons / path_converters.py
Created October 14, 2019 10:33
Django Date Path Converter
from datetime import datetime, date
class DateConverter:
regex = '[0-9]{4}-[0-9]{2}-[0-9]{2}'
def to_python(self, value: str):
return datetime.strptime(value, '%Y-%m-%d').date()
def to_url(self, value: date):
@nwjlyons
nwjlyons / email.sh
Last active November 6, 2023 21:12
Send plain and html email via telnet.
telnet 0.0.0.0 1025
Trying 0.0.0.0...
Connected to 0.0.0.0.
Escape character is '^]'.
220 mailhog.example ESMTP MailHog
helo localhost
250 Hello localhost
mail from: <neil@example.com>
250 Sender neil@example.com ok
rcpt to: <lisa@example.com>
@nwjlyons
nwjlyons / integer_float.py
Last active August 19, 2023 10:04
Python isinstance checks behave differently from type annotations
>>> isinstance(1, int)
True # 👍
>>> isinstance(1, float)
False # 👍
>>> isinstance(1.0, int)
False # 👍
>>> isinstance(1.0, float)
@nwjlyons
nwjlyons / laptop_fan.sh
Last active July 13, 2023 10:05
Manually control Mac Book Pro laptop fan from linux
# First su in as root
sudo su
# Then set fan control to manual mode
echo 1 > /sys/devices/platform/applesmc.768/fan1_manual
# Then set the fan speed to any value between fan1_min and fan1_max. To view these values
cat /sys/devices/platform/applesmc.768/fan1_min
cat /sys/devices/platform/applesmc.768/fan1_max
@nwjlyons
nwjlyons / roman_numeral.ex
Last active June 12, 2023 19:47
Converts a number to a roman numeral.
defmodule RomanNumeral do
@min_number 1
@max_number 3999
@type to_roman_error() :: :less_than_min_number | :greater_than_max_number
@spec to_roman(pos_integer()) :: {:ok, String.t()} | {:error, to_roman_error()}
@doc """
Converts a number to a roman numeral.
javascript:(function() {
var adjectives = [
"Autumn", "Hidden", "Bitter", "Misty", "Silent", "Empty", "Dry", "Dark", "Summer", "Icy",
"Delicate", "Quiet", "White", "Cool", "Spring", "Winter", "Patient", "Twilight", "Dawn",
"Crimson", "Wispy", "Weathered", "Blue", "Billowing", "Broken", "Cold", "Damp", "Falling",
"Frosty", "Green", "Long", "Late", "Lingering", "Bold", "Little", "Morning", "Muddy", "Old",
"Red", "Rough", "Still", "Small", "Sparkling", "Wandering", "Withered", "Wild", "Black",
"Young", "Holy", "Solitary", "Fragrant", "Aged", "Snowy", "Proud", "Floral", "Restless",
"Divine", "Polished", "Ancient", "Purple", "Lively", "Nameless"];
var nouns = [
@nwjlyons
nwjlyons / index.py
Last active November 4, 2022 19:49
CGI Python Example
#! /usr/bin/env python
print("""Content-Type: text/html
<h1>Hello World</h1>""")
@nwjlyons
nwjlyons / list_view.ex
Last active August 4, 2022 21:11
Reusable Phoenix.LiveView
defmodule ListView do
@callback query(socket :: Phoenix.LiveView.Socket.t()) :: Ecto.Query.t()
defmacro __using__(opts \\ []) do
quote bind_quoted: [opts: opts] do
opts = Keyword.validate!(opts, [:repo, :schema, :per_page])
@repo Keyword.fetch!(opts, :repo)
@schema Keyword.get(opts, :schema)
@per_page Keyword.get(opts, :per_page, 10)
@nwjlyons
nwjlyons / chunks.py
Created December 6, 2017 10:26
Python split generator into sub lists
def chunks(generator, chunk_size):
"""Yield successive chunks from a generator"""
chunk = []
for item in generator:
if len(chunk) >= chunk_size:
yield chunk
chunk = [item]
else:
chunk.append(item)