Skip to content

Instantly share code, notes, and snippets.

@perryism
perryism / log_level_argparse.py
Last active April 25, 2020 03:44
Log level arparse
import argparse
import logging
import os
import sys
parser = argparse.ArgumentParser(description='Read a teflon event file')
parser.add_argument("--log", choices=["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], default="INFO",
help="Log level")
args = parser.parse_args()
@perryism
perryism / items_contains_in_list.py
Created April 24, 2020 17:21
Return an intercept(or not) list
import re
list_one = ["apple", "orange", "banana", "pineapple", "watermelon"]
list_two = ["apple", "water"]
filter_func = lambda x, lst: all(i not in x for i in lst)
select_func = lambda x, lst: any(i in x for i in lst)
def intercept(list_one, list_two, select=True):
func = select_func if select else filter_func
return filter(lambda x: func(x, list_two), list_one)
@perryism
perryism / dict_as_an_object.py
Created April 24, 2020 16:19
Use dict as an object
class DictAsObject:
def __init__(self, dic):
self._dict = dic
def __getattr__(self, name):
if name == "value":
return self._dict
v = self._dict[name]
return DictAsObject(v)
@perryism
perryism / .es_proxy
Created April 16, 2020 22:17
ElasticSearch Proxy
function es_proxy () {
if [ ! -z $1 ]
then
profile="-e AWS_PROFILE=$1"
echo $1 profile will be used
fi
if [ -z $REGION ]
then
REGION="us-west-2"
echo $REGION region is used
@perryism
perryism / README.md
Last active February 26, 2020 22:45
Backup s3 bucket to a different account

How to copy a bucket from one account to the other

Ref

Prerequisites

  • Setup aws profile for both source and destination

Setup parameters

@perryism
perryism / http_dump.py
Created December 3, 2019 19:12
the script dumps the headers and the body of a request
from flask import Flask, request
app = Flask(__name__)
import logging, sys
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logger = logging.getLogger(__name__)
@app.route("/", methods=["POST", "GET"])
def call():
@perryism
perryism / aws_elastic_search_client.py
Created September 13, 2019 21:17
elastic search client using aws credentials
import boto3
from requests_aws4auth import AWS4Auth
from elasticsearch import Elasticsearch, RequestsHttpConnection
class AwsElasticSearchClient(Elasticsearch):
def __init__(self, **args):
session = boto3.Session()
creds = session.get_credentials().get_frozen_credentials()
awsauth = AWS4Auth(
creds.access_key, creds.secret_key, args["region"], "es", session_token=creds.token
@perryism
perryism / aws-elastisearch-failed.py
Created September 9, 2019 23:43
get errors from elastic search failed logs
import json
import base64
def each_data(stream):
for line in stream.readlines():
data = json.loads(line)
data['errorMessage'] = json.loads(data['errorMessage'])
yield data, json.loads(base64.b64decode(data['rawData']).decode())
import boto3
@perryism
perryism / firehose_record_decorator
Last active August 21, 2019 15:58
firehose json record decorator
class Record:
def __init__(self, record):
self.record = record
def decoded_data(self):
return base64.b64decode(self.record['data'])
def merge(self, new_data):
return {
'recordId': self.record['recordId'],
@perryism
perryism / gist:75cf03242e79559f755297d9ae28977b
Created March 29, 2018 16:34
Hiding user input on terminal when typing password to unlock keychain
read -s PASSWORD && security -v unlock-keychain -p $PASSWORD "/Users/plee/Library/Keychains/login.keychain-db" 2>&1 | grep -v 'unlock-keychain'