Skip to content

Instantly share code, notes, and snippets.

@BrennanBarker
BrennanBarker / pydantic_xml_example.py
Created November 8, 2023 03:22
pydantic-xml example
from typing import Optional
from pydantic import AnyUrl
from pydantic_xml import BaseXmlModel, RootXmlModel, element
class Tags(RootXmlModel, tag="tags"): # type: ignore
root: list[str] = element(tag="tag")
class Notes(BaseXmlModel, tag="notes"):
@BrennanBarker
BrennanBarker / pyodide-progress.js
Created July 9, 2022 18:22
Progress bar updates for pyodide
// <progress id="progress"></progress>
async function go() {
await pyodide.runPythonAsync(`r = np.random.randn(10000000); 'hi'`)
document.querySelector('#progress').value=30
await pyodide.runPythonAsync(`r = np.random.randn(10000000); 'hi'`)
document.querySelector('#progress').value=60
await pyodide.runPythonAsync(`r = np.random.randn(10000000); 'hi'`)
document.querySelector('#progress').value=90
return 5
@BrennanBarker
BrennanBarker / build.log
Created June 11, 2022 15:30
pomegranate failed pyodide build.log
* Creating virtualenv isolated environment...
* Installing packages in isolated environment... (cython, setuptools, wheel)
* Installing packages in isolated environment... (cython, pythran)
* Getting dependencies for wheel...
Error compiling Cython file:
------------------------------------------------------------
...
from libc.stdlib cimport free
from libc.stdlib cimport malloc
@BrennanBarker
BrennanBarker / meta.yaml
Created June 11, 2022 15:21
meta.yaml for pomegranate 0.14.8 pyodide build
package:
name: pomegranate
version: 0.14.8
source:
url: https://files.pythonhosted.org/packages/f8/29/0e3433b4657ea582e2d4c12dc3f27ba0fe1d9b45eebedfb475ed16d3d193/pomegranate-0.14.8.tar.gz
sha256: 2296651290482dd53204ffaaaea267ceee057ce1b3ef1f9d9793febe66d6693d
test:
imports:
- pomegranate
about:
@BrennanBarker
BrennanBarker / jeditab.vim
Created October 13, 2021 23:36
settings to get Jedi-vim to play nicely with superb
autocmd FileType python setlocal completeopt-=preview
let g:jedi#max_doc_height=5
let g:SuperTabContextDefaultCompletionType = "<c-x><c-o>"
let g:SuperTabDefaultCompletionType = "context"
@BrennanBarker
BrennanBarker / bt.py
Created September 9, 2021 04:44
An implementation of the Bradley-Terry model for paired comparisons.
import numpy as np
def p(i, w, p_prime):
def summand(w, i, j, p_prime):
return (w[i][j] + w[j][i])/(p_prime[i] + p_prime[j])
def denom(i, w, p_prime):
return sum(summand(w, i, j, p_prime) for j in range(len(w.T)) if j != i)
return np.nansum(w[i])/denom(i, w, p_prime)
def bt(w, n):
@BrennanBarker
BrennanBarker / export_pdf_pages.py
Last active April 9, 2021 20:29
Export pages from a pdf
from PyPDF2 import PdfFileReader, PdfFileWriter
def extract_pages(in_fp, pages, out_fp):
"""Extract a list of pages from a pdf into a new pdf."""
pdf = PdfFileReader(input_fp)
pdf_writer = PdfFileWriter()
for p in pages:
pdf_writer.addPage(pdf.getPage(p - 1)) # PyPDF2 starts at 0...
@BrennanBarker
BrennanBarker / gist:5af75bd78f59ad8b6bf92db041b9fba4
Created December 16, 2020 19:07
Bash Partial History Completion and Realtime History Updates
# In .inputrc:
## arrow up
"\e[A":history-search-backward
## arrow down
"\e[B":history-search-forward
# In .bashrc:
shopt -s histappend
PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
@BrennanBarker
BrennanBarker / qt_connect.py
Created November 30, 2020 17:39
connecting local qtconsole to a remote Jupiter kernel over ssh
""" Connect via ssh to a remote jupyter kernel
1. Start a jupyter kernel on the remote machine:
`(remote)$ jupyter kernel`
2. Note the kernel connection file location and copy to local machine (can use globbing if you escape, e.g. "\*"):
`(local)$ scp remote-user@remote-host:<conn-file.json> .`
3. Use this script to quickly forward the appropriate ports over SSH:
`(local)$ for port in $(python qt_connect.py <conn-file.json>); do ssh -f -N -L $port\:localhost:$port pi@192.168.1.90; done`
4. Open a local qtconsole instance specifying a connection to the remote
kernel:
`(local)$ jupyter qtconsole --existing <conn-file.json>`
@BrennanBarker
BrennanBarker / shovelpass.py
Created November 19, 2020 19:38
look for an environment variable before using get pass
"""First check environment variables for a password, if not then use `getpass`.
You might set the environment variable ahead of time with (from a shell)::
read -s secret
export secret
"""
import os, getpass