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
def build_suffix_array(s: str): | |
""" | |
Implements the "prefix doubling" algorithm. | |
See https://en.wikipedia.org/wiki/Suffix_array | |
""" | |
n = len(s) | |
if n == 0: | |
return [] | |
suffix_arr = list(range(n)) |
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 typing import Callable | |
class AuditDescriptor: | |
def __init__(self, field_name, audit_action): | |
self.field_name = field_name | |
self.audit_action = audit_action | |
def __get__(self, instance, owner): | |
if instance is None: | |
return self | |
return instance.__dict__[self.field_name] |
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
#!/bin/bash -e | |
# set -x | |
# References: | |
# * https://wiki.mozilla.org/Sandbox/OS_X_Rule_Set | |
# * https://reverse.put.as/wp-content/uploads/2011/09/Apple-Sandbox-Guide-v1.0.pdf | |
# * https://mybyways.com/blog/creating-a-macos-sandbox-to-run-kodi (sound) | |
# * See also existing rulesets in `/usr/share/sandbox` | |
# * https://github.com/devpi/devpi (pypi proxy) | |
echo args="$@" |
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
cat /usr/share/dict/words \ | |
| tr '[:upper:]' '[:lower:]' \ | |
| grep -E '^.....$' \ | |
| shuf | head -n 1 |
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
#!/usr/bin/env python3 | |
""" | |
Run a sqlite query on the given database and print the results using pandas. | |
Also adds user-defined confidence interval functions to the database. | |
Usage: | |
sqlite-query.py database.db "SELECT * FROM table WHERE column = 'value'" | |
""" |
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 inspect, functools | |
>>> | |
>>> class Void: | |
... pass | |
... | |
>>> def handle_defaults(f, undefined=Void): | |
... sig = inspect.signature(f) | |
... @functools.wraps(f) | |
... def defaults_handled(*args, **kwargs): | |
... bound = sig.bind(*args, **kwargs) |
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 dis | |
>>> from itertools import islice | |
>>> def first_two_islice(it): | |
... return tuple(islice(it, 2)) | |
... | |
>>> def first_two_destructuring(it): | |
... x, y, *rest = it | |
... return x, y | |
... | |
>>> dis.dis(first_two_islice) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 pdb import Pdb, set_trace | |
from contextlib import contextmanager | |
@contextmanager | |
def debug_at(filename, lineno, condition=None): | |
p = Pdb() | |
filename = p.canonic(filename) | |
break_command = f"break {filename}:{lineno}" + (f', {condition}' if condition else '') | |
p.rcLines.append(break_command) | |
p.rcLines.append('continue') |
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
const tar = require("tar"); | |
exports.unTar = (tarballStream) => { | |
const results = {}; | |
return new Promise((resolve, reject) => { | |
const parser = tarballStream.pipe(new tar.Parse()); | |
parser.on( | |
"entry", async (entry) => { | |
const chunks = [] | |
for await (let chunk of entry) { |
NewerOlder