Skip to content

Instantly share code, notes, and snippets.

View amcgregor's full-sized avatar
🏢
Examining Options

Alice Zoë Bevan–McGregor amcgregor

🏢
Examining Options
View GitHub Profile
@amcgregor
amcgregor / model.py
Created February 18, 2010 10:39
The data model for the basic WebCore Wiki example.
# encoding: utf-8
"""Web application data model."""
import web.core
import textile
from paste.registry import StackedObjectProxy
@amcgregor
amcgregor / controller.py
Created February 18, 2010 20:15
Cleaned up Wiki example, step 2.
# encoding: utf8
"""Web application root controller."""
import web.core
import datetime
from megawiki import model as db
@amcgregor
amcgregor / controller.py
Created February 18, 2010 20:48
The Article controller from the WebCore Wiki example.
class Article(web.core.RESTMethod):
def __init__(self, name):
self.name = name
self.record = db.session.query(db.Article).get(name)
super(Article, self).__init__()
def get(self, raw=False):
record = self.record
if raw:
@amcgregor
amcgregor / gist:405354
Last active June 15, 2020 16:35
Python timeit benchmarking for realistic optimizations.
# All units in usecs (µsec) comparing Python 2.7 | 3.7.
# Last updated: 2019-02-11
# MacBook Pro (15-inch, 2016)
# macOS 10.14.3
# 2.7 GHz Intel Core i7
# 16 GB 2133 MHz LPDDR3
python -m timeit "200 <= 250 < 300" # 0.0354 | 0.059
python2.7 -m timeit "250 in xrange(200, 300)" # 1.25
@amcgregor
amcgregor / irc.py
Created June 17, 2010 10:57
An extensible IRC bot, including logging to MongoDB.
#!/usr/bin/env python
# encoding: utf-8
"""An implementation of a very simple IRC bot using the Protocol wrappers. This is the main script.
Requires: pymongo
"""
import logging
import re
@amcgregor
amcgregor / lessons.textile
Created November 16, 2010 00:02
Lessons learned from #python on irc.freenode.net regarding support.

Lessons Learned from #python

Over the years myself and others have attempted to utilize the #python support channel on irc.freenode.net when solving problems. For the most part the time spent explaining the problem, the rationale behind the design, uncovering the exact question to ask to get the desired (helpful) response, and personality conflicts have resulted in more frustration than problems solved. So I’m writing up this lessons file in an effort to not repeat the past mistakes in supporting my own users.

What it really comes down to, other than wasted development hours, is not having to explain to your boss that your team should rewrite 30% of your project because some random git in a chat room said you were Doing it Wrong™.

Alternate Support Channel

In an effort to produce a place where individuals can ask questions without fear of being directly or indirectly called an idiot, I’ve opened #python-friendly, the friendly place to discuss Python, ask questions, and get helpful answers. #pyt

@amcgregor
amcgregor / Terminal 1 - Server
Created November 20, 2010 16:26
Logs from two terminals, the first running the marrow.server.http HTTP/1.1 server in pre-fork mode (no threading) and the second running AB and checking memory. The third is a partial snapshot of `top` checking CPU utilization. [benchmark, performance, http]
ecanus ~ # echo 32768 > /proc/sys/fs/file-max
ecanus ~ # ulimit -n 32768
ecanus ~ # cd ~amcgregor/personal; . bin/activate; cd src
(personal)ecanus src # python
Python 2.6.5 (release26-maint, Aug 8 2010, 20:35:07)
[GCC 4.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
(personal)ecanus src # python marrow.server.http/marrow/server/http/__init__.py
INFO:marrow.server.base:Starting up.
@amcgregor
amcgregor / asynchat.py
Created November 25, 2010 05:53
Asynchronous chat server.
# encoding: utf-8
import logging
from functools import partial
from marrow.server.base import Server
from marrow.server.protocol import Protocol
@amcgregor
amcgregor / package.py
Created November 26, 2010 13:29
Python package blueprint, including namespace packages.
# encoding: utf-8
from marrow.blueprint.api import Blueprint, Folder, File
__all__ = ['PackageBlueprint']
class PackageBlueprint(object):
@amcgregor
amcgregor / wsgi2-filter-vs-middleware.py
Created November 29, 2010 09:16
A comparison of a stupid/simple filter and its implementation as middleware.
# You have a simple application:
def hello(environ):
status = b'200 OK'
headers = [(b'Content-Type', b'text/plain')]
body = b"Hello " + environ['wsgiorg.routing_args'][1]['name'] + b'!'
headers.append((b'Content-Length', len(body).encode('ascii')))
return status, headers, [result]