Skip to content

Instantly share code, notes, and snippets.

View Cartroo's full-sized avatar

Andy Pearce Cartroo

View GitHub Profile
@Cartroo
Cartroo / macros.html
Created July 12, 2023 09:10
Useful Jinja macros
{% macro plural(num, unit) -%}
{{ num }} {% if num == 1 %}{{ unit }}{% else %}{{ unit }}s{% endif -%}
{%- endmacro %}
{% macro ordinal(number) -%}
{%- if number % 100 > 3 and number % 100 < 21 -%}{{ number }}th
{%- elif number % 10 == 1 -%}{{ number }}st
{%- elif number % 10 == 2 -%}{{ number }}nd
{%- elif number % 10 == 3 -%}{{ number }}rd
{%- else -%}{{ number }}th{%- endif -%}
@Cartroo
Cartroo / application-skeleton.py
Last active October 4, 2022 10:32
A useful skeleton template for Python command-line applications, which sets up logging and provides useful command-line options to control it.
#!/usr/bin/env python3
import argparse
import logging
import logging.config
import logging.handlers
import os
import sys
from typing import Any
@Cartroo
Cartroo / asyncio-chat-server.py
Last active October 1, 2023 07:26
Example of a simple chat room implemented using `asyncio`.
#!/usr/bin/env python3
import asyncio
import sys
class ChatServer:
def __init__(self, server_name, port, loop):
self.server_name = server_name
@Cartroo
Cartroo / procpoller.py
Last active August 29, 2015 14:21
procpoller.py
import os
import select
import signal
import subprocess
import time
class ProcPoller(object):
"""Watches multiple processes for output on stdout and stderr."""
def __init__(self):
#include <string>
#include <stdlib.h>
namespace {
static const char * const DIGITS = "0123456789";
}
/// Use with std::sort(start, end, NaturalComparator())
class NaturalComparator
{
@Cartroo
Cartroo / gist:b2ad2c2190f1fad31b7a
Last active August 29, 2015 14:16
Code Club: Turtle Scene
from math import *
from random import *
from turtle import *
def grass():
color("ForestGreen")
begin_fill()
forward(5000)
for i in range(3):
right(90)
from math import *
from random import *
from turtle import *
def tree(branchLen):
old_color = color()
old_width = width()
if branchLen >= 20:
color("brown")
width(branchLen / 5.0)
@Cartroo
Cartroo / python_topological_sort.py
Last active December 15, 2015 09:19
This snippet demonstrates a function to implement a simple topological sort in Python. This could be used to resolve a set of targets with dependencies between them, for example.
import functools
def toposort(data):
"""Argument is a dict mapping element to set of dependency elements.
Generator yields sets of elements in an appropriate order to satisfy
dependencies. Each element in the set from a single iteration may be
processed in parallel (i.e. they have no dependencies between them).
"""