Skip to content

Instantly share code, notes, and snippets.

View pkutaj's full-sized avatar

pavol kutaj pkutaj

View GitHub Profile
@pkutaj
pkutaj / match-branches.ps1
Created September 16, 2021 06:21
Show Git Branches Which are local-only, remote-only or both
function match-branch ([switch]$local_only, [switch]$remote_only, [switch]$both) {
$localBranches = ((git branch -l) -replace "\*", "") -replace " ", ""
$remoteBranches = (((git branch -r) -replace "\*", "") -replace " ", "") -replace "origin/", ""
$branch_comparison = Compare-Object -ReferenceObject $localBranches -DifferenceObject $remoteBranches -IncludeEqual
| Select-Object @{Label = "branch"; Expression = { $_.InputObject } },
@{Label = ”both”; Expression = { $_.SideIndicator -eq "==" } },
@{Label = ”remote_only”; Expression = { $_.SideIndicator -eq "=>" } },
@{Label = ”local_only”; Expression = { $_.SideIndicator -eq "<=" } }
if ($local_only) { $branch_comparison | Where-Object -Property "local_only" -EQ "true" | Select-Object branch }
elseif ($remote_only) {$branch_comparison | Where-Object -Property "remote_only" -EQ "true" | Select-Object branch}
@pkutaj
pkutaj / filecount-per-folder.ps1
Last active October 14, 2021 14:36
File Count per Folder in Powershell (not recursive)
Get-ChildItem -Recurse -Directory |
Select-Object Name, @{
Name="FileCount";
Expression={(Get-ChildItem $_ -File | Measure-Object).Count }
}
@pkutaj
pkutaj / get_files_with_parent_folder.ps1
Last active November 27, 2021 08:44
powershell command for getting files in subfolders in a table with parent folder in another column
dir *.mp4 -rec -file |
select-object @{
Name="folder";
Expression = {Split-Path (Split-Path $_.Fullname -parent) -leaf}
},name
@pkutaj
pkutaj / argparse_boilerplate.py
Last active December 31, 2021 08:43
template for using Python's argparse module
#file: foobar.py
import argparse
def function_proper(param1, param2) -> None:
#CODE...
def init_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument("--param1")
parser.add_argument("--param2")
@pkutaj
pkutaj / ask_for_input.sh
Last active February 16, 2024 10:55
Ask for Input in Bash
echo -n "Are you sure (Y/N)? " #01
answered= #02
while [[ ! $answered ]]; do #03
read -r -n 1 -s answer #04
if [[ $answer = [Yy] ]]; then #05
answered="yes" #06
elif [[ $answer = [Nn] ]]; then #07
answered="no" #08
fi #09
done #10
@pkutaj
pkutaj / Ask_for_input_in_bash_rubberduck.md
Created January 13, 2022 08:15
Ask for Input in Bash Rubberduck Table
STEP# CODE COMMENT
01 echo -n "Are you sure (Y/N)? " print prompt just once with echo
02 answered= initiate an empty test binding
03 while [[ ! $answered ]]; do loop until test binding is populated
04 read -r -n 1 -s answer get input, supress escape, only 1 char, supress input
05 if [[ $answer = [Yy] ]]; then test if input is y or Y
06 answered="yes" ..if true, set test binding to yes → step #12
07 elif [[ $answer = [Nn] ]]; then else, test is input is n or N
08 answered="no" ..if trye, set test binding to no → step #12
@pkutaj
pkutaj / missing_module_error.py
Created January 13, 2022 09:37
Snippet to print missing module error instead of Traceback with ModuleNotFoundError
moduleMissingError = 'Missing dependency module: Run "pip install -r requirements.txt" → Retry'
try:
import #<module1>
import #<package2>
import #<subpackage1>
except ImportError:
sys.exit(moduleMissingError)
@pkutaj
pkutaj / multi_replace_command.ps1
Last active January 15, 2022 01:51
Multiple Replaces and Ternary Operator
# Sometimes URL is copied from a browser and arrives as
# https://consul.foo.com/client/ami_size/edit
# Othertimes URL is copied from Consul CLI and arrives as
# client/ami_size
# And I just need the actual key, being 'ami_size' for the script
$url = ($url -match "^https://") ?
($url -replace "https://consul.foo.com/client/", "") -replace "/edit", "":
($url -replace "client/", "")
@pkutaj
pkutaj / accept_multiple_inputs_from_pipeline.py
Created January 20, 2022 06:25
Exemplary Function Accepting Multiple Inputs from Pipeline in PowerShell
### func1
function query-consul () {
param (
[String[]]$consul_keys,
[String]$queryString,
)
foreach ($consul_key in $consul_keys) {
consul kv get -recurse $consul_key_ |
Select-String $queryString
@pkutaj
pkutaj / Exception-Handling-of-Python-Requests-Module-template-rubberduck.md
Last active January 28, 2022 07:19
Request Module Exception Handling Template
NR CODE COMMENT
#1 response = requests.get(url,...) within a try block, create variable with a response object
#2 response.raise_for_status() raises exception 4xx & 5xx HTTP Statuses/errors
#3 except requests.exceptions.RequestException as e: exception base class for all other exceptions
#4 print(e, file=sys.stderr) print exception object into stderr stream