Skip to content

Instantly share code, notes, and snippets.

View amcgregor's full-sized avatar
🏢
I died! …well, I got better…

Alice Zoë Bevan–McGregor amcgregor

🏢
I died! …well, I got better…
View GitHub Profile
@amcgregor
amcgregor / 00-custom.py
Last active November 28, 2018 19:02
Non-recursive stream processor for a Textile-like (or Markdown, or reStructuredText, or…) inline syntax.
# encoding: utf-8
from __future__ import print_function
tokens = {
'@': 'code',
'*': 'strong',
'_': 'emphasis',
'-': 'del',
@amcgregor
amcgregor / 01-rough.py
Created September 3, 2011 04:30
A Cucumber/Lettuce-like test runner with in-Python "Gherkin" syntax and complete parallelism.
# encoding: utf-8
"""
Sample output (at no concurrency):
Feature: Test running.
Scenario: Basic tests.
Given: We have something to test.
When: Tests pass.
Then: No error should be present.
@amcgregor
amcgregor / BDHost.m
Created October 5, 2011 18:35
Useful Objective-C methods, classes, objects, and categories. Basically a snippit collection.
// From: http://www.bdunagan.com/2009/11/28/iphone-tip-no-nshost/
// MIT license
// Remember to add CFNetwork.framework to your project using Add=>Existing Frameworks.
#import "BDHost.h"
#import <CFNetwork/CFNetwork.h>
#import <netinet/in.h>
#import <netdb.h>
#import <ifaddrs.h>
@amcgregor
amcgregor / 0-overview.textile
Created November 4, 2011 04:31
BDD Python DSL

What I propose below is a Behaviour (or Story) Driven Development domain-specific language for writing your tests in Python. This uses some tricks of how imports are done (via encoding) to dynamically translate the tests from the DSL into pure Python. Additional tricks are used to preserve the line numbers of errors, offer abbreviated asserts, and automatically pass local variables from parent to child scopes.

This is parallelizable for efficient test running, compiles to Python bytecode for efficiency, and follows the same style as doctests.

If you hate DSLs (hey, Python ain’t Ruby!) consider this to be a file-length docstring. If you don’t like doctests then consider this to be a template engine for tests. (Most modern template engines generate Python code…)

class Tuneable(object):
levels = [1000, 2000, 3000, 4000] # level every 1K XP
# Use:
def get_level(user):
for l, i in enumerate(Tuneable.levels):
if user.xp < i:
break
return l + 1
@amcgregor
amcgregor / 00-index.textile
Created November 9, 2011 20:57
A quick query on configuration of logging.

Question of the Day

How would you configure a logging system like the one below?

The current framework (in which the below code runs) is operational and is divided into the following:

  1. Log class — a chainable class whose methods return new, mutated Log instances. Only methods whose names are logging levels are terminators.
  2. LoggingLevel instances — these declare the relative weights of different logging levels, allowing for filtering.
  3. Message class — this is passed around internally during the delivery of a log message.
  4. Transport classes — these are individually configured transports, like channel, file, list, stream, or syslog.
@amcgregor
amcgregor / tags.tsv
Last active July 2, 2019 19:31
Tags (and their colours) I use on GitHub Issues.
dd5500 0.security A critical security issue is involved.
e10c02 1.defect A problem has been encountered that is preventing utilization.
2d9e11 2.enhancement A request for a new feature or altered behaviour.
ad009f 3.question Additional information has been requested.
2a4380 area:
d4c5f9 client:
6c8ad5 component:crud Issue involves the REST API operations: create, read, update, or delete.
@amcgregor
amcgregor / styles.py
Created November 18, 2011 16:00
Calling conventions from controller to view.
@route('/')
def index():
return template.render('foo.html')
class RootController(Controller):
@expose('myapp.package.templates.foo')
def index(self):
return dict()
@amcgregor
amcgregor / failure.py
Created November 28, 2011 17:05
MongoEngine >= 0.5 dies on the following code.
# encoding: utf-8
"""Asset model.
All asset types must descend from this class.
"""
import re
from datetime import datetime
@amcgregor
amcgregor / 00-overview.textile
Created December 6, 2011 16:02
A high-level overview of Marrow Automaton.

Marrow Automaton

automaton |ôˈtämətən, -ˌtän|

noun ( pl. automata |-tə| or automatons ) — a moving mechanical device made in imitation of a human being.

  • A machine that performs a function according to a predetermined set of coded instructions, esp. one capable of a range of programmed responses to different circumstances.
  • A scripting infrastructure for project and system management.