Skip to content

Instantly share code, notes, and snippets.

View mwcampbell's full-sized avatar

Matt Campbell mwcampbell

  • Wichita, KS, US
View GitHub Profile
from twisted.internet.protocol import Protocol, ClientCreator
from twisted.internet import reactor
class TestProtocol(Protocol):
def connectionMade(self):
self.transport.write("1234" * 1048576)
self.transport.loseConnection()
def connectionLost(self, reason):
reactor.stop()
#include <stdio.h>
void hello() {
printf("Hello, World\n");
hello();
}
int main() {
hello();
return 0;
# Omitting typical SQLAlchemy setup
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
username = Column(String(32))
# ...
class Users(object):
def __getitem(self, username):
@mwcampbell
mwcampbell / gist:1111783
Created July 28, 2011 15:42
decorator.py
def get_event(request, event_id):
event = get_object_or_404(Event, pk=event_id)
user = request.user
if not event.is_access_allowed(user):
raise PermissionDenied()
return event
def takes_event(base_argname="event"):
id_argname = "%s_id" % base_argname
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
username = Column(String(32))
# ...
users = Collection(DBSession, User, "username")
root = {"users": users}
# or root = users
@mwcampbell
mwcampbell / sqlalchemy_traversal.py
Created August 17, 2011 16:47
SQLAlchemy traversal helpers
class DictLike(object):
def __contains__(self, key):
try:
ignored = self[key]
return True
except KeyError:
return False
has_key = __contains__
def get(self, key, default=None):
#include <iostream>
using namespace std;
class Expr {
public:
virtual ~Expr() { }
virtual int eval() = 0;
};
class Literal : public Expr {
type
TExpr = object ## abstract base class for an expression
TLiteral = object of TExpr
x: int
TPlusExpr = object of TExpr
a, b: ref TExpr
method eval(e: ref TExpr): int =
quit "to override!"
@mwcampbell
mwcampbell / PrintLoop.java
Created June 11, 2012 12:38
Sample Java program with both JVM and x86 disassembly
public class PrintLoop {
public static void main(String[] args) {
for (int i = 0; i < 10; i++)
System.out.println("iteration " + i);
}
}
server {
listen 80;
server_name www.raddevon.com;
location /
rewrite ^(.*)$ http://raddevon.com$1;
}
}