Skip to content

Instantly share code, notes, and snippets.

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();
import org.apache.commons.math3.stat.regression.SimpleRegression;
import java.util.List;
public class LinearRegressionCalculator {
/**
* Calculates the slope and intercept of the line of best fit using simple linear regression.
*
* @param data A List<Double> object that represents the 'y' values of the data points.
* @return A double array of size 2 where the first element is the slope and the second element is the intercept.
*/
import java.util.AbstractList;
import java.util.Deque;
import java.util.LinkedList;
public class DequeList<T> extends AbstractList<T> {
private final Deque<T> deque;
public DequeList(Deque<T> deque) {
this.deque = deque;
}
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;
public class PolynomialRegressionExample {
public static void main(String[] args) {
// Define your input data (x and y values)
double[][] data = {
{1.0, 2.3},
{2.0, 4.5},
{3.0, 6.7},
// Add more data points as needed
@romanbsd
romanbsd / flatten.js
Last active June 8, 2023 09:16
flatten.js
export default function flattenArray(arr) {
return arr.reduce(function (flatArray, element) {
if (Array.isArray(element)) {
return flatArray.concat(flattenArray(element));
} else {
return flatArray.concat(element);
}
}, []);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.custom.keyboard-remap</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/hidutil</string>
<string>property</string>
@romanbsd
romanbsd / table_extract.py
Created October 31, 2021 08:38
Extract tables from HTML
from typing import List, Union
from bs4 import BeautifulSoup
from bs4.element import Tag
class TableExtract:
def __init__(self, headers: List[str], keep_html=False) -> None:
self._headers = headers
self._keep_html = keep_html
@romanbsd
romanbsd / eslintrc.json
Created October 20, 2021 12:41
simple eslintrc for react
{
"env": {
"es6": true,
"node": true,
"browser": true
},
"parser": "@babel/eslint-parser",
"extends": "eslint:recommended",
"parserOptions": {
"requireConfigFile": false,
from scipy.stats import norm
import numpy as np
N = norm.cdf
def d1_d2(S: float, K: float, T: float, r: float, sigma: float):
d1 = (np.log(S / K) + (r + sigma ** 2 / 2) * T) / (sigma * np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)