Skip to content

Instantly share code, notes, and snippets.

@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
@decatur
decatur / Python_App-Definition.md
Last active July 26, 2021 17:54
A definition of what constitutes a Python Application

What is a Python Application

A Python Application describes required packages, entry point and constraints for the operation and development process.

Operation

  1. An entry point, e.g. my_app.my_module:App
  2. Constraints for required packages AppR, e.g. {my_app=1.2.3, my_util=0.1.2, pandas=1.2.5, ...}

Development

@decatur
decatur / README.md
Last active April 1, 2021 21:25
Chrome extension to make yammer feed pages more user friendly.

This a Chrome extension to make yammer feed pages more user friendly. As I personally do not trust extensions from the Chrome Web Store, this extension is only offered through this gist and must be installed in Developer Mode.

Setup

  1. Download this gist to some folder my_yammer_extension_folder
  2. Add this Chrome Extension according to Getting started - Chrome Developers:
    1. Open the Extension Management page by navigating to chrome://extensions.
    2. Enable Developer Mode by clicking the toggle switch next to Developer mode.
  3. Click the LOAD UNPACKED button and select the my_yammer_extension_folder
@decatur
decatur / pandas_with_typing.py
Last active September 18, 2020 09:32
typed pandas
import numpy as np
class Plant:
"""
KISS implementation of a time series data class with a ndarray backing.
Think of Pandas with typing.
Properties can be also set by scalar values.
"""
def __init__(self, dim: int):