This file contains 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 math import log10 | |
from typing import Sequence | |
def print_numbered_list(items: Sequence, fmt='{num}. {item}') -> None: | |
"""Display as a numbered list where the items are left aligned. | |
items: | |
A list of printable items. | |
fmt: |
This file contains 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 math import log10 | |
from typing import Sequence | |
def print_table(rows: Sequence[Sequence], hdr=True, rownums=True) -> None: | |
"""Display as a simple text table; rows, columns & optional header. | |
rows: | |
Rows & columns where each cell object is a string or can be | |
converted into a string. |
This file contains 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 shlex | |
import subprocess | |
import sys | |
def bash(cmd): | |
print(cmd) | |
try: | |
res = subprocess.run(shlex.split(cmd), capture_output=True, check=True) | |
except subprocess.CalledProcessError as e: | |
print(res.stderr, file=sys.stderr) |
This file contains 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
~/w/rest-client-boilerplate *… restclient cat ratelimiter.py | |
""" | |
Key-based Rate limiter | |
====================== | |
Rate limiter for API keys & resource ID's. | |
Not thread-safe; use something like redis for that. | |
""" | |
import threading |
This file contains 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 json | |
import sys | |
import click | |
import click._termui_impl | |
class JSONFile(click.File): | |
name = 'JSON.load' |
This file contains 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 itertools import repeat | |
from collections import Mapping, Sequence, deque, namedtuple | |
from treecrawler._compat import string_type, text_type, binary_type | |
DONT_ITER_TYPES = string_type, text_type, binary_type | |
Node = namedtuple('Node', ['keys', 'val']) | |
def get_children(node, element_char, mappings=True, sequences=False, |
This file contains 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 random import randint | |
# --- Base Solution --- | |
def base_sum_diags(n, rows): | |
ldiag = [row[i] for i, row in enumerate(rows)] | |
rdiag = [list(reversed(row))[i] for i, row in enumerate(rows)] | |
print(ldiag) | |
print(rdiag) |
This file contains 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 re | |
from collections import Counter | |
from unidecode import unidecode | |
from toolz import curry, pipe | |
WHITESPACE = re.compile(r'\s+') | |
NONALPHANUM = re.compile(r'[^\w_-]') | |
This file contains 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 collections | |
def get_keys(d, keys=None, pointer=None, sep='.', prefix=''): | |
if keys is None: | |
keys = set() | |
if pointer is None: | |
pointer = [] |
This file contains 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
""" | |
JSON Keys. | |
A simple command-line utility that allows the user to quickly: | |
1. Set the root level of JSON data by using a JSON pointer. | |
2. Select items to pull from each object in a JSON array. | |
3. Drop items from objects in a JSON array. | |
It is not uncommon for an API to return a JSON array of objects containing | |
much more detail than is needed. This tools is designed to allow the user to |
NewerOlder