Skip to content

Instantly share code, notes, and snippets.

View dlukes's full-sized avatar

David Lukes dlukes

View GitHub Profile
@dlukes
dlukes / utf-8-sig.ipynb
Created December 26, 2022 00:21
Comparing the performance of utf-8 vs. utf-8-sig encodings in Python
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dlukes
dlukes / app.py
Last active December 2, 2022 15:48
Dispersion plot with Shiny for Python
from collections import Counter
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
import regex as re
from shiny import App, reactive, render, ui
mpl.rcParams.update(
{
@dlukes
dlukes / multi_borrow.rs
Created May 6, 2021 06:38
E0499 and function signatures extending mutable borrow lifetimes
// Some instances of E0499 ("cannot borrow X as mutable more than once
// at a time") are straightforward to understand, but some can be
// tricky. Among the latter ones (at least for me) are those related to
// the way a function potentially extends the lifetime of a borrow by
// tying it to another value.
//
// For instance, this function: https://github.com/benhoyt/countwords/blob/5318b1acdd5bd313039d480af535cf79565c2e62/rust/optimized-unsafe/main.rs#L72
//
// Try changing it so that it accepts a &'a mut Vec<u8> instead of a
// &'a Cell<Vec<u8>>. You'll get the following error:
// ==UserScript==
// @version 0.8.1
// @name Zotero ShareLaTeX Cite-as-you-Write
// @namespace https://github.com/dlukes
// @author dlukes
// @description Insert citations from Zotero into ShareLaTeX as you write.
// @match *://sharelatex.korpus.cz/*
// @run-at document-end
// @grant unsafeWindow
// @grant GM.xmlHttpRequest
diff -ru a/src/main/java/mpi/eudico/client/annotator/player/VLCJMediaPlayer.java b/src/main/java/mpi/eudico/client/annotator/player/VLCJMediaPlayer.java
--- a/src/main/java/mpi/eudico/client/annotator/player/VLCJMediaPlayer.java 2016-01-14 16:44:42.000000000 +0100
+++ b/src/main/java/mpi/eudico/client/annotator/player/VLCJMediaPlayer.java 2019-04-08 16:39:56.894874290 +0200
@@ -597,6 +597,7 @@
public void setOffset(long offset) {
logger.log(Level.FINE, "set offset {0}", offset);
this.timeOffset = offset;
+ mediaDescriptor.timeOrigin = offset;
}
@dlukes
dlukes / remove_docx_dates.py
Last active May 7, 2019 15:30
Remove dates from comments and tracked edits in docx. Also, a cheatsheet for namespaces in lxml.
#!/usr/bin/env python3
"""Usage: {} AUTHOR_SUBSTRING INPUT.DOCX OUTPUT.DOCX
Remove date metadata from Word document for authors matching
AUTHOR_SUBSTRING. Handy if you don't want other people to know when
exactly you found time to work on their document ;)
In more detail: Read INPUT.DOCX, extract the comments and tracked edits,
manipulate them (cf. functions `modify_comments()` and
`modify_tracked_edits()` -- by default, they remove date metadata
@dlukes
dlukes / explicitly_installed.py
Created November 6, 2017 10:38
List packages explicitly installed with pacman or the AUR on Arch Linux
#!/usr/bin/env python3
"""List packages explicitly installed with pacman or pacaur."""
import re
from collections import namedtuple
from subprocess import check_output
Package = namedtuple("Package", "date name")
@dlukes
dlukes / cnc-toolbar-links.js
Created March 4, 2017 07:59
CNC Toolbar Links
#!/usr/bin/env python3
import time
import subprocess as sp
import concurrent.futures as cf
start = time.time()
with cf.ThreadPoolExecutor() as e:
futures = [e.submit(sp.check_output, f"sleep 2s && echo Done {i}.", shell=True) for i in range(3)]
@dlukes
dlukes / this_regular_vs_arrow_functions.js
Last active January 25, 2017 12:09
A review of `this` in regular vs. arrow functions in JavaScript.
/* With regular functions, `this` is bound purely depending on how the function is called. If it's
* bound to an object (either explicitly or via the `instance.funcName()` sugar), `this` refers to
* that object. Otherwise, it's `undefined` (or `window`, depending on your exact environment and
* whether `"use strict";` is in effect).
*
* In particular, it doesn't matter whether the function is attached to the object itself as a
* property, i.e. each instance has its own version of the function created during initialization,
* (see `Class2`), or whether it exists on the object's `prototype` as a method and all instances
* share it (see `Class1`). `this` is always resolved dynamically, i.e. depending on the call site.
* Neither of these is well suited for yanking out of the context of the original object and using