Skip to content

Instantly share code, notes, and snippets.

@ReallyLiri
ReallyLiri / safe_contextlib.py
Created June 30, 2019 12:07
safe_contextmanager
import sys
from functools import wraps
from contextlib import ContextDecorator, AbstractContextManager
# Copied and changed from python3.6/contextlib.py
def safe_contextmanager(func):
@wraps(func)
def helper(*args, **kwds):
@ReallyLiri
ReallyLiri / process.py
Created June 30, 2019 12:10
Run external process from python
import gzip
import shutil
import logging
import os
from subprocess import Popen, PIPE, TimeoutExpired
SUCCESS_CODE = 0
TIMEOUT_SECONDS = 600
@ReallyLiri
ReallyLiri / uwsgi-nginx: Dockerfile
Last active July 3, 2019 10:21
Make tiangolo/uwsgi-nginx crash on python app failure
FROM tiangolo/uwsgi-nginx:python3.7
# or FROM tiangolo/uwsgi-nginx-flask:python3.7
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY stop-supervisor.sh /etc/supervisor/stop-supervisor.sh
RUN chmod +x /etc/supervisor/stop-supervisor.sh
@ReallyLiri
ReallyLiri / gke-svc-ext-ip.sh
Last active July 10, 2019 07:41
Get external IP of a service in GKE
# required vars: gcloud-api-key.json, $GCLOUD_PROJECT, $GCLOUD_ZONE, $CLUSTER_NAME, $SERVICE_NAME
gcloud auth activate-service-account --key-file gcloud-api-key.json
gcloud config set project $GCLOUD_PROJECT
gcloud config set compute/zone $GCLOUD_ZONE
lines=`gcloud container clusters list --filter NAME="$CLUSTER_NAME" | wc -l`
if [ $lines -eq 0 ] ; then echo "No deployed cluster for $CLUSTER_NAME" && exit 1 ; fi
gcloud container clusters get-credentials $CLUSTER_NAME
external_ip=$(kubectl get svc $SERVICE_NAME --template="{{range .status.loadBalancer.ingress}}{{.ip}}{{end}}")
echo "IP for cluster $CLUSTER_NAME is $external_ip"
@ReallyLiri
ReallyLiri / ng-routing.interceptor.ts
Created July 10, 2019 07:51
Routing Interceptor for Angular7
import { Router, NavigationStart } from '@angular/router';
import { filter, pairwise } from 'rxjs/operators';
export function parseQuery(queryString: string) {
const query = {};
const pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&');
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i].split('=');
query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
@ReallyLiri
ReallyLiri / injectable_resource.py
Created July 11, 2019 08:47
Flask resource with http method logic injection capability
from functools import wraps
from flask_restplus import Resource
HTTP_METHODS = ["get", "post", "delete", "put", "patch"]
class InjectableResource(Resource):
def hook_all_http_methods(self, pre_execution=None, post_execution=None, exception_handler=None):
for method in HTTP_METHODS:
@ReallyLiri
ReallyLiri / map_reduce.py
Created July 11, 2019 08:52
Simply map-reduce with multiprocessing
import logging
import pandas as pd
import queue
from multiprocessing import Process, Queue
from time import sleep
DEFAULT_TIMEOUT_SEC = 60*10
def _chunks(l, n):
@ReallyLiri
ReallyLiri / run_process.py
Created July 11, 2019 08:53
Run external processes from python
import gzip
import shutil
import logging
import os
from subprocess import Popen, PIPE, TimeoutExpired
SUCCESS_CODE = 0
TIMEOUT_SECONDS = 600
@ReallyLiri
ReallyLiri / remote_debug.py
Created July 11, 2019 08:55
Attach remote debugger to python code point
import sys
import os
import pydevd
import logging
def stop_on():
sys.path.append('/Applications/PyCharm.app/Contents/helpers/pydev/')
sys.path.append('/Applications/PyCharm.app/Contents/debug-eggs/')
@ReallyLiri
ReallyLiri / gcr-cleanup.sh
Last active February 16, 2021 11:34
Google Container Registry cleanup
#!/bin/bash
# USAGE: gcr-cleanup.sh [tag-to-clean]
# if you want to cleanup a specific tag, pass its name, otherwise all non-tagged images will be cleaned
images=$(gcloud container images list --repository=gcr.io/xxx --format='get(name)')
for image_name in $images; do
if [ -n "$1" ]
then
gcloud container images untag --quiet $image_name:$1