This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io, tokenize | |
def extract_comments(code: str | io.TextIOBase) -> str: | |
""" | |
Extract comments from a piece of Python code, returning a string of | |
*just* the comments. | |
Example: | |
>>> extract_comments(r''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Renaming files to make them more consistent/easier to type. | |
""" | |
import unicodedata | |
import re | |
from pathlib import Path | |
# You can use whatever function you prefer, but Django's slugify is pretty slick | |
def slugify(value, allow_unicode=False): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import operator | |
from functools import reduce | |
def get(obj, key): | |
"""Get the item within `obj` corresponding to `key`. | |
If `obj` is subscriptable, return `obj[key]`, otherwise assume `key` | |
refers to an attribute and return `obj.key` instead. | |
""" | |
if hasattr(obj, '__getitem__'): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%html | |
<canvas id="canvas"></canvas> | |
<script> | |
/* A function that reports the width of a Jupyter notebook cells */ | |
function reportWidth() { | |
/* Get an output area element */ | |
let el = document.querySelector("div.output_subarea.output_text.output_stream.output_stdout > pre"); | |
/* Determine the width of the output area, in pixels */ | |
let output_width = el.clientWidth; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from colorama import Fore, Back, Style | |
foregrounds_dark = { | |
i: Fore.__dict__[i] | |
for i in dir(Fore) | |
if not (i.startswith("_") or i.startswith("LIGHT") or i.startswith("RESET")) | |
} | |
foregrounds_light = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Calculate the number of distinct floats between two numbers via bit packing and | |
unpacking. | |
-------------------------------------------------------------------------------- | |
>>> distinct_floats(1, 0) | |
4607182418800017408 | |
>>> distinct_floats(-1, 0) | |
4616189618054758400 | |
>>> distinct_floats(0, float('inf')) | |
9218868437227405312 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from matplotlib.colors import Normalize | |
class FixedMidpointNorm(Normalize): | |
def __init__(self, vcenter): | |
""" | |
Normalize data with a set center. | |
For plotting diverging colormaps around a particular value, at the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
******************************************************************************** | |
Matplotlib can create some great plots, although it is often a hassle to | |
customize said plots after their creation, particularly if you're creating a | |
large number of them, or regenerating them for minor changes (e.g. scaling) | |
is inconvenient. | |
I've been experimenting with generating "minimal" plots, which contain only the | |
actual representation of the data. | |
Insofar as I can add titles/labels in LaTeX, this allows me to embed the plots |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Here we imagine that you've plotted some things, added a legend, and now | |
want to be able to save that legend separately. | |
Perhaps you want to rescale things later, or maybe you've plotted a bunch | |
of similar graphs and want to have a common legend for all of them, which | |
can be awkard to do in matplotlib. | |
Here is one method for doing this: | |
0. Plot some things, set up a legend | |
1. Get information about the legend, in particular its bounding box |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
def is_iterable(x) -> bool: | |
"""Return `True` if `x` is iterable.""" | |
try: | |
iter(x) | |
return True | |
except TypeError: | |
return False |
NewerOlder