Skip to content

Instantly share code, notes, and snippets.

import scala.concurrent.{Await, ExecutionContext, Future}
import java.util.concurrent.{ExecutorService, Executors}
import scala.concurrent.duration.DurationInt
val singleThreadedPool: ExecutorService = Executors.newSingleThreadExecutor()
implicit val ec =
concurrent.ExecutionContext.fromExecutorService(singleThreadedPool)
val rand = new scala.util.Random
def merge(message: String): Future[(String, List[String])] =
Future.successful(
message,
@paul-butcher
paul-butcher / gist:45f2f33dac3c59b28f704b60e56842b8
Last active February 8, 2024 16:37
An Elasticsearch runtime field to remove some impertinent confounding content from a log entry
if(!doc.containsKey("log.keyword") || doc['log.keyword'].empty){
emit("");
} else {
def log = doc['log.keyword'].value;
if (log != null) {
int closingBracketIndex = log.lastIndexOf(']');
if (closingBracketIndex > 0) {
emit(log.substring(closingBracketIndex+1));
return;
@paul-butcher
paul-butcher / explanation
Created July 7, 2022 10:59
Elasticsearch _bulk NOOP
This fails, you cannot update something that is not there
{"update": {"_index":"banana","_id":"dolly"}}
{"doc": {"hello":"dolly"}}
This succeeds, but will always update, even if unchanged.
The version count will increase, and the value of "result" will be "updated"
{"index": {"_index":"banana","_id":"kitty"}}
{"doc": {"hello":"kitty"}
@paul-butcher
paul-butcher / delete_all_indices.sh
Last active June 24, 2022 09:33
Delete all open Elasticsearch indices on localhost
curl -XGET localhost:9200/_cat/indices 2>/dev/null | sed 's/.*open //' | sed 's/ .*//'| xargs -I % echo curl -XDELETE localhost:9200/%
@paul-butcher
paul-butcher / desmond.user.js
Last active November 5, 2021 14:07
wikipedia userscript to help make Richard Desmond more comfortable about his career
// ==UserScript==
// @name Porn-Phil
// @version 1
// @grant none
// @include https://*.wikipedia.org/*
// ==/UserScript==
// Replace any references to pornography with philanthropy
(function() {
@paul-butcher
paul-butcher / side_effect.py
Last active June 22, 2021 09:32
Demonstration of using Mock.side_effect to return a different value on each call.
"""
Using Mock.side_effect with a list to provide multiple return values
"""
from unittest import mock
class MyClass:
@staticmethod
def hello(lang):
@paul-butcher
paul-butcher / if_vs_dict
Created June 17, 2021 15:16
A demonstration of using a dict of callables instead of a chain of if/elif/elif branches
from timeit import timeit
import string
# If you have a chain of if and elif like this
# that you then use to decide a particular
# course of action
def ifelif(arg1, arg2):
if arg1 == 'hello':
return 'hi ' + arg2
elif arg1 == 'namaskara':
@paul-butcher
paul-butcher / patch_methods.py
Created June 16, 2021 08:12
Demonstration of patching python methods whilst leaving the rest of the class intact.
from unittest import mock
class MyClass:
def __init__(self):
print('hello')
self.my_property = 'dolly'
def do_something(self):
print('world')
@paul-butcher
paul-butcher / Mapping script
Created November 29, 2018 12:39
Radon CC warnings plugin
import hudson.plugins.warnings.parser.Warning
String filename = matcher.group(1)
String line = matcher.group(2)
gradeMap = [A:'low - simple block',
B:'low - well structured and stable block',
C:'moderate - slightly complex block',
D:'more than moderate - more complex block',
E:'high - complex block, alarming',
F:'very high - error-prone, unstable block']
String message = gradeMap.get(matcher.group(4))+ " " + matcher.group(4) + " " + matcher.group(5)
@paul-butcher
paul-butcher / mapping script
Last active December 7, 2018 13:12
Radon Maintainability index Warnings plugin
myproj/myfile.py - B (14.83)