Skip to content

Instantly share code, notes, and snippets.

View himaprasoonpt's full-sized avatar
🎯
Focusing

Himaprasoon P T himaprasoonpt

🎯
Focusing
View GitHub Profile
@himaprasoonpt
himaprasoonpt / block_system_import.py
Last active September 12, 2021 00:30
Block os import and any other system import in python
from contextlib import contextmanager
@contextmanager
def block_system_import():
block_import = ["os",'sys']
saved = {}
import sys
for i in block_import:
saved[i] = sys.modules[i]
sys.modules[i] = None
yield None
@himaprasoonpt
himaprasoonpt / protobuff_install_mac.sh
Created February 9, 2019 13:56
Google protobuf installation on Mac
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protobuf-all-3.6.1.tar.gz
tar xvf protobuf-all-3.6.1.tar.gz
cd protobuf-3.6.1
./configure CC=clang CXX=clang++ CXXFLAGS='-std=c++11 -stdlib=libc++ -O3 -g' LDFLAGS='-stdlib=libc++' LIBS="-lc++ -lc++abi"
make -j 4
sudo make install
protoc --version
@himaprasoonpt
himaprasoonpt / tensorboard_launcher.py
Created February 9, 2019 13:55
Launch Tensorboard from python code
import tensorflow as tf
from tensorboard import main as tb
tf.flags.FLAGS.logdir = "/tmp/hima"
tf.flags.FLAGS.port = "6009"
tb.main()
@himaprasoonpt
himaprasoonpt / get_version.py
Created February 9, 2019 13:52
Get module/ package version of any package
import pkg_resources
print(pkg_resources.get_distribution('typeguard').version)
@himaprasoonpt
himaprasoonpt / maximal_cycle_free_path.py
Created February 9, 2019 13:51
Generate the maximal cycle-free paths in graph (as dictionary) starting at a node
def maximal_cycle_free_path(graph, v):
"""Generate the maximal cycle-free paths in graph starting at v.
graph must be a mapping from vertices to collections of
neighbouring vertices.
>>> g = {1: [2, 3], 2: [3, 4], 3: [1], 4: []}
>>> sorted(get_paths(g, 1))
[[1, 2, 3], [1, 2, 4], [1, 3]]
>>> sorted(get_paths(g, 3))
[[3, 1, 2, 4]]
"""
@himaprasoonpt
himaprasoonpt / opencv_show_image.py
Created February 9, 2019 13:51
OpenCV python show image
import cv2
def show_image(img, wait_key=0, text="image", fx=0.5, fy=0.3):
"""
Utility to show image
:param img: Image array
:param wait_key: Wait key to stop showing
:param text: Text to be displayed
:param fx: resize factor in x dimension
:param fy: resize factor in y dimension
:return:
@himaprasoonpt
himaprasoonpt / sliding_window.py
Created February 9, 2019 13:50
Returns a sliding window (of width n) over data from the iterable
from itertools import islice
def sliding_window(seq, n=3):
"""
Returns a sliding window (of width n) over data from the iterable
s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...
"""
it = iter(seq)
result = tuple(islice(it, n))
if len(result) <= n:
@himaprasoonpt
himaprasoonpt / check_sublist.py
Created February 9, 2019 13:50
Check if a list is a sublist of another list (Not subset )
def contains_sublist(big_list, sub_list):
"""
:param big_list: list: bigger list to search in
:param sub_list: list: sub-list
:return: boolean: True if base_list contains sub_list, False otherwise
"""
try:
big_list.__str__().index(sub_list.__str__())
return True
except ValueError:
@himaprasoonpt
himaprasoonpt / process_bar.py
Created February 9, 2019 13:49
Bi directional Horizontal Process bar
import sys
def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=100,reversed =False):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)