Skip to content

Instantly share code, notes, and snippets.

View brandon-rhodes's full-sized avatar

Brandon Rhodes brandon-rhodes

View GitHub Profile
@brandon-rhodes
brandon-rhodes / conf.py
Created July 12, 2018 08:51
Make Sphinx doctest insensitive to object address differences
# Append this to the `conf.py` file at the root of your Sphinx project
# that is already using the `sphinx.ext.doctest` extension:
import doctest
import re
import sphinx.ext.doctest as ext_doctest
ADDRESS_RE = re.compile(r'\b0x[0-9a-f]{1,16}\b')
class BetterDocTestRunner(ext_doctest.SphinxDocTestRunner):
# Inspired by the following sentence that I ran across this morning:
#
# "f_lineno is the current line number of the frame - writing to
# this from within a trace function jumps to the given line
# (only for the bottom-most frame). A debugger can implement a
# Jump command (aka Set Next Statement) by writing to f_lineno."
#
# https://docs.python.org/2/reference/datamodel.html
#
# There is an older implementation of a similar idea:
@brandon-rhodes
brandon-rhodes / VenusFromJupiter.ipynb
Created August 5, 2014 17:16
Where Venus appears from Jupiter, relative to Jupiter's axis and equator
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@brandon-rhodes
brandon-rhodes / gist:7ba1582c65a817ac3bcd
Created July 15, 2014 11:55
Two-level breadth-first descent using a generator
# Generators leave it up to the caller whether
# to build a list, or set, or maybe just to
# loop and not build a data structure at all:
def get_children(self):
for c in self.children.itervalues():
yield c
for c in self.children.itervalues():
for c2 in c.get_children():
yield c2
@brandon-rhodes
brandon-rhodes / listlines.rst
Created January 4, 2014 05:28
Test of putting literal lines inside of list items.

The following list has three items.

  • The first item is a normal paragraph with no lines breaks or other details.

  • The second item has only a single line break inside, as it has only two interior lines that each start with
@brandon-rhodes
brandon-rhodes / build-overlay.py
Last active April 4, 2023 13:35
Script that builds a Google Earth overlay that projects the map of Tolkien’s Middle-earth atop modern Europe at the correct position and scale. Once the .kmz overlay file has been generated, simple open it using the File menu in Google Earth.
"""Project a map of Middle-earth on modern Europe.
Builds an `overlay.kmz` file in the current directory which should be
opened with Google Earth.
"""
import os
import urllib2
import zipfile
from math import cos, radians
@brandon-rhodes
brandon-rhodes / refraction.ipynb
Created April 14, 2013 20:32
A quick iPython Notebook showing the algorithm that libastro, and thus PyEphem, uses to adjust the visual altitude of objects that are 15° or higher in the sky. (For objects closer to the horizon, a different formula is used; see `refract.c`.)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@brandon-rhodes
brandon-rhodes / gist:3438936
Created August 23, 2012 17:19
Geographically correct alternative to Radarmatic.com's sweepArc() function
/* NOTE: this function requires the Google Maps "geometry" library which
Radarmatic does not currently load. It would need to load Maps with:
http://maps.google.com/maps/api/js?libraries=geometry&sensor=false */
function sweepArc(context, center_x, center_y, radius, width,
start_angle, end_angle) {
/* Special case: if we are being asked to draw the big gray circle
around the current radar site, then draw a big gray circle. */
@brandon-rhodes
brandon-rhodes / simplehttpserver.sh
Created August 10, 2012 01:41
Shell script that finds a free TCP port, runs the Python built-in HTTP file server on that port, and then opens a new Google Chrome tab displaying the site's root directory
#!/bin/bash
#
# To use: cd to a directory you want to browse, and run this script.
#
# This lets you skip finding a free TCP port on your system and
# running SimpleHTTPServer on that port and pointing your browser there.
# Instead, we ask the SimpleHTTPServer to run at whichever free port the
# operating system would like to assign, we watch its output to see what
# port was in fact assigned, and then we open a new tab in the browser
# (in this case, Google Chrome) automatically.