Skip to content

Instantly share code, notes, and snippets.

Avatar

pavol kutaj pkutaj

View GitHub Profile
View Exception-Handling-of-Python-Requests-Module-template-rubberduck.md
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
@pkutaj
pkutaj / accept_multiple_inputs_from_pipeline.py
Created Jan 20, 2022
Exemplary Function Accepting Multiple Inputs from Pipeline in PowerShell
View accept_multiple_inputs_from_pipeline.py
### 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 / multi_replace_command.ps1
Last active Jan 15, 2022
Multiple Replaces and Ternary Operator
View multi_replace_command.ps1
# 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 / missing_module_error.py
Created Jan 13, 2022
Snippet to print missing module error instead of Traceback with ModuleNotFoundError
View missing_module_error.py
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 / Ask_for_input_in_bash_rubberduck.md
Created Jan 13, 2022
Ask for Input in Bash Rubberduck Table
View Ask_for_input_in_bash_rubberduck.md
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 / ask_for_input.sh
Last active Jan 13, 2022
Ask for Input in Bash
View ask_for_input.sh
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 / argparse_boilerplate.py
Last active Dec 31, 2021
template for using Python's argparse module
View argparse_boilerplate.py
#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 / get_files_with_parent_folder.ps1
Last active Nov 27, 2021
powershell command for getting files in subfolders in a table with parent folder in another column
View get_files_with_parent_folder.ps1
dir *.mp4 -rec -file |
select-object @{
Name="folder";
Expression = {Split-Path (Split-Path $_.Fullname -parent) -leaf}
},name
@pkutaj
pkutaj / filecount-per-folder.ps1
Last active Oct 14, 2021
File Count per Folder in Powershell (not recursive)
View filecount-per-folder.ps1
Get-ChildItem -Recurse -Directory |
Select-Object Name, @{
Name="FileCount";
Expression={(Get-ChildItem $_ -File | Measure-Object).Count }
}
@pkutaj
pkutaj / match-branches.ps1
Created Sep 16, 2021
Show Git Branches Which are local-only, remote-only or both
View match-branches.ps1
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}