Skip to content

Instantly share code, notes, and snippets.

View lucaswiman's full-sized avatar

[object Object] lucaswiman

View GitHub Profile
@lucaswiman
lucaswiman / local-sandbox.sh
Created June 4, 2023 18:02
MacOS Sandbox for Python Code
#!/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="$@"
@lucaswiman
lucaswiman / audit_descriptor.py
Created July 10, 2023 23:26
re: https://hachyderm.io/@glyph@mastodon.social/110692244224116619
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]
cat /usr/share/dict/words \
| tr '[:upper:]' '[:lower:]' \
| grep -E '^.....$' \
| shuf | head -n 1
#!/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'"
"""
>>> 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)
@lucaswiman
lucaswiman / .py
Created June 20, 2022 07:58
islice versus destructuring dis
>>> 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)
@lucaswiman
lucaswiman / wrapt_type_munging_example.ipynb
Last active April 20, 2022 02:12
Using Wrapt to alter a method signature
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@lucaswiman
lucaswiman / breakpoint_at.py
Created April 12, 2022 08:29
Context manager to add a breakpoint at a particular code location
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')
@lucaswiman
lucaswiman / untar.js
Created March 24, 2022 23:36
js untar in memory
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) {
import numpy as np
import cv2
def sp_noise(image, prob):
'''
Add salt and pepper noise to image
prob: Probability of the noise
'''
output = image.copy()