Skip to content

Instantly share code, notes, and snippets.

View rcook's full-sized avatar

Richard Cook rcook

View GitHub Profile
@rcook
rcook / main.rs
Created April 18, 2024 20:50
Show creation and modification times for files in a directory
use anyhow::Result;
use chrono::{DateTime, Local};
use std::collections::VecDeque;
use std::fs::{metadata, read_dir};
use std::iter::once;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
fn main() -> Result<()> {
let mut queue =
@rcook
rcook / macros.rs
Created July 21, 2023 23:58
More Rust macros
use std::collections::HashMap;
macro_rules! from_map {
($map: ident, $obj: ident, $field: ident) => {
$obj.$field = $map.get(stringify!($field)).unwrap().to_string();
};
($map: ident, $obj: ident, $head: ident, $($tail: ident), +) => {
from_map!($map, $obj, $head);
from_map!($map, $obj, $($tail), +);
@rcook
rcook / lib.rs
Created May 21, 2023 19:54
Clippy settings
#![warn(clippy::all)]
#![warn(clippy::cargo)]
//#![warn(clippy::expect_used)]
#![warn(clippy::nursery)]
//#![warn(clippy::panic_in_result_fn)]
#![warn(clippy::pedantic)]
#![allow(clippy::derive_partial_eq_without_eq)]
#![allow(clippy::enum_glob_use)]
#![allow(clippy::match_wildcard_for_single_variants)]
#![allow(clippy::missing_errors_doc)]
@rcook
rcook / var_opt.rs
Last active May 9, 2023 17:03
Optionally get environment variable
fn var_opt<K>(key: K) -> Result<Option<String>>
where
K: AsRef<OsStr>,
{
use std::env::{var, VarError};
match var(key) {
Ok(value) => Ok(Some(value)),
Err(VarError::NotPresent) => Ok(None),
_ => bail!("environment variable not found"),
@rcook
rcook / foo.ps1
Last active March 15, 2023 11:49
param(
[Parameter(Mandatory = $false, Position = 0, ValueFromRemainingArguments = $true)]
[object[]] $Arguments
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Show-Usage {
Write-Host -Object @"
@rcook
rcook / compare_dicts.py
Created March 13, 2022 16:49
Compare dictionaries in Python
def dump_diffs(x, y, parts):
path = "/".join(parts)
if x is None:
if y is None:
pass
else:
print(f"{path}: x value {x} does not match y value {y}")
elif isinstance(x, int):
if isinstance(y, int):
if x == y:
@rcook
rcook / Properties.cs
Created February 23, 2022 18:56
Different C# property syntaxes
var x = new MyClass(100, 200);
Console.WriteLine(x.Property0);
Console.WriteLine(x.Property1);
Console.WriteLine(x.Property2);
Console.WriteLine(x.Property3);
Console.WriteLine(x.Property4);
class MyClass
{
public MyClass(int property0, int property1)
@rcook
rcook / prune_empty_dirs.py
Created February 18, 2022 20:28
prune_empty_dirs.py
def prune_empty_dirs(dir, dry_run=True):
for current_dir, ds, _ in os.walk(dir, topdown=False):
for d in ds:
p = os.path.join(current_dir, d)
if os.path.isdir(p):
try:
if not dry_run:
os.removedirs(p)
except FileNotFoundError:
pass
<?xml version="1.0" encoding="UTF-8"?>
<plan>
<workbooks/>
<users>
<mappings>
<userMappingx>
<source mapping="user-mapping-source"/>
<destination mapping="user-mapping-destination"/>
</userMappingx>
<groupMapping>
@rcook
rcook / gist:10c2f49a239777e7f699e7df43790b77
Created April 16, 2020 22:49
Prevent auto-formatting on save in VSCode from moving "import" statements in Python scripts
{
"editor.formatOnSave": true,
"editor.renderControlCharacters": true,
"editor.renderWhitespace": "all",
"editor.rulers": [
72
],
"editor.tabSize": 4,
"editor.wordWrap": "off",
"editor.wordWrapColumn": 72,