Skip to content

Instantly share code, notes, and snippets.

View pashri's full-sized avatar
🌍
Github has statuses?

Patrick Linton pashri

🌍
Github has statuses?
View GitHub Profile
@pashri
pashri / boto_request.py
Created May 14, 2024 15:43
Make an iterator of boto3 request results, which handles pagination.
from collections.abc import Callable, Iterator
from typing import Any, Optional
def boto_request(
func: Callable,
params: Optional[dict[str, Any]] = None,
items_key: str = 'Items',
last_key: str = 'LastEvaluatedKey',
start_key: str = 'ExclusiveStartKey',
@pashri
pashri / harmonic_fibonacci_mean.py
Created May 14, 2024 15:41
Get the average estimate using the harmonic mean, returning the closest Fibonacci number
from functools import lru_cache
import numpy as np
import scipy as sp
from scipy.stats import hmean as harmonic_mean
@lru_cache
def fibonacci(n):
"""Get the nth Fibonacci number"""
@pashri
pashri / iam_api_request.R
Last active May 8, 2024 19:46
API call using IAM authentication in R
library(aws.signature)
library(httr)
library(jsonlite)
library(purrr)
Sys.setenv(
AWS_ACCESS_KEY_ID = "",
AWS_SECRET_ACCESS_KEY = "",
AWS_REGION = "us-east-1"
)
@pashri
pashri / expand_numeric_range.py
Last active April 27, 2023 09:29
Expand numeric range in pandas
from typing import Union
import pandas as pd
def expand_numeric_range(
frame: pd.DataFrame,
colname: str,
start: str,
stop: str,
step: Union[str, int] = 1,
) -> pd.DataFrame:
@pashri
pashri / iam_api_request.py
Last active May 8, 2024 19:50
API request using IAM authentication
"""Use IAM credentials as authentication"""
import boto3
from botocore.awsrequest import AWSRequest
from botocore.auth import SigV4Auth
from requests.auth import AuthBase
from requests import PreparedRequest
class ApiGateway(AuthBase): # pylint: disable=too-few-public-methods
@pashri
pashri / validate_uei.py
Last active August 9, 2022 15:58
Validate GSA unique entity identifier (UEI)
from functools import reduce
from itertools import starmap
import re
from typing import Iterable
def checksum(uei: str) -> bool:
"""Gets a checksum of a UEI"""
def reducer(chars: Iterable[int]) -> int:
@pashri
pashri / decode_mystery_message_from_aws.py
Created August 3, 2021 10:36
Decode that base64 encoded gzipped json from AWS
import gzip
import base64
import io
import json
import pprint
def decode_mystery_message_from_aws(data: str) -> list:
"""Decodes a mystery message from AWS"""
decoded_data = base64.b64decode(data)
@pashri
pashri / pip_install.py
Created February 1, 2021 03:09
Pip install into current environment from within a script
import subprocess
import sys
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
@pashri
pashri / index.html
Created January 23, 2021 23:29
The cubes
<div class="container">
<div id="cube">
<div class=""><div class="front">THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES</div></div>
<div class="a1"><div class="front">THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES</div></div>
<div class="a2"><div class="front">THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES</div></div>
<div class="a3"><div class="front">THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES</div></div>
<div class="a4"><div class="front">THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES</div></div>
<div class="a5"><div class="front">THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES</div></div>
<div class="a6"><div class="front">THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES<br/>THE CUBES</div></div>
<div class="a7"><div class="front">THE CUBES<br/>THE CUBES<br/>
@pashri
pashri / encoding.md
Created January 7, 2021 23:16
UTF-8 And Latin-1 character encoding errors.

Sometimes you've got some incorrectly-encoded text that you want to fix. If you encode it to bytes and decode it back to a string, you can select the source and target encodings and fix the encoding in Python.

'Microsoft® Windows™ 10 is Patrick’s favourite OS'.encode('utf-8').decode('latin-1')
'Microsoft® Windowsâ\x84¢ 10 is Patrickâ\x80\x99s favourite OS'.encode('latin-1').decode('utf-8')