Skip to content

Instantly share code, notes, and snippets.

@bparaj
bparaj / howto_python_flask_iis_wfastcgi
Last active December 1, 2023 05:42
Python Flask on IIS with wfastcgi
Assume IIS is installed. My machine already had IIs 8.5.
Install Python
==============
1. Download web installer (Python 3.6.3).
2. Run as Administrator.
3. Select custom installation for all users.
4. Choose install directory such that there are no white spaces in the path. Not sure if it needs to be done. Just being cautious.
5. Check the box for "Add Python 3.6 to PATH".
@bparaj
bparaj / nvidia_gpu_tools.py
Created January 31, 2018 22:49
Parse output of nvidia-smi to select the GPU with free memory
import subprocess
import re
def parse_free_memory(output):
"""
$ nvidia-smi -q -d MEMORY -i 1
==============NVSMI LOG==============
Timestamp : Wed Jan 31 14:13:22 2018
@bparaj
bparaj / example.prototxt
Last active February 7, 2018 23:10
Example Caffe layers
# 1.
# Flip the label values. Change 0's to 1's and viceversa.
# PowerLayer computes outputs y = (shift + scale * x) ^ power
layer {
name: "flip_label"
type: "Power"
bottom: "label"
top: "flip_label"
power_param {
power: 1
1. When drawing links from source to target, make sure that the link simply touches the target.
This will make the arrow-head at the end of the link visible.
.attr("x1", function(d) { return d.source.x_ * width + nodeW / 2 ; })
.attr("y1", function(d) { return d.source.y_ * height + nodeH / 2 ; })
.attr("x2", function(d) { return d.target.x_ * width + nodeW / 2 + nodeW / 2 * Math.sign(d.source.x_ - d.target.x_); })
.attr("y2", function(d) { return d.target.y_ * height + nodeH / 2 + nodeH / 2 * Math.sign(d.source.y_ - d.target.y_); })
2. If you want to insert text in a rectangle, the text and the rect elements should be siblings. https://stackoverflow.com/a/5450269
To keep the text within the rect, specify the following attributes:
@bparaj
bparaj / cluster_images_with_pca.py
Last active December 4, 2021 20:12
Cluster images with kmeans after dimension reduction with PCA. Use Python, OpenCV and scikit-learn.
import cv2
import numpy as np
from sklearn.decomposition import IncrementalPCA
from sklearn.cluster import KMeans
import glob
import re
from collections import defaultdict
import sys
@bparaj
bparaj / copy_mysql_database_from_windows_to_linux
Created April 26, 2019 20:32
Create a new mysql database in a Linux server from the mysql database dump obtained from Windows server. This need arose when I wanted to run tests on a snapshot of the production database.
1. Create a MySQL dump with `mysqldump.exe`::
mysqldump.exe --single-transaction -u user -p password -h localhost db_name > db_name_dump.sql
2. Transfer `db_name_dump.sql` to your Linux machine
3. Assume you are now in a Linux machine. `db_name_dump.sql` may not be encoded in utf-8.
$ # Find out what encoding the file has from the terminal.
$ file -i db_name_dump.sql
@bparaj
bparaj / physical_cpu_core_count.py
Created May 6, 2020 03:46
Count number of physical cpu cores to determine the number of parallel processes when using multiprocessing module in Python
import re
# Assume a system where /proc/cpuinfo exists. We will just parse this file with re module.
cpu_info = open("/proc/cpuinfo").read()
# Pattern of the line in "/proc/cpuinfo" which has the needed count:
# cpu cores : 12
patc = re.compile(r"cpu cores\s:\s(\d+)")
# Search and extract.
@bparaj
bparaj / check_s3_bucket_access.py
Created July 2, 2020 16:06
Check for read/write permission on a list of s3 bucket names. Use boto3.
import boto3
# Build your list of bucket names.
s3_list = ["abc", "xyz", "my-dev-bucket"]
s3 = boto3.client('s3')
for i, bkt in enumerate(s3_list):
# Try reading first.
try:
@bparaj
bparaj / using_tmux.md
Last active October 2, 2020 14:01
Basic commands for using tmux

Start a new tmux session with a name

tmux new -s mysessionname

Detach from the current session

  1. While pressing Ctrl, hit b and release both.
  2. Hit d.

List available tmux sessions

@bparaj
bparaj / install_python_libraries_apple_silicon_m1.md
Created April 1, 2021 22:16
Install scikit-learn within a virtual Python environment on Mac (Apple Silicon M1)

Issue

I was trying to install scikit-learn with:

python3 -m pip install scikit-learn

but it failed with a ton of errors while dealing with numpy as a dependency. Main error: