Skip to content

Instantly share code, notes, and snippets.

@ariel-co
ariel-co / .vimrc
Created September 11, 2025 01:35
vimrc
set background=dark
nnoremap <silent> <C-l> :nohlsearch<CR>
set autoindent
filetype plugin indent on
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
@ariel-co
ariel-co / vscode-separate-history.ps1
Created April 18, 2025 16:14
Powershell / Bash: use a separate history file for VS Code
# $Profile has the profile location
# Detect VS Code environment and change the history path
if ( $env:TERM_PROGRAM -eq "vscode" ) {
Set-PSReadLineOption -HistorySavePath ((Get-PSReadLineOption).HistorySavePath + "_vscode")
}
# Or, to preserve the file extension:
if ( $env:TERM_PROGRAM -eq "vscode" ) {
$h = (Get-PSReadLineOption).HistorySavePath
@ariel-co
ariel-co / logging_multiple.py
Created April 8, 2025 22:40
logging to both stdout and file
import logging, logging.config
logging.config.dictConfig({
"version": 1,
"formatters": {
"common": {
"format": (f"%(asctime)s%(msecs)03d %(process)7d "
f"[{JOB_ID or 0:4d}] %(levelname)-7s %(message)s"),
"datefmt": "%Y-%m-%dT%H:%M:%S" }},
"handlers": {
"stdout": {
@ariel-co
ariel-co / FormatterWithFallback.py
Last active February 18, 2025 20:28
string formatter without KeyError
import string
from typing import Callable
class FormatterWithFallback(string.Formatter):
def __init__(self, fallback: str|Callable):
self.fallback=fallback
def get_value(self, key, args, kwds):
try:
return super().get_value(key, args, kwds)
@ariel-co
ariel-co / is_subarray.py
Created September 9, 2024 19:08
test subarray in Python
def is_subarray(a, b):
return any(
all( e_a==e_b for e_a, e_b in zip(a,b[i:]) )
for i in range(len(b)-len(a)+1) )
assert is_subarray('ab', 'abcd')
assert is_subarray('bc', 'abcd')
assert is_subarray('cd', 'abcd')
assert not is_subarray('de', 'abcd')
assert not is_subarray('abcd', 'ab')
@ariel-co
ariel-co / group-nosort.ps1
Last active August 30, 2024 02:22
group-object without sorting first
function Group-NoSort() {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$true)] $InputObject,
[Parameter(Mandatory, Position=0,
HelpMessage="Name of the property to use as a grouping key")][String] $Property,
[Parameter(
HelpMessage="If true, keys will be compared for full equality (e.g. 2.0 and 2 are not equal)")]
[switch] $StrictEq
)
@ariel-co
ariel-co / encodings.ps1
Created May 12, 2024 01:49
how strings get garbled by wrong encodings
[System.Text.Encoding]::UTF8.GetBytes("é") | % ToString X2 | Join-String -Separator " "
# C3 A9
[System.Text.Encoding]::Latin1.GetBytes("é") | % ToString X2 | Join-String -Separator " "
# E9
[System.Text.Encoding]::Latin1.GetString( [System.Text.Encoding]::UTF8.GetBytes("é") )
# é
@ariel-co
ariel-co / timer-ctxmgr.py
Last active March 30, 2024 23:05
context manager class for timing a code block
class Timer():
def __init__(self):
self.timer = time.perf_counter
def __enter__(self):
self.start = self.timer()
return self
def __exit__(self, *args):
self.elapsed = self.timer() - self.start
if __name__ == "__main__":
@ariel-co
ariel-co / argparser_helpmsg.py
Created March 30, 2024 23:01
argparser.ArgumentParser subclass that prints error message automatically
class MyArgParser(argparse.ArgumentParser):
def error(self, message):
print(f'Error: {message}\n', file=sys.stderr)
self.print_help()
sys.exit(2)
@ariel-co
ariel-co / PbReadStdout.java
Created March 16, 2024 16:21
read process output: three ways
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringWriter;
public class PbReadStdout {
public static void main(String[] argv) {
try {
String[] cmd = {
// "/bin/sh", "-c",
// String.format(