Skip to content

Instantly share code, notes, and snippets.

View bradmontgomery's full-sized avatar

Brad Montgomery bradmontgomery

View GitHub Profile
@bradmontgomery
bradmontgomery / digger.sh
Created May 4, 2017 20:59
Script to check a domain name against several public DNS servers.
#!/bin/bash
DOMAIN=$1;
if [ -z "$DOMAIN" ]; then
echo "USAGE: ./digger <your-domain>"
else
@bradmontgomery
bradmontgomery / sample_logging.py
Created March 3, 2017 22:10
quick & dirty example of logging in a single module. You could do similar things across all modules.
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG, filename="temp.log")
# ^^^ the filename param says print all log in a specified file.
def main():
logger.info("Getting Started")
@bradmontgomery
bradmontgomery / process_pool_executor_example.py
Created March 3, 2017 20:06
Simple example of using ProcessPoolExecutor vs. serial execution in python.
import hashlib
import sys
from concurrent.futures import ProcessPoolExecutor
from time import sleep, time
def t1(n):
"""Silly function whose time increases as n does."""
for i in range(n):
@bradmontgomery
bradmontgomery / Results.txt
Last active December 6, 2016 04:46
Analyzing the St. Jude Memphis Marathon results. See https://github.com/bradmontgomery/st-jude-marathon
This is data from the 2016 St Jude Memphis Marathon.
Code to generate these results has been moved to:
https://github.com/bradmontgomery/st-jude-marathon
-----------------------------------------------------
2474 Marathon Runners
From 50 different states
Total distance combined ==> 64,818+ miles
Mostly from (top-10 cities):
@bradmontgomery
bradmontgomery / models.py
Created September 21, 2016 23:21
example querying by date
from datetime import date, datetime
from django.db import models
class Foo(models.Model):
day = models.DateField()
created = models.DateTimeField()
def __unicode__(self):
return "{}".format(self.day)
@bradmontgomery
bradmontgomery / single_dispatch_example.py
Created August 23, 2016 17:08
Example of single dispatch in python. This is seriously cool stuff.
"""
Playing with python's single dispatch.
See: https://hynek.me/articles/serialization/
See also PEP 443: https://www.python.org/dev/peps/pep-0443/
"""
from datetime import datetime
from functools import singledispatch
@bradmontgomery
bradmontgomery / example.py
Created August 16, 2016 16:57
Example of setting a choices value for django ArrayField
# Here's your list of choices that would be displayed in a drop-down
# element on the web. It needs to be a tuple, and we define this
# as a variable just for readability/convenience.
#
# This example has 3 choices, each of which consists of two parts:
# 1. the thing that get strored in your database
# 2. the thing that you see in a dropdown list
LABEL_CHOICES = (
('this gets stored in your database', 'This item is what you see in the drop-down'),
('django', 'Django'),
@bradmontgomery
bradmontgomery / raffle.py
Last active January 29, 2019 16:45
raffle.py - a python powered raffle chooser thing
#!/usr/bin/env python
"""
A raffle random-person chooser thing. To make this work,
pip install progress
Then edit the `PEOPLE` list, then run it:
python raffle.py
@bradmontgomery
bradmontgomery / models.py
Created April 30, 2016 16:13
Additional example to return the "display" portion of options for an django ArrayField. A follow-up to https://bradmontgomery.net/blog/nice-arrayfield-widgets-choices-and-chosenjs.
LABEL_CHOICES = (
('django', 'Django'),
('python', 'Python'),
)
class Post(models.Model):
title = models.CharField(max_length=128)
content = models.TextField()
labels = ArrayField(
models.CharField(max_length=32, blank=True, choices=LABEL_CHOICES),
@bradmontgomery
bradmontgomery / context.py
Created February 15, 2016 22:54
simple examples of a context manager in python
"""
Simple example of building your own context manager.
Resources:
- http://preshing.com/20110920/the-python-with-statement-by-example/
- https://docs.python.org/3/library/contextlib.html
- PEP 343 -- the "with" statement: https://www.python.org/dev/peps/pep-0343/
"""