Skip to content

Instantly share code, notes, and snippets.

@abhijitmamarde
abhijitmamarde / timeit_demo.py
Created December 19, 2015 11:01
Script which uses timeit module to check execution times for all functions written in it.
from random import random
import types
def list_without_comprehension():
l = []
for i in xrange(1000):
l.append(int(random()*100 % 100))
return l
def list_with_comprehension():
@abhijitmamarde
abhijitmamarde / findinpath.py
Created April 4, 2016 10:16
Search a file from environment PATH
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import os
import sys
import os.path
path_dirs = os.getenv('PATH').split(':')
cmd = sys.argv[1]
@abhijitmamarde
abhijitmamarde / rfind.py
Created April 4, 2016 10:25
Search files matching with patterns from current directory.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import fnmatch
import os
import sys
def find_files(filepath, patterns):
matches = 0
if os.path.exists(filepath):
@abhijitmamarde
abhijitmamarde / daysfor.py
Created August 1, 2016 18:50
Get days remaining or passed for particular date. Can add dates/events in json file
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import time
import string
from datetime import datetime
from os.path import expanduser, join
import json
@abhijitmamarde
abhijitmamarde / schedule_event.py
Created October 9, 2016 12:27
logic for scheduling event
import datetime
import time
from pprint import pprint
# duration of each slot, in mins
mins_of_slot = 15
# default time slots info
default_time_slots = [
{
@abhijitmamarde
abhijitmamarde / gitprof.py
Created October 29, 2016 16:27
git profile changer tool
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
Simple utility to change the active global git profile.
sample profile config file (yaml):
mygit:
user: Abhijit
email: abhijit.XXXX@gmail.com
@abhijitmamarde
abhijitmamarde / sample_cmd_snippet.sh
Created September 19, 2018 07:23
add all current untracked files to git ignore
$ git status -s
?? bmp.log
?? log.html
?? output.xml
?? report.html
$ git ls-files --others --exclude-standard
bmp.log
log.html
output.xml
@abhijitmamarde
abhijitmamarde / mock_builtin_len.py
Created November 30, 2018 10:41
creating mock of builtin functions in Python
from unittest.mock import Mock
import builtins
len = builtins.len
def new_len(o):
return builtins.len(o) + 1
@abhijitmamarde
abhijitmamarde / mock_os_path_exists.py
Created November 30, 2018 11:22
mock method from standard package
from os.path import exists as orig_exists
from unittest.mock import Mock
exists = orig_exists
def new_exists(file):
print(f"new_exists: checking if '{file}' exists:", orig_exists(file))
print("but will send exists = True")
return True
@abhijitmamarde
abhijitmamarde / pub_client.py
Created January 31, 2019 15:42
pub-sub using zmq and multiprocess module
import zmq
import baker
from multiprocessing import Process
@baker.command
def start(name, topicfilter="10001", port="5566"):
print(f"listening pub-server at port:{int(port)}; topic:{topicfilter}")
# Socket to talk to server
context = zmq.Context()