Skip to content

Instantly share code, notes, and snippets.

@gurunars
gurunars / birthday_loops.py
Last active June 5, 2019 10:07
Loopy and non loopy solutions
import json
def find_meaning(param):
return 11
def try_luck(param):
return 7
@gurunars
gurunars / gist:f3645a9289fd642242a1ce29d133a4d4
Created January 14, 2018 18:55
Regexp to replace all javaish docstrings that could be one-liners with one line docstrings e.g. via IntelliJ
Regexp:
\/\*\*\n\s*\*(.{1,80})\n\s*\*\/
With:
/**$1 */
From:
/**
* Foobar
*/
@gurunars
gurunars / cleanup_docker.sh
Created April 20, 2017 13:41
cleanup-docker
#!/bin/bash
docker ps -aqf "status=exited" | xargs -r docker rm -f
docker images -qf "dangling=true" | xargs -r docker rmi -f
docker volume ls -qf "dangling=true" | xargs -r docker volume rm
ffmpeg -i input.mp4 -vf scale=360:640 -pix_fmt rgb24 output.gif
@gurunars
gurunars / base_test.py
Last active May 26, 2017 10:52
Python unit test with multipatching
from abc import ABCMeta, abstractproperty
import unittest
import mock
class BaseTest(unittest.TestCase):
__metaclass__ = ABCMeta
@gurunars
gurunars / sample-program
Last active July 12, 2016 11:59
Sample Linux service config for init.d
#!/bin/sh
# Put to /usr/bin/sample-program
sleep 600 &
PID="$!"
# Trap is required to kill all child processes when the service is stopped
trap "kill $PID" exit INT TERM KILL TERM
@gurunars
gurunars / path_finder.py
Last active October 16, 2016 19:14
Djikstra algorithm implementation in Python
"""
Glossary used in the functions below:
Node can be virtually any hashable datatype.
:param start: starting node
:param end: ending node
:param weighted_graph: {"node1": {"node2": "weight", ...}, ...}
"""
@gurunars
gurunars / find_latest_ami_name.py
Last active October 30, 2020 01:41
AWS lambda function to find the latest Amazon AMI
"""
Publish the function to S3:
cd $DIR_WITH_THIS_SCRIPT
zip find_latest_ami_name.zip find_latest_ami_name.py
aws s3 cp find_latest_ami_name.zip s3://$YOUR_S3_BUCKET/find_latest_ami_name.zip
"""
import json
@gurunars
gurunars / ask_confirmation.py
Created May 4, 2016 13:43
Ask for confirmation in Python
def confirm():
"""
Ask user to enter Y or N (case-insensitive).
:return: True if the answer is Y.
:rtype: bool
"""
answer = ""
while answer not in ["y", "n"]:
answer = raw_input("OK to push to continue [Y/N]? ").lower()
@gurunars
gurunars / main.py
Last active October 6, 2022 15:06
Regexp based validation for argparse arguments
import argparse
import re
class Validator(object):
def __init__(self, pattern):
self._pattern = re.compile(pattern)
def __call__(self, value):