Skip to content

Instantly share code, notes, and snippets.

@decatur
decatur / why_getter_costs.rs
Last active January 2, 2025 14:57
With Rust we do not just use Getters and Setters.
// Do not use Getter/Setter without immediate benefits, see also https://news.ycombinator.com/item?id=42506984
// There are costs for abstraction and indirection.
// And in Rust, there is the borrow checker!
#[derive(Default)]
struct Strategy {
throughput: f64,
position: Vec<f64>,
}
@decatur
decatur / DeliveryTime.py
Last active November 19, 2024 09:47
DeliveryTime is a point in time Python abstraction aligned to a 1/4hour grid. TransactionTime is a point in time abstraction
from __future__ import annotations
from datetime import datetime, timedelta, time, tzinfo
from dateutil import tz
BERLIN = tz.gettz('Europe/Berlin')
class DeliveryTime:
"""
DeliveryTime is a point in time abstraction aligned to a ¼hour grid and
optimized for arithmetic operations.
@decatur
decatur / schema_evolution.rs
Last active September 6, 2024 09:08
Demonstrate Schema Evolution
//! # Demonstrate Schema Evolution in Rust
//! A structure must deserialized from several, possibly persistent, variants.
//!
//! ┌────────────────────────────────────────────┐
//! │ MyStruct │
//! └────────────────────────────────────────────┘
//! ▲ ▲ ▲ │
//! from from from into
//! │ │ │ ▼
//! ┌───────┐ ┌───────┐ ┌───────┐
@decatur
decatur / enum_container.rs
Created September 11, 2023 12:59
Example of a container using enum
/// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=35d62d49d15756d1116d620099f324ae
/// Example of a container using enum.
use std::collections::HashMap;
use serde::Serialize;
#[derive(Debug, Serialize)]
struct PlantA {
id: String,
}
@decatur
decatur / object_safe.rs
Created September 11, 2023 12:56
trait is not object safe
#[derive(Clone)]
struct S;
fn main() {
let s
// Uncomment this line to provoke an "trait is not object safe" error
: Box<dyn Clone>
= Box::new(S);
let _ = s.clone();
}
@decatur
decatur / vwap.rs
Created May 15, 2023 06:37
Compute VWAP in rust
use itertools::put_back;
use itertools::Itertools;
fn main() {
let qp_it = put_back([(1, 2), (2, 2), (3, 2), (4, 3), (5, 2)].into_iter());
let acc_qp_it = qp_it.batching(|it| {
let mut q_acc = 0;
let mut qp_acc = 0;
while q_acc < 5 {
match it.next() {
@decatur
decatur / merge_yaml.rs
Last active March 27, 2024 12:53
rust merge multiple yaml docs
// See https://stackoverflow.com/questions/67727239/how-to-combine-including-nested-array-values-two-serde-yamlvalue-objects
// Note this is not https://docs.rs/serde_yaml/latest/serde_yaml/value/enum.Value.html#method.apply_merge
use serde::{Serialize, Deserialize};
use serde_yaml::Value;
fn merge_yaml(a: &mut serde_yaml::Value, b: serde_yaml::Value) {
match (a, b) {
(a @ &mut serde_yaml::Value::Mapping(_), serde_yaml::Value::Mapping(b)) => {
let a = a.as_mapping_mut().unwrap();
@decatur
decatur / pytz_madness.py
Created December 9, 2021 12:44
Demonstrates pytz violates python spec
# Please read https://blog.ganssle.io/tag/pytz.html
import datetime
# The Good
try:
import zoneinfo
except ImportError:
@decatur
decatur / no_env_loader.py
Last active July 18, 2021 11:51
Way out of Python venv hell.
"""
So you are doing Python on MS Windows. Maybe even deploying your stuff on a Windows Server.
By now you know that virtual environments are HEAVY on Windows, and you have many projects or application with the same
copy of numpy (60MB each) over and over again.
No more. Install a package by unpacking wheels (not pip install) to the corresponding versioned folder:
__pypackages__
└── numpy-1.21.0
└── numpy
@decatur
decatur / name_changing_stream.py
Last active July 18, 2021 09:59
So you want your stdout, stderr and logs in a file, but you only know the final log file name somewhere down the road?
"""
So you want your stdout, stderr and logs in a file, but you only know the final log file name somewhere down the road?
"""
import io
import sys
import threading
from pathlib import Path
from typing import IO, cast