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
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
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
@willmcgugan
willmcgugan / fizzbuzz.py
Created April 10, 2021 16:27
Fizz Buzz in Python
print((0x310A320A46697A7A0A340A42757A7A0A46697A7A0A370A380A46697A7A0A42757A7A0A31310A46697A7A0A31330A31340A46697A7A42757A7A0A31360A31370A46697A7A0A31390A42757A7A0A46697A7A0A32320A32330A46697A7A0A42757A7A0A32360A46697A7A0A32380A32390A46697A7A42757A7A0A33310A33320A46697A7A0A33340A42757A7A0A46697A7A0A33370A33380A46697A7A0A42757A7A0A34310A46697A7A0A34330A34340A46697A7A42757A7A0A34360A34370A46697A7A0A34390A42757A7A0A46697A7A0A35320A35330A46697A7A0A42757A7A0A35360A46697A7A0A35380A35390A46697A7A42757A7A0A36310A36320A46697A7A0A36340A42757A7A0A46697A7A0A36370A36380A46697A7A0A42757A7A0A37310A46697A7A0A37330A37340A46697A7A42757A7A0A37360A37370A46697A7A0A37390A42757A7A0A46697A7A0A38320A38330A46697A7A0A42757A7A0A38360A46697A7A0A38380A38390A46697A7A42757A7A0A39310A39320A46697A7A0A39340A42757A7A0A46697A7A0A39370A39380A46697A7A0A42757A7A).to_bytes(412, "big").decode())
class MoyaFileHandler(logging.Handler):
def __init__(self, filename):
self._filename = filename
super(MoyaFileHandler, self).__init__()
def emit(self, record):
text = self.format(record)
with io.open(self._filename, 'at', encoding="utf-8") as f:
f.write(text + '\n')
@willmcgugan
willmcgugan / template example
Created February 15, 2015 13:50
A demonstration of how to render a parsed template
#!/usr/bin/env python
class Node(object):
def __init__(self, params, children):
self.params = params
self.children = children
class IfNode(Node):