Skip to content

Instantly share code, notes, and snippets.

View willmcgugan's full-sized avatar
🏠
Drinking milkshakes

Will McGugan willmcgugan

🏠
Drinking milkshakes
View GitHub Profile
@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");
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@willmcgugan
willmcgugan / console.svg
Last active May 2, 2022 17:44
Example of svg export
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
from rich.markdown import Markdown
from textual import events
from textual.app import App
from textual.widgets import Header, Footer, Placeholder, ScrollView
class MyApp(App):
"""An example of a very simple Textual App"""
@willmcgugan
willmcgugan / new_simple.py
Created October 24, 2021 20:25
Textual App with CSS
from rich.markdown import Markdown
from textual.app import App
from textual.widgets import Header, Footer, Placeholder, ScrollView
class MyApp(App):
"""An example of a very simple Textual App"""
stylesheet = """
@willmcgugan
willmcgugan / typed_property.py
Created October 18, 2021 10:12
A property decorator that permits a different type in the setter
from typing import Any, Callable, Generic, NamedTuple, Optional, Tuple, TypeVar
GetterT = TypeVar("GetterT")
SetterT = TypeVar("SetterT")
GetterCallable = Callable[[Any], GetterT]
SetterCallable = Callable[[Any, SetterT], SetterT]
def typed_property(getter: GetterCallable) -> "TypedPropertyGetter[GetterT]":
from typing import NamedTuple, Tuple, Union
class ConsoleDimensions(NamedTuple):
width: int
height: int
class TestClass:
def __init__(self) -> None:

With dataclasses Rich will essentially replace the dataclass __repr__ by inspecting the dataclass fields. It does this so it can know how to expand the dataclass on to multiple lines with indentation.

For example, here is a dataclass

@dataclass
class DC:
    foo: str
    bar: int