Skip to content

Instantly share code, notes, and snippets.

View pgolding's full-sized avatar
🎯
Focusing

Oberon Dude pgolding

🎯
Focusing
View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@pgolding
pgolding / embeddings.ipynb
Created February 23, 2024 04:13
Demo of embeddings file sizes for OpenAI Embeddings
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@pgolding
pgolding / streamingbody.md
Last active January 30, 2024 16:28
Handling of StreamingBody Response from Invoking a Lambda Function

Handling of StreamingBody Response from Invoking a Lambda Function

When calling a Lambda Function via Boto3, the returned object is something like:

{u'Payload': <botocore.response.StreamingBody object at 0x7f8d5b62ac50>, 
'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '5bdbb3ca-3b35-11e7-9816-397b140c3bac', 'HTTPHeaders': {'x-amzn-requestid': '5bdbb3ca-3b35-11e7-9816-397b140c3bac', 'content-length': '1636', 'x-amzn-trace-id': 'root=1-591ca199-00d34d5474d16275ec2c8d10;sampled=0', 'x-amzn-remapped-content-length': '0', 'connection': 'keep-alive', 'date': 'Wed, 17 May 2017 19:16:41 GMT', 'content-type': 'application/json'}}, u'StatusCode': 200}

The Payload parameter is <botocore.response.StreamingBody> which is a data streaming object.

@pgolding
pgolding / conditional_check_fail.py
Created December 12, 2017 17:13
The unclearly documented error exceptions of Boto3 (Python) DynamoDB - e.g. ConditionalCheckFailedException
from botocore import exceptions
try:
# to update table conditionally
except exceptions.ClientError as e:
if e.response['Error']['Code'] == 'ConditionalCheckFailedException':
print("ConditionalCheckFailedException detected")
# do something else, like raise a custom error based on the condition
@pgolding
pgolding / example.md
Last active August 25, 2023 16:07
Passing AWS Lambda Function parameters using the ClientContext object in Python

Boto3 documentation for lambda_client.invoke suggests it is possible to pass data via the ClientContext object as a string.

The string is required to be base64 encoded JSON.

To encode such a string, use:

import baseb4
base64.b64encode(b'<string>').decode('utf-8')
@pgolding
pgolding / readme.md
Last active August 17, 2023 08:34
How to use pyjwt to decode RS256-signed JWT tokens (e.g. for Auth0) and run it on AWS Lambda with Python 3.6 (e.g. custom authorizers for serverless)

PyJWT with Python AWS Lambda Functions (LF)

If you are using Auth0 (or some OAuth authorization service) then you will most likely be interested in using JWT tokens via some kind of grant. Auth0 discusses how to call an API with such a token.

Perhaps you wish to use a custom authorizer for your serverless project.

To do so, you must write code to decode the JWT token before creating a policy (or not) to grant invoke permissions on your LF. Per various recommendations, the best method to protect your JWT tokens is to use RS256 signing.

However, if you are using pyjwt to decode RS256 tokens, this library depends upon cryptography and that, in turn, has compiled dependencies

@pgolding
pgolding / scan.py
Created June 5, 2017 19:40
scan all elements from a dynamodb table using Python (boto3)
from __future__ import print_function # Python 2/3 compatibility
import boto3
import json
import decimal
from boto3.dynamodb.conditions import Key, Attr
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
@pgolding
pgolding / policy.md
Last active October 18, 2022 13:21
s3 bucket policy for presigned URLs generated by serverless lambda functions

AWS Presigned URLs

Presigned URLs are useful for fine-grained access control to resources on s3.

For example, if storing larger text blocks than DynamoDB might allow with its 400KB size limits s3 is a useful option.

Ignoring various ACL methods and using presigned URLs, it's possible to create lambda functions that can generate the required upload and download URLs.

Using the default IAM roles and lambda proxy configuration of serverless, lambdas are assigned an IAM role for the application (so that a logical group of functions can share resources - e.g. for a CRUD REST API). Each function then assumes the IAM role via its own function name.

@pgolding
pgolding / cosine_similarity.py
Created May 27, 2017 20:26
Cosine Similarity Python Scikit Learn
# http://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.cosine_similarity.html
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
# The usual creation of arrays produces wrong format (as cosine_similarity works on matrices)
x = np.array([2,3,1,0])
y = np.array([2,3,0,0])
# Need to reshape these
x = x.reshape(1,-1)
@pgolding
pgolding / bayesian_testing.py
Last active June 22, 2020 22:12
Bayesian A/B testing implementation in Python
from scipy.stats import beta
from scipy.special import betaln
# based upon https://www.evanmiller.org/bayesian-ab-testing.html#binary_ab_implementation
def prob_B_beats_A(alpha_A, beta_A, alpha_B, beta_B):
total = 0
for i in range(0,alpha_B-1):
total += np.exp(betaln(alpha_A + i, beta_B + beta_A) - \
np.log(beta_B + i) - betaln(1+i, beta_B) - betaln(alpha_A, beta_A))
return total