Skip to content

Instantly share code, notes, and snippets.

View AndrewDowning's full-sized avatar

Andrew Downing AndrewDowning

  • Intersystems
  • Sydney, Australia
  • 01:31 (UTC -12:00)
View GitHub Profile
@AndrewDowning
AndrewDowning / rooms.py
Last active December 20, 2015 13:29
Rooms - My fun way to do the Rooms Python project that Aaron had to do at school.
import csv
class Command(object): # Decorator for command functions in Rooms class.
Functions = {} # Builds dict of command functions {name:function}
HelpText = {} # Builds dict of help text {name:helptext}
def __init__(self, helpText):
self.HelpText = helpText
def __call__(self, f):
Command.Functions[f.__name__] = f
Command.HelpText [f.__name__] = self.HelpText
@AndrewDowning
AndrewDowning / borg.py
Created July 9, 2013 15:04
Borg pattern demo
class Borg(object):
_state = {}
def __new__(cls, *p, **k):
self = object.__new__(cls, *p, **k)
self.__dict__ = cls._state
return self
class MyBaseClass(object):
def __init__(self, base_thing):
self.base_thing = base_thing
@AndrewDowning
AndrewDowning / Singleton
Created July 9, 2013 13:39
Most trivial Singleton in Python
class Singleton(object):
def __init__(self, thing):
global Singleton
Singleton = self
self.thing = thing
def __call__(self, thing):
return self
singleton = Singleton("Original Instance") # This could happen anywhere
@AndrewDowning
AndrewDowning / Polly Stories
Last active December 16, 2015 20:49
Big list of user stories to define the behaviours of Polly
Definitions:
PollyDiscussionSubjectTree - Subject tree created by facilitators. Made of PollyDiscussionFolders.
PollyDiscussionSubjectFolder - Place(e.g. "Copyright Discussions") for PollyDiscussionSubject to be created.
PollyDiscussionSubject - A subject created in a PollyDiscussionFolder.
A subject is some kind of proposition someone would like agreement on.
PollyDiscussionTree - Tree of PollyDiscussionLists related to a PollyDiscussionSubject.
PollyDiscussionList - Lists of PollyDiscussions at many levels in a PollyDiscussionTree
PollyDiscussionList are sortable by relevance, date/time.
PollyDiscussion - An instance of a discussion point by a specific contributor.
@AndrewDowning
AndrewDowning / MongoSafeModify.py
Created February 24, 2013 12:29
MongoDB doing find_and_modify, allowing for possible operation failure and auto reconnections. This example shows using find_and_modify to run a counter on a document that may not initially exist.
import time
import pymongo
from pymongo import Connection
def safe_modify(col, query, update):
for attempt in range(5):
try:
result = col.find_and_modify(query={'name': 'Andrew'},
update={"$inc": {"count": 1}},
upsert=True, # If document for 'name':'Andrew' not in there, make one.
@AndrewDowning
AndrewDowning / PollySchema.py
Last active December 11, 2015 02:19
Startings of a Python JSON schema validation. - Schema validation. - Data type validation. - Required/optional data members in dicts. - Lists with optional min/max lengths. - List member tye validation. - Strings with optional min/max lengths. - Integers with optional min/max values. - String value checks - Integer Value checks - Extensible sche…
'''
JSONValidate.py - JSON data structures are often received from a uncontrolled sources such as
Web Clients, as a string representation. Standard python tools such as json.py
may be used to parse and validate the string representation into Python data
structures, comprised of dicts, lists, and assorted basic types.
JSONValidate.py provides application level schema validation.
- Schema validation.
- Data type validation.
- Required/optional data members in dicts.
@AndrewDowning
AndrewDowning / PollyDiscussionTree.py
Created September 18, 2012 13:10
First cut at a Tornado/Motor/PyMongo/MongoDB Asynchronous implementation of a Polly Discussion Tree
import tornado
from pymongo import DESCENDING, ASCENDING
from bson.code import Code
import motor
from datetime import datetime
import sys
#
# A Sortable Discussion Tree stored in MongoDB
#