Skip to content

Instantly share code, notes, and snippets.

View scottcode's full-sized avatar

Scott Hajek scottcode

View GitHub Profile
@scottcode
scottcode / http_client_request_examples.py
Created October 1, 2021 14:13
Examples of using python's http.client module to make requests that require authorization
import http.client
conn = http.client.HTTPSConnection("HOSTNAME")
payload = 'grant_type=password&username=USERNAME&password=PASSWORD'
headers = {
'Authorization': 'Basic abcdef0123456==',
'Content-Type': 'application/x-www-form-urlencoded'
}
conn.request("POST", "/api/oauth/token", payload, headers)
res = conn.getresponse()
data = res.read()
@scottcode
scottcode / urlunquote.py
Created August 27, 2021 19:46
Unquote a URL-encoded string
"""Unquote a URL-encoded string
adapted from https://gist.github.com/scottcode/374a3849f96ba29624fd7dd6c4fa1074"""
import sys
import urllib.parse
def unquote(string):
return urllib.parse.unquote(string.strip())
@scottcode
scottcode / jq_example.sh
Created August 19, 2021 04:24
Example processing/querying JSON with jq
# Jq querying
jq 'path(.. | select(.target?))' example.json
jq '.. | select(.target?) | .target' example.json
# Or you can do the same as above on YAML files by using yq and the .yml file as the argument
@scottcode
scottcode / set_comparer.py
Created August 18, 2021 19:41
Class to compare sets
class SetComparer:
def __init__(self, a, b):
self.a = set(a)
self.b = set(b)
@property
def union(self):
return self.a | self.b
@property
@scottcode
scottcode / graph_sampling.py
Created August 17, 2021 18:24
Randomly sample edges from a graph. Can be positive (attested) edges or negative (unattested) edges.
import networkx as nx
G: nx.Graph
nodes: set
adjacency_map = {
node: set(G.neighbors(node))
for node in nodes
}
@scottcode
scottcode / git-backup.sh
Last active April 11, 2022 17:45
git backing up current untracked/uncommitted state
# where 'bkp' is a remote corresponding to a bare local repository in a cloud-synced folder
git stash push --include-untracked
git branch stashed/DATE stash@{0}
git push -u bkp stashed/DATE
git stash pop
git_backup () {
local remote=$1
if [ -z "$remote" ]
@scottcode
scottcode / ssh_proxy_config.txt
Created June 30, 2021 20:28
SSH proxy configuration
# Following goes in ~/.ssh/config to ensure that OpenSSH uses the appropriate proxy
ProxyCommand connect -H proxy_host:proxy_port %h %p
@scottcode
scottcode / unquote_url.sh
Created June 30, 2021 15:14
Unquote URL using python from the command line
url=https://example.com/my%20quoted%20url
echo $url | python -c "import sys, urllib.parse; print(urllib.parse.unquote(sys.stdin.read().strip()))"
@scottcode
scottcode / excel_count_cells_by_color.vba
Created June 21, 2021 17:34
Count cells in a range that have a certain color
' taken from https://docs.microsoft.com/en-us/previous-versions/office/troubleshoot/office-developer/count-cells-number-with-color-using-vba
Function CountCcolor(range_data As Range, criteria As Range) As Long
Dim datax As Range
Dim xcolor As Long
xcolor = criteria.Interior.ColorIndex
For Each datax In range_data
If datax.Interior.ColorIndex = xcolor Then
CountCcolor = CountCcolor + 1
End If
Next datax
@scottcode
scottcode / descriptor_usage.py
Last active June 8, 2021 21:05
How to enable decorator to work on methods inside class definitions
"""
For inspiration on how to do decorators with optional first calls before the decoration,
see example in pytest.fixture:
https://github.com/pytest-dev/pytest/blob/8300b266a8e48b7db2240ae5b52632f018d2c0bd/src/_pytest/fixtures.py#L1339
"""
class decorated(object):
"""https://stackoverflow.com/a/48491028/1789708"""
def __init__(self, func, type_=None):