Skip to content

Instantly share code, notes, and snippets.

View nipuntalukdar's full-sized avatar

Nipun Talukdar nipuntalukdar

View GitHub Profile
@nipuntalukdar
nipuntalukdar / xgb_tree_count.py
Created September 11, 2024 02:04
Get the number of trees in an XGBoost model
import sys
import json
import xgboost as xgb
if len(sys.argv) < 2:
print(f'Usage: {sys.argv[0]} <model-file>')
exit(1)
loaded_model = xgb.Booster()
loaded_model.load_model(sys.argv[1])
@nipuntalukdar
nipuntalukdar / multiprocessing_work_que.py
Created July 5, 2024 07:36
Example of using multiprocessing python module as work queue executor
from multiprocessing import Process, Queue
from queue import Empty
import os
from time import sleep
def fun(q, timeout):
while True:
try:
item = q.get(timeout=timeout)
print(item, os.getpid())
from __future__ import print_function
from datetime import datetime
import asyncore
from smtpd import SMTPServer
class EmlServer(SMTPServer):
no = 0
def process_message(self, peer, mailfrom, rcpttos, data, mail_options=None,
rcpt_options=None):
filename = '%s-%d.eml' % (datetime.now().strftime('%Y%m%d%H%M%S'),
@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