Skip to content

Instantly share code, notes, and snippets.

View nipuntalukdar's full-sized avatar

Nipun Talukdar nipuntalukdar

View GitHub Profile
@nipuntalukdar
nipuntalukdar / replace_multiple_lines.py
Created July 4, 2018 07:07
Script to replace multiple mattching lines with another set of lines
from __future__ import print_function
import sys
import re
if len(sys.argv) < 3:
print('{} <input-file> <line-seq-file><replace-lines-file>'.format(sys.argv[0]))
sys.exit(1)
input_lines = []
line_seq = []
@nipuntalukdar
nipuntalukdar / es_indexes.py
Created April 10, 2018 09:54
List Elasticsearch Index and their mappings and settings in a cluster
import json
from elasticsearch import Elasticsearch
try:
es = Elasticsearch(['127.0.0.1:9200'])
indexes = es.indices.get_alias('*')
index_list = []
for idx in indexes:
print 'Index ' + idx
index_list.append(idx)
package main
import (
"fmt"
"time"
"github.com/go-kit/kit/log"
"github.com/prometheus/tsdb"
"github.com/prometheus/tsdb/labels"
)
@nipuntalukdar
nipuntalukdar / mlock.py
Last active August 7, 2017 03:42
Demo showing synchronizing in using Python multiprocessing Lock
from multiprocessing import Process, Lock
from time import sleep
def f(name, l, shouldsleep):
i = 1
while i < 30:
l.acquire()
print 'Myself', name, 'The lock', l
if shouldsleep:
print name, 'will sleep for 10 secs, others have to wait'
@nipuntalukdar
nipuntalukdar / consulconfigrefresh.go
Created June 21, 2017 08:21
Consul config watch and restarting service with supervisor
package main
import (
"crypto/md5"
"encoding/json"
"flag"
"fmt"
"github.com/hashicorp/consul/api"
"io/ioutil"
"os"
@nipuntalukdar
nipuntalukdar / dlockmongo.py
Created April 15, 2017 03:18
Distributed locks using MongoDB
from threading import Thread, Lock
from time import sleep, time
from datetime import datetime
from pymongo import MongoClient
from uuid import uuid1
import atexit
class DistLockMongo(Thread):
def __init__(self, host='localhost', port=27017, db='lockdb'):
@nipuntalukdar
nipuntalukdar / mybinsearch.py
Created November 7, 2016 01:51
Binary search in sorted array
def my_binary_search(lst, val, start, end):
if start == end:
if val == lst[start]:
return start
return -1
if val < lst[start] or val > lst[end]:
return -1
if val > lst[end]:
if val == lst[end]:
return end
@nipuntalukdar
nipuntalukdar / tree.py
Created September 2, 2016 10:33
binary tree
class Tree(object):
def __init__(self, val=None):
self.left = None
self.right = None
self.val = val
def add(self, val):
if self.val is None:
self.val = val
return
@nipuntalukdar
nipuntalukdar / detect_zookeeper_leader.py
Created August 17, 2016 05:01
Detecting leader in zookeeper cluster and also getting the list of followers
import socket
'''
leader_takes an array of (host, port) tuples and returns leader, array-of-followers,
array-of-down-hosts
'''
def leader_detect(hostports):
sk = None
leader = None
followers = []
down = []
@nipuntalukdar
nipuntalukdar / ucd.sh
Created August 5, 2016 04:17
A bash function to climb up directory
# A function to climb up directory
# To climb up 13 levels up, issue command: $ ucd 13
function ucd {
if [ $# -eq 0 ]
then
cd ..
else
x=$1
curpwd=`pwd`
while [ $x -gt 0 ]