Skip to content

Instantly share code, notes, and snippets.

Avatar

Joshua Bixby bixb0012

View GitHub Profile
@bixb0012
bixb0012 / powershell_management.ps1
Last active Jul 3, 2022
PowerShell: Management-related
View powershell_management.ps1
#Requires -Version 5.1
# Reference: 1) https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist
# Reference: 2) https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch
# Reference: 3) https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream
# Example 1: Copy file and measure combined (read & write) transfer rate over time
$FilePath = "" # Path of the file to copy
$Destination= "" # Path to the new directory or folder
$BufferSize = 64 # Buffer size, in KB, for copying file
View python_class_tree_helpers.py
#!python3
# Reference: 1) https://docs.python.org/3/library/stdtypes.html#special-attributes
# Example 1: Function to print base class or subclass hierarchies
def print_classtree(cls, bases=True, level=0):
print(f"{'-'*2*level} {cls}")
if bases:
classes = cls.__bases__
else:
classes = cls.__subclasses__()
@bixb0012
bixb0012 / powershell_security.ps1
Last active Apr 30, 2021
PowerShell: Security-related
View powershell_security.ps1
#Requires -Version 5.1
# Reference: 1) https://docs.microsoft.com/en-us/dotnet/api/system.security.principal
# Example 1: Retrieve security identifier value (SID) for an NT account
# Adapted from https://devblogs.microsoft.com/scripting/use-powershell-to-translate-a-users-sid-to-an-active-directory-account-name/
$DomainName = "" # Domain of account, commonly $Env:USERDOMAIN
$AccountName = "" # Name of account, commonly $Env:USERNAME
$NTAccount = [Security.Principal.NTAccount]::New($DomainName, $AccountName)
$Sid = ($NTAccount.Translate([Security.Principal.SecurityIdentifier])).Value
@bixb0012
bixb0012 / powershell_servicepointmanager_customizations.ps1
Last active Apr 26, 2021
PowerShell: ServicePointManager Customizations
View powershell_servicepointmanager_customizations.ps1
#Requires -Version 5.1
# Reference: 1) https://docs.microsoft.com/en-us/dotnet/api/system.net.servicepointmanager
# Reference: 2) https://docs.microsoft.com/en-us/dotnet/api/system.net.security.remotecertificatevalidationcallback
# Example 1: Force TLS 1.2 connections from client
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Example 2: Ignore all SSL/TLS policy errors, e.g., ignore SSL/TLS secure channel errors
# from self-signed certificates
@bixb0012
bixb0012 / powershell_regular_expressions.ps1
Last active Apr 2, 2021
PowerShell: Regular Expressions
View powershell_regular_expressions.ps1
#Requires -Version 5.1
# Reference: 1) https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_regular_expressions
# Reference: 2) https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
$TestEmailFrom = @(
"smokey.bear@usda.gov"
" smokey.bear@usda.gov "
"<smokey.bear@usda.gov>"
" < smokey.bear@usda.gov > "
"Smokey Bear <smokey.bear@usda.gov>"
View icon_sisyphean.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bixb0012
bixb0012 / python_lazy_counters.py
Last active Jul 30, 2019
Python: Lazy Counters
View python_lazy_counters.py
#!python
# Reference: 1) https://docs.python.org/3/library/collections.html#collections.defaultdict
# Reference: 2) https://www.python.org/dev/peps/pep-0342/
from collections import defaultdict
# Example 1: Using coroutine via generator for multivalue counter
def multi_counter_generator():
cnt = defaultdict(int)
incr = yield cnt.items()
@bixb0012
bixb0012 / python3_generate_english_word_list.py
Last active Feb 6, 2019
Python3: Generate English Word List
View python3_generate_english_word_list.py
#!python3
# Reference: 1) https://www.gutenberg.org/ebooks/29765
# Reference: 2) https://wordnet.princeton.edu/download/current-version
from itertools import dropwhile, takewhile
import re
import urllib.request
# Example 1: Project Gutenberg's Webster's Unabridged Dictionary (Webster's Dictionary 1913)
url = r"https://www.gutenberg.org/cache/epub/29765/pg29765.txt"
@bixb0012
bixb0012 / arcpy_pro_look_ahead_cursor.py
Last active Jul 31, 2019
ArcPy (Pro): Look-Ahead Cursor
View arcpy_pro_look_ahead_cursor.py
#!python3
# Reference: 1) https://pro.arcgis.com/en/pro-app/arcpy/data-access/searchcursor-class.htm
# 2) https://pro.arcgis.com/en/pro-app/arcpy/data-access/updatecursor-class.htm
# 3) https://docs.python.org/3/library/itertools.html
import arcpy
from itertools import chain, zip_longest, tee
fc = # Path to feature class or name of feature layer
flds = # Fields to include in cursor
@bixb0012
bixb0012 / python_data_stream_sampling.py
Last active Jul 31, 2019
Python: Data-Stream Sampling
View python_data_stream_sampling.py
#!python
# Background https://www.springer.com/cda/content/document/cda_downloaddocument/9783540286073-c2.pdf
# Example 1 adapted from https://stackoverflow.com/questions/12581437/python-random-sample-with-a-generator-iterable-iterator/
#
# Reference: 1) https://docs.python.org/3/library/functions.html
# 2) https://docs.python.org/3/library/itertools.html
# 3) https://docs.python.org/3/library/random.html
from itertools import islice
from random import randint