Skip to content

Instantly share code, notes, and snippets.

View Kagre's full-sized avatar

Kagre

  • USA
View GitHub Profile
@Kagre
Kagre / MAD Example.sqlserver.sql
Created December 8, 2021 20:21
Example: Calculating the Median Absolute Deviation in SQL Server for Item level PO pricing and usage
declare @Items as table(
item varchar(35) primary key
,MedianPrice decimal(20,6)
,MADprice decimal(20,6)
,AvgPrice decimal(20,6)
,MedianQty decimal(20,6)
,MADqty decimal(20,6)
-- ,QtySum decimal(20,6)
-- ,QtyCount decimal(20,6)
,AvgQty decimal(20,6)
@Kagre
Kagre / DownloadSQLiteDLL.ps1
Created October 19, 2021 18:58
Downloads and loads the current SQLite DLL that should work with 64-bit Win10 Powershell
function Download-SQLiteDLL{
[Net.ServicePointManager]::SecurityProtocol = 'Tls11,Tls12'
$BaseURI = 'https://system.data.sqlite.org'
$DownloadsURI = $BaseURI + '/index.html/doc/trunk/www/downloads.wiki'
$r = Invoke-WebRequest -URI $DownloadsURI
$Entry = ([regex]'(?s)(?<=<tr>).*?(?=</tr>)').Matches($r.Content).where{$_.value -match 'sqlite-netFx46-binary-bundle-x64'}.value
$HashType = $Entry -replace '(?s).*\(\s*(.*?):?\s*[0-9a-fA-F]{40,}\s*\).*','$1'
$Hash = $Entry -replace "(?s).*\(\s*${HashType}:?\s*(.*)\).*",'$1'
@Kagre
Kagre / PipelineToggleExample.ps1
Created October 7, 2021 20:14
strange things you should probably avoid doing with the pipeline
function FooBarPipe{
param([parameter(ValueFromPipeline=$true)][int]$val)
begin{
$reset = (get-item Function:\FooBarPipe).ScriptBlock
$Foo = {
param([parameter(ValueFromPipeline=$true)][int]$val)
begin{set-item Function:\FooBarPipe -Value $bar -Force}process{write-output ('foo {0}' -f $val)}
}
$Bar = {
param([parameter(ValueFromPipeline=$true)][int]$val)
@Kagre
Kagre / AwaiterFoo.cs
Last active October 4, 2021 23:35
Un-Winding a C# Async/Await
//////////////////////////////////////////////////////////////////////////////////////
// https://bytelanguage.net/2020/05/31/tpl-and-asynchronous-programming/
// https://www.markopapic.com/csharp-under-the-hood-async-await/
using System;
using System.Threading.Tasks;
namespace MyApplication{public class Program{
public static void Main(string[] args){
Foo(10).Wait();
}
@Kagre
Kagre / AtCLI.ps1
Last active January 26, 2021 20:10
PowerShell - Profile working folder
$FolderProfile = ([io.directoryinfo]((resolve-path '.').ProviderPath)).GetFileSystemInfos('*',[IO.SearchOption]::AllDirectories) | &{
begin{$r=@{}}
process{
$n = resolve-path (split-path $_.FullName) -Relative;
$r.$n+=$_.Length
}
end{$r}
};
$Data = $FolderProfile.Keys | sort | &{
#[switch]$AutoRun is a parameter in the host script that is used when the host-script is called by an automation context
#typical usage: if(!$?){throw-automated 'prevoius line is broken'}
funciton throw-automated([string]$Message,[int]$Offset=1){
try{throw $Message}
catch{
if(!$AutoRun){
$Here = $MyInvocation
$LineNo = $Here.ScriptLineNumber - $Offset
$Line = (gc $Here.ScriptName -TotalCount $LineNo)[-1]
write-host "`n$_`nAt $($Here.ScriptName):$LineNo char:1`n+$Line`n" -BackgroundColor Black -ForegroundColor Red
@Kagre
Kagre / JSUnintTester.html
Last active November 12, 2020 23:53
Stupidly simplified JavaScript Unit-Tester framework
<!DOCTYPE html>
<html><head> <!------------------------------------------------------------------------------------------------------------------------>
<title>A quick and dirty unit testing framework for JavaScript</title>
<!--
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
@Kagre
Kagre / Example1.java
Created September 23, 2020 23:47
RFC 4180 Compliant - OpenCSV Implementation
import com.opencsv.*;
import java.io.*;
class Example1{
public static void main(String[] args)throws Exception{
String DATA_FILE = "TestCSVparse.win.csv";
char DELIMITER = ',';
char QUOTE = '\"';
int SKIP_LINES = 0;
@Kagre
Kagre / AESThenHMAC.Tests.ps1
Last active September 3, 2021 23:49 — forked from jbtule/AESGCM.cs
Convert the AESThenHMAC code example to script for best practice encrypting a string in PowerShell. It uses authenticated encryption. http://stackoverflow.com/a/10366194/637783
<#
.SYNOPSIS
Pester'ng the AESThenHMAC class library
.EXAMPLE
Invoke-Pester .\AESThenHMAC.Tests.ps1
#>param()
. (join-path $PSScriptRoot 'AESThenHMAC.ps1')
$ProjectName = 'AESThenHMAC'