Skip to content

Instantly share code, notes, and snippets.

View dmyersturnbull's full-sized avatar

Douglas Myers-Turnbull dmyersturnbull

  • Stanford University
  • Stanford, CA
View GitHub Profile
@dmyersturnbull
dmyersturnbull / smart_enum.py
Created May 5, 2020 01:28
Python enum with a function for lookup by name.
import enum
class SmartEnum(enum.Enum):
"""
An enum with a classmethod `of` that parses a string of the member's name.
"""
@classmethod
def of(cls, v):
"""
@dmyersturnbull
dmyersturnbull / fancy_cmaps.py
Created May 5, 2020 01:28
Nice extra colormaps for matplotlib.
class FancyCmaps:
"""
Useful colormaps for matplotlib. Most importantly:
- white_red
- white_blue
- blue_white
- white_black
The built-in matplotlib ones don't actually go between pure color values!!
For ex, 'Greys' doesn't go from pure white to pure black!
So colormaps to consider avoiding include Greys, Blues, Greens, (etc), bwr, and seismic.
@dmyersturnbull
dmyersturnbull / devnull.py
Created May 5, 2020 01:27
Fake writer (to devnull).
class DevNull:
"""Pretends to write but doesn't."""
def write(self, msg):
pass
def flush(self):
pass
def close(self):
@dmyersturnbull
dmyersturnbull / despine.py
Created May 5, 2020 01:26
Remove spines on a matplotlib figure.
from matplotlib.axes import Axes
@classmethod
def despine(cls, ax: Axes) -> Axes:
"""
Removes all spines and ticks on an Axes.
"""
ax.set_yticks([])
ax.set_yticks([])
ax.set_xticklabels([])
@dmyersturnbull
dmyersturnbull / copy_docstring_decorator.py
Created May 5, 2020 01:26
Python copy-docstring decorator.
from typing import Type
from functools import wraps
def copy_docstring(from_obj: Type):
"""
Decorator.
Copies the docstring from `from_obj` to this function or class.
"""
@wraps(copy_docstring)
@dmyersturnbull
dmyersturnbull / jupyter_audio_display.py
Created May 5, 2020 01:11
Jupyter notebook display using the full width of the page
from IPython.display import display, Markdown, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
@dmyersturnbull
dmyersturnbull / jupyter_audio_display.py
Created May 5, 2020 01:10
Audio container in Jupyter notebook
from typing import Union
from pathlib import PurePath
def listen(path: Union[str, PurePath, bytes]):
"""
Returns an audio container that Jupyter notebook will display.
Must be run from a Jupyter notebook.
Will raise an ImportError if IPython cannot be imported.
:param path: The local path to the audio file
:return: A jupyter notebook ipd.Audio object
@dmyersturnbull
dmyersturnbull / toml_data.py
Last active February 8, 2017 00:18
A data structure for TOML data that's more convenient to use than a plain dict.
from typing import Iterator, Tuple, Dict, List
class MissingConfigEntry(Exception): pass
class TomlData:
"""A better TOML data structure than a plain dict.
Usage examples:
data = TomlData({'x': {'y': {'z': 155}}})
print(data['x.y.z']) # prints 155
@dmyersturnbull
dmyersturnbull / LICENSE
Created November 21, 2016 23:13
Apache License, Version 2.0 for all Gists under dmyersturnbull. The copyright owners may be different for each Gist.
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
@dmyersturnbull
dmyersturnbull / SpiderRecovery.py
Last active November 21, 2016 23:16
Makes a best-effort attempt to recover SMILES strings from compound names unambiguously by searching ChemSpider.
# Douglas Myers-Turnbull wrote this for the Kokel Lab, which has released it under the Apache Software License, Version 2.0
# See the license file here: https://gist.github.com/dmyersturnbull/bfa1c3371e7449db553aaa1e7cd3cac1
# The list of copyright owners is unknown
import re
import warnings
import time
from chemspipy import ChemSpider