Skip to content

Instantly share code, notes, and snippets.

View rmorshea's full-sized avatar

Ryan Morshead rmorshea

View GitHub Profile
@rmorshea
rmorshea / metasuper.py
Last active May 26, 2016 02:42
call methods of a parent class during class construction.
import inspect
def metamethod(cls, name):
"""Get a method owned by a metaclass
The method is retrieved from the instantiating metaclass.
Parameters
----------
cls: class
@rmorshea
rmorshea / actively.py
Created July 26, 2017 19:58
A single file module for creating staged actions.
import six
import inspect
class MetaEngine(type):
def __init__(cls, name, bases, classdict):
cls._cycle = []
rotation = cls.next_rotation(None)
while rotation is not None:
cls._cycle.append(rotation)
@rmorshea
rmorshea / schema_compose.py
Created October 9, 2018 00:00
Compose two or more schemas into one - see https://github.com/keleshev/schema/pull/169 for details.
from schema import Schema, _priority, DICT, Optional
def compose(*schemas, **kwargs):
"""Compose two or more schemas into one.
See https://github.com/keleshev/schema/pull/169 for details.
"""
reduce = kwargs.pop("reduce", lambda schemas: schemas[0])
if not callable(reduce):
@rmorshea
rmorshea / component_application.py
Created December 15, 2018 18:31
component_application
import inspect
from weakref import finalize
class Component:
def _prepare(self):
pass
def _require(self):
@rmorshea
rmorshea / voluptuous_model.py
Last active January 17, 2019 21:14
Voluptuous sphinx documentation model.
# Copyright (c) 2019 by Cisco Systems, Inc.
from voluptuous import Schema, All
from collections import Mapping
from abc import ABCMeta
from typing import Dict, Any
class MetaModel(ABCMeta):
def __init__(self, name, bases, attrs):
attr_validators = self._attribute_validators.copy()
@rmorshea
rmorshea / idom_snake.py
Created February 28, 2019 22:47
The game Snake implemented in Python with iDOM (https://github.com/rmorshea/idom)
import idom
import enum
import time
import random
import asyncio
class WASD(enum.Enum):
w = (-1, 0)
@rmorshea
rmorshea / idom_snake.py
Last active March 27, 2019 15:01
The game Snake implemented in Python with iDOM (https://github.com/rmorshea/idom)
import idom
import enum
import time
import random
import asyncio
class WASD(enum.Enum):
w = (-1, 0)
@rmorshea
rmorshea / snake_game_class.py
Last active October 22, 2021 02:29
Class-based Snake Game With IDOM
import asyncio
import enum
import random
import time
from dataclasses import dataclass, replace
import idom
class GameState(enum.Enum):
@rmorshea
rmorshea / auto-activate-venv.sh
Created July 15, 2022 23:12
Auto activate venv in current directory
#!/bin/bash
# activate Python venv in present dir
function cd() {
if [[ -d ./.venv ]] && [[ $(which python) = $PWD* ]]; then
deactivate
fi
builtin cd $1
if [[ -d ./.venv ]]; then
import sys
from inspect import signature, Signature, Parameter, BoundArguments, cleandoc
from argparse import ArgumentParser, RawTextHelpFormatter
from typing import TypeVar, Callable
F = TypeVar("F", bound=Callable[..., None])
class Commands: