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 / app-server.textile
Created April 10, 2011 02:40
An overview of some ideas for a Python application server.

Application Management Server

Web-based interface (w/command-line tools) for managing:

  1. Application server configuration.
    1. SSH keys, etc.
  2. Application installation.
    1. Drop a .zip (or .egg, more on this below) into a folder.
    2. Upload the same via web interface.
    3. Enter the URL of a SCM system for checkout and deployment.
@amcgregor
amcgregor / fhs-python-deployment.textile
Created April 19, 2011 00:58
An example filesystem structure that loosely follows the File Hierarchy Standard.

FHS-Based Python Web Application Deployment

The following is based off of one client application deployment scenario, with all of the files described herein managed in a Git repository.

This is NOT a proposed standard; if this is used at all, it’ll be used at the application server level and will be transparent to the underlying (embedded/mounted/mapped/attached/installed) application.

  1. /home/<user>/apps/<application>/ — Git Root
    1. .gitignore — Ignore almost everything.
    2. README.textile — Fancy GitHub notice.
    3. restart — Progressive upgrade / roll-out script. This and the start/stop scripts, below, are venv aware and do not require the venv to be active.
@amcgregor
amcgregor / code.py
Created June 25, 2011 03:46
marrow.mailer futures vs. dynamic thread pools
from datetime import datetime
import logging
from marrow.mailer import Message, Delivery
logging.basicConfig(level=logging.DEBUG)
mail = Delivery({
'manager': 'futures',
'manager.workers': 10,
'transport': 'smtp',
'transport.host': 'secure.emailsrvr.com',
@amcgregor
amcgregor / __init__.py
Created July 3, 2011 16:36
Utilizing marrow.mailer in your Pyramid application.
"""Marrow Mailer integration sample for Pyramid.
For information on Mailer, see:
https://github.com/marrow/marrow.mailer#readme
Delivery methods include: Google AppEngine, IMAP, log, maildir, mbox, sendmail,
Amazon Simple E-Mail Service, and SMTP.
"""
@amcgregor
amcgregor / 00-python-enum-bitfield.textile
Created July 12, 2011 14:14
A blog-ish post about bitfield data design in Python.

Enumerated Bitfield Data Type

If it’s worth doing once, it’s worth writing a system to do it.

So I’m exploring writing a DNS server in Python, and while there are a number of solutions for reading and writing DNS data in a variety of formats (such as BIND configuration files, over-the-wire encoding, etc.) I learn best by doing, not by using someone else’s code. Calling it “not invented here” is naïve at best, so let’s get started with the first over-engineered bit I’ve written.

Flags

DNS is a binary protocol, which is a seriously good thing compared to protocols such as SMTP, NNTP, POP, and IMAP. It makes rather extensive use of bit masks to represent flags, thus we’ll need a method to encode and decode these bit masks, and a convenient way to display them in a human-readable way. Also attached to this Gist is a copy of the flags.py file from the dnspython package, a quite complete and mature package by any standard. Unfortunately, it followed in twisted.names’ footsteps. If you exam

@amcgregor
amcgregor / daemonizing.textile
Created July 14, 2011 15:32
Configuration directives for daemonization.
  • processes (default 1) – number of processes to spawn (0 or None to autodetect cores)
  • respawn (default True) – automatically re-spawn dead slaves in a multi-process environment
  • cwd – change current working directory during daemonization
  • detach (default None; autodetect) – detach from the terminal when daemonizing
  • files
    • preserve (default []) – fileno() descriptors to avoid closing
    • stdin (default os.devnull)
    • stdout (default os.devnull)
    • stderr (default os.devnull)
  • pid
@amcgregor
amcgregor / 01-sample-script.py
Created July 20, 2011 05:05
Example marrow.script help generation output.
class Service(object):
"""Example service.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
@amcgregor
amcgregor / 00-irc-log.textile
Created August 10, 2011 02:03
Where asking a simple question, and not accepting useless and insulting answers, gets you banned from an IRC channel.

Cleaned IRC log of #python starting Monday, 25 July, 2011 at 17:13:55 EDT.

Gems are highlighted in bold.

GothAlice How do I determine if a given object is a @classmethod, and, additionally, how do I determine if it is a @staticmethod? 1
KirkMcDonald GothAlice: Why would you care?
GothAlice KirkMcDonald: Enforcing interfaces, not that it matters.
dash GothAlice: you’re right, it doesn’t matter
_habnabit GothAlice, if you wanted an answer to the question as asked, why don’t you try ##python-friendly? #python is about writing better code.
nedbat GothAlice: what KirkMcDonald should have said was, “The Python culture is to not check ahead of time, do you have an unusual requirement that means you really have to know?”
@amcgregor
amcgregor / routing.py
Last active October 28, 2015 15:26
Example router implementation and example for WebCore 2.
# encoding: utf-8
import re
from functools import wraps
from collections import namedtuple
re_type = type(re.compile(""))
@amcgregor
amcgregor / response-registry.py
Created August 18, 2011 20:39
An example response registry for WebCore 2, eliminating the need for the hard-coded response handlers in web.core.application and the weirdness of the templating middleware.
import types
class IFile(object):
pass
class ResponseRegistry(object):
def __init__(self):
self.registry = list()
def register(self, *kinds):