Skip to content

Instantly share code, notes, and snippets.

View willmcgugan's full-sized avatar
🏠
Working from home

Will McGugan willmcgugan

🏠
Working from home
View GitHub Profile
@willmcgugan
willmcgugan / last_lines.py
Created March 2, 2024 16:10
Get the last lines from a file
import mmap
def get_last_lines(path: str, count: int) -> list[str]:
"""Get count last lines from a file."""
with open(path, "r+b") as text_file:
text_mmap = mmap.mmap(text_file.fileno(), 0, mmap.ACCESS_READ)
position = len(text_mmap)
while count and (position := text_mmap.rfind(b"\n", 0, position)) != -1:
count -= 1
Cook the pinto beans in a pressure cooker with 3 parts water to 1 part beans, and some salt.
While that is cooking dice an onion and 3 stalks of cellery, add a few cloves of garlic crushed with a knife. Fry lightly and stir for around 20 minutes. Add sliced sausage and fry for another 10 minutes.
When the beans are ready take a couple of scoops and add them to the onion mixture. Mash the beans so they are completely broken down (this will thicken the sauce). Add paprika, chipotle, tomatoe puree and black bepper, then combine with the beans.
Simmer for another 10 minutes or so until the sauce thickens. Add some vinegar and chopped parsely near the end.
@willmcgugan
willmcgugan / calculator.css
Created August 30, 2022 15:16
Textual calculator example
Screen {
overflow: auto;
}
#calculator {
layout: table;
table-size: 4;
table-gutter: 1 2;
table-columns: 1fr;
table-rows: 2fr 1fr 1fr 1fr 1fr 1fr;
@willmcgugan
willmcgugan / stopwatch.css
Created August 23, 2022 13:30
Examples application in Textual introduction
Stopwatch {
layout: horizontal;
background: $panel-darken-1;
height: 5;
min-width: 50;
margin: 1;
padding: 1;
}
TimeDisplay {
@willmcgugan
willmcgugan / timers.css
Created August 18, 2022 15:13
Timers example
TimerWidget {
layout: horizontal;
height: 5;
background: $panel-darken-1;
border: tall $panel-darken-2;
margin: 1;
padding: 0 1;
transition: background 200ms linear;
}
@willmcgugan
willmcgugan / fractions.py
Created August 2, 2022 10:43
Fractions are accurate
def segments_float(size, parts):
end = 0.0
part = size / parts
for n in range(parts):
start = int(end)
end += part
print(str(n) * int(end - start), end="")
print()
@willmcgugan
willmcgugan / intersection.py
Created August 1, 2022 18:51
lru_cache is fast
@lru_cache(maxsize=4096)
def intersection(self, region: Region) -> Region:
"""Get the overlapping portion of the two regions.
Args:
region (Region): A region that overlaps this region.
Returns:
Region: A new region that covers when the two regions overlap.
"""
@willmcgugan
willmcgugan / render_map.py
Last active August 1, 2022 18:43
Dict views are amazing
from typing import NamedTuple
class Region(NamedTuple):
x: int
y: int
width: int
height: int
@willmcgugan
willmcgugan / partition.py
Created July 25, 2022 19:29
Ludicrous partition function
def partition_will(pred, values):
if not values:
return [], []
if len(values) == 1:
return ([], values) if pred(values[0]) else (values, [])
values = sorted(values, key=pred)
lower = 0
upper = len(values)
index = (lower + upper) // 2
try:
<html>
<body>
<svg class="rich-terminal" viewBox="0 0 1221.6000000000001 549" xmlns="http://www.w3.org/2000/svg">
<style>
@font-face {
font-family: "Fira Code";
src: local("FiraCode-Regular"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");