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 / HttpHeadResponse.java
Created May 19, 2020 21:27
Elegant wrapper around an HTTP HEAD response in Java. Parse dates correctly.
// copied from https://github.com/dmyersturnbull/genomics-io/blob/master/core/src/main/java/org/pharmgkb/parsers/utils/HttpHeadResponse.java
package org.pharmgkb.parsers.utils;
import com.google.common.collect.ImmutableMap;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.io.IOException;
import java.net.HttpURLConnection;
@dmyersturnbull
dmyersturnbull / AwesomeTry.java
Created May 19, 2020 21:23
Java monadic Try<> with map and recovery, a lot like Scala's.
// copied from https://github.com/dmyersturnbull/genomics-io/blob/master/core/src/main/java/org/pharmgkb/parsers/utils/Try.java
package org.pharmgkb.parsers.utils;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
@dmyersturnbull
dmyersturnbull / code_status.py
Created May 5, 2020 06:25
Decorator to generate warnings for immature, preview, obsolete, or deprecated code.
import enum
from warnings import warn
from functools import wraps
class CodeIncompleteError(Exception): pass
class ImmatureWarning(UserWarning): pass
class ObsoleteWarning(UserWarning): pass
@enum.unique
class CodeStatus(enum.Enum):
@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