Skip to content

Instantly share code, notes, and snippets.

View dfinke's full-sized avatar

Doug Finke dfinke

View GitHub Profile
@dfinke
dfinke / Logger-DecoratorPattern.ps1
Last active May 27, 2024 11:53
PowerShell Decorator Pattern: Enhance Logger with Timestamp and Uppercase
class Logger {
log($message) { # Define a method called "log" that takes a message as input
$message | Out-Host # Output the message to the console
}
}
class TimeStampingLogger : Logger { # Define a class called "TimeStampingLogger" that inherits from "Logger"
$logger # Declare a variable called "logger"
TimeStampingLogger($logger) { # Define a constructor that takes a "logger" as input
@dfinke
dfinke / ClockObserverPattern.ps1
Last active May 23, 2024 14:13
PowerShell implementation of the Observer Pattern with a Clock Timer example, featuring Digital and Analog clocks.
class Subject {
hidden [System.Collections.ArrayList]$observers
Subject() {
$this.observers = New-Object System.Collections.ArrayList
}
Attach([Observer]$o) { $this.observers.Add($o) }
@dfinke
dfinke / Try-CompositePattern.ps1
Last active May 21, 2024 12:42
Composite Pattern: Simplifies client code by treating individual objects and compositions uniformly, making it easier to work with complex structures
. .\Employee.ps1
$CEO = [Employee]::new("John","CEO", 30000)
$HeadSales = [Employee]::new("Robert","Head Sales", 20000)
$HeadMarketing = [Employee]::new("Michel","Head Marketing", 20000)
$clerk1 = [Employee]::new("Laura","Marketing", 10000)
$clerk2 = [Employee]::new("Bob","Marketing", 10000)
$salesExecutive1 = [Employee]::new("Richard","Sales", 10000)
$salesExecutive2 = [Employee]::new("Rob","Sales", 10000)
@dfinke
dfinke / CeilingFan.ps1
Created May 10, 2024 12:27
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
<#
Allow an object to alter its behavior when its internal state changes.
The object will appear to change its class.
The State pattern puts each branch of the conditional in a separate class.
This lets you treat the object's state as an object in its own right
that can vary independently from other objects
#>
function Test-ToolResources {
param (
[Parameter(Mandatory = $true)]
[hashtable]$ToolResources
)
if ($ToolResources.ContainsKey('code_interpreter')) {
if ($ToolResources['code_interpreter'] -isnot [hashtable]) {
throw "code_interpreter must be a hashtable"
}
@dfinke
dfinke / Invoke-FilesToPrompt.ps1
Created May 4, 2024 13:48
Concatenate a directory full of files into a single prompt for use with LLMs
<#
.SYNOPSIS
Concatenate a directory full of files into a single prompt for use with LLMs
.DESCRIPTION
Takes one or more paths to files or directories and outputs every file, recursively, each one preceded with its filename like this:
path/to/file.py
----
Contents of file.py goes here
# AI-enhanced PowerShell script using prompting for natural language and predictive insights
Import-Module PSAI
$assistant = New-OAIAssistant
# System Health Metrics
$metrics = @{
CPU = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue
Disk = (Get-WmiObject Win32_LogicalDisk | Measure-Object -Property FreeSpace -Average).Average
Memory = (Get-Counter '\Memory\% Committed Bytes In Use').CounterSamples.CookedValue
function Invoke-ATMCashDispenser {
param([int]$AmountRequested)
class UnitOfWork {
hidden [int]$Amt
[int]$Request
[int]$NumberOf50
[int]$NumberOf20
[int]$NumberOf10
[int]$NumberOf5
<#
.EXAMPLE
llm "what is the capital of spain" -provider openai
.EXAMPLE
llm "what is the capital of spain" -provider anthropic
.EXAMPLE
llm "what is the capital of spain" -provider gemini
#>
param(
$Alive = 'O',
$Dead = ' ',
$SleepInSeconds = .5
)
<#
Conway's Game of Life
The classic cellular automata simulation. Press Ctrl-C to stop.
More info at: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life