Skip to content

Instantly share code, notes, and snippets.

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)
def cache_disk(seconds = 900, cache_folder="/tmp"):
def doCache(f):
def inner_function(*args, **kwargs):
# calculate a cache key based on the decorated method signature
key = sha1(str(f.__module__) + str(f.__name__) + str(args) + str(kwargs)).hexdigest()
filepath = os.path.join(cache_folder, key)
# verify that the cached object exists and is less than $seconds old
if os.path.exists(filepath):
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.io.IOException;
public class EmptyStringToNullDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
String value = jsonParser.getValueAsString();