Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
require 'digest'
all = Hash.new
Dir["*.jpg"].each do |file|
md5 = Digest::MD5.hexdigest(IO.read(file))
ary = all.fetch(md5, [])
ary << file
all[md5] = ary
def sha512_base64(string)
# Compute SHA-512 digest in binary form
digest = OpenSSL::Digest::SHA512.digest(string)
# Encode the binary digest in base64
base64_encoded = Base64.strict_encode64(digest)
"sha512-#{base64_encoded}"
end
import Chart from 'stimulus-chartjs'
// Connects to data-controller="async-chart"
export default class extends Chart {
static values = {
url: String
}
connect() {
fetch(this.urlValue)
module ExternalApiRuntime
extend ActiveSupport::Concern
module ClassMethods
def log_process_action(payload)
messages = super
api_runtime = payload[:api_runtime]
messages << (format('ExternalAPI: %.1fms', api_runtime.to_f)) if api_runtime
messages
end
import { Controller } from '@hotwired/stimulus'
import { TempusDominus } from '@eonasdan/tempus-dominus'
function findParentBySelector(element, selector) {
while (element && element.parentElement) {
if (element.parentElement.matches(selector)) {
return element.parentElement;
}
element = element.parentElement;
}
import numpy as np
import matplotlib.pyplot as plt
def cross_correlation(x, y):
# Calculate cross-correlation using convolution
ccf_values = np.convolve(x, y[::-1], mode='full') / np.sum(y**2)
# Adjust indices to represent lags
lags = np.arange(-(len(y) - 1), len(x))
@romanbsd
romanbsd / list_packages_lambda.py
Created October 18, 2023 09:57
List preinstalled packages on AWS lambda
from pkg_resources import working_set
def lambda_handler(event, context):
print(*(f'{m.key}~={m.version}' for m in sorted(working_set, key=lambda x: x.key)), sep='\n')
class VertxRetryHelper {
private static final Logger LOG = LoggerFactory.getLogger(VertxRetryHelper.class);
private final Vertx vertx;
public VertxRetryHelper(Vertx vertx) {
this.vertx = vertx;
}
public <T> Supplier<CompletionStage<T>> decorateCompletionStage(
RateLimiter rateLimiter,
@romanbsd
romanbsd / MyVerticle.java
Created September 21, 2023 08:37
asynchronously reserve permission
class MyVerticle extends AbstractVerticle {
/* Try to acquire permits asynchronously */
private <T> Supplier<CompletionStage<T>> decorateCompletionStage(
RateLimiter rateLimiter,
Supplier<CompletionStage<T>> supplier) {
return () -> {
final CompletableFuture<T> promise = new CompletableFuture<>();
final BiConsumer<T, Throwable> biConsumer = (result, throwable) -> {
if (throwable != null) {
import pickle
import os
def cache_to_pickle(cache_dir):
def decorator(func):
def wrapper(*args, **kwargs):
# Generate a unique cache filename based on the function and its arguments
cache_filename = f"{func.__name__}_{hash((args, tuple(kwargs.items())))}.pickle"
cache_path = os.path.join(cache_dir, cache_filename)