Skip to content

Instantly share code, notes, and snippets.

View LouisAmon's full-sized avatar

Louis Amon LouisAmon

View GitHub Profile
@LouisAmon
LouisAmon / json_to_gzip.py
Created July 26, 2018 12:49
How to compress JSON-able data using gzip (Python 3)
import json
import gzip
def compress_data(data):
# Convert to JSON
json_data = json.dumps(data, indent=2)
# Convert to bytes
encoded = json_data.encode('utf-8')
# Compress
compressed = gzip.compress(encoded)
@LouisAmon
LouisAmon / pandas_to_elasticsearch.py
Last active November 4, 2023 08:34
Convert a `pandas.DataFrame` to a generator of ElasticSearch actions to be passed to a bulk helper
def pandas_to_elasticsearch(df):
"""
Generator that converts `pandas.DataFrame` rows into ElasticSearch actions
"""
# ElasticSearch will raise an exception on `NaN` values
df = df.dropna()
for i, row in df.iterrows():
action = {
'_op_type': 'index',
'_index': 'my_index',
@LouisAmon
LouisAmon / avro_to_dataframe.py
Created January 15, 2017 11:55
Read Avro file from Pandas
import pandas
import fastavro
def avro_df(filepath, encoding):
# Open file stream
with open(filepath, encoding) as fp:
# Configure Avro reader
reader = fastavro.reader(fp)
# Load records in memory
records = [r for r in reader]
version: '3'
services:
opensearch-node1:
image: opensearchproject/opensearch:1.0.0-rc1
container_name: opensearch-node1
environment:
- cluster.name=opensearch-cluster
- node.name=opensearch-node1
- discovery.seed_hosts=opensearch-node1
- cluster.initial_master_nodes=opensearch-node1
@LouisAmon
LouisAmon / shovel.py
Created July 5, 2017 05:06 — forked from jdmaturen/shovel.py
SQS + Boto + Eventlet, re http://gist.github.com/434053
(jd@XXX) /home/jd/shovel> time ./shovel.py jd_test
got 901 messages
real 0m1.617s
user 0m0.558s
sys 0m0.044s
@LouisAmon
LouisAmon / mod10.py
Created May 18, 2021 14:35 — forked from emesik/mod10.py
python mod10 checker
def mod10(number):
digits = []
even = False
for digit in reversed(number):
digit = ord(digit) - ord('0')
if even:
digit = digit * 2
if digit >= 10:
digit = digit % 10 + digit / 10
digits.append(digit)
@LouisAmon
LouisAmon / example_provider.py
Created February 26, 2020 07:15
Pulumi Dynamic Provider (Python)
# This simple example illustrates how Nuage recommends you should build
# a Dynamic Provider in Pulumi
#
# The example is based on the official documentation:
# https://www.pulumi.com/docs/intro/concepts/programming-model/#example-github-labels-rest-api
#
# It defines a Github Label programmatically via Pulumi.
#
# The recommended directory structure is the following:
#
@LouisAmon
LouisAmon / filebase64sha256.py
Last active January 25, 2020 09:17
Terraform's `filebase64sha256` function, in Python (cf. https://www.terraform.io/docs/providers/aws/r/lambda_function.html)
import base64
import hashlib
def sha256sum(filename):
"""
Helper function that calculates the hash of a file
using the SHA256 algorithm
Inspiration:
https://stackoverflow.com/a/44873382
@LouisAmon
LouisAmon / s3_download_file_progress_bar.py
Created October 19, 2018 10:57 — forked from wy193777/s3_download_file_progress_bar.py
Show AWS s3 download_file Progress using tqdm
#python3
def hook(t):
def inner(bytes_amount):
t.update(bytes_amount)
return inner
BUCKET_NAME = 'your_s3_bucket_name'
FILE_NAME = 'your_s3_file_name'
s3 = boto3.resource('s3')
@LouisAmon
LouisAmon / lambda_handler.py
Created October 15, 2018 08:16
Run a system command in a Lambda
import subprocess
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
"""
Runs a shell command and returns the output code
STDOUT & STDERR are logged into Cloudwatch