This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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>, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! # Demonstrate Schema Evolution in Rust | |
//! A structure must deserialized from several, possibly persistent, variants. | |
//! | |
//! ┌────────────────────────────────────────────┐ | |
//! │ MyStruct │ | |
//! └────────────────────────────────────────────┘ | |
//! ▲ ▲ ▲ │ | |
//! from from from into | |
//! │ │ │ ▼ | |
//! ┌───────┐ ┌───────┐ ┌───────┐ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Please read https://blog.ganssle.io/tag/pytz.html | |
import datetime | |
# The Good | |
try: | |
import zoneinfo | |
except ImportError: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 | |
NewerOlder