Skip to content

Instantly share code, notes, and snippets.

View nitinbhojwani's full-sized avatar

Nitin Bhojwani nitinbhojwani

View GitHub Profile
@nitinbhojwani
nitinbhojwani / measure-disk-speed.py
Last active May 22, 2018 16:28
Schedule any python script or steps continuously & Example of Measure Disk Speed
import os, time, sched
MB = 1048576
def measure_write_speed(file_path):
string = "Let's test write speed..."
n = (100 * MB) // len(string)
start = time.time()
file = open(file_path, 'w')
for i in range(n):
@nitinbhojwani
nitinbhojwani / Dockerfile
Created December 1, 2017 17:59
Run Python script inside Docker container
FROM python:3
ADD script.py /
CMD [ "python", "-u", "./script.py" ]
@nitinbhojwani
nitinbhojwani / download-file.ts
Created May 30, 2018 11:25
Typescript to create and download file at client
downloadFile(dataToBeDownloaded): void {
let a = document.createElement("a");
let dataURI = "data:text/plain;base64," + btoa(dataToBeDownloaded);
a.href = dataURI;
a['download'] = this._sddcInformation.id + ".pem";
a.click();
a.remove();
}
@nitinbhojwani
nitinbhojwani / Dockerfile
Created July 10, 2018 03:59
Docker Image for UI application, to be served by Nginx server
FROM nginx:latest
# dist includes the built output - npm build
COPY dist /usr/share/nginx/html
# nginx*conf to copy all the nginx configuration files to image.
COPY nginx*conf /opt/app-name/
# copy app entrypoint shell script
COPY entrypoint.sh /opt/app-name/
@nitinbhojwani
nitinbhojwani / modulo_operations.py
Last active August 22, 2018 08:22
Multiplication and Power Modulo operations for large numbers in Python
cal_add_mod_dict = {}
cal_sub_mod_dict = {}
cal_mul_mod_dict = {}
cal_pow_mod_dict = {}
def cal_add_mod(a, b, n):
"""Calculates (a+b) mod n"""
t = (a,b,n)
if t not in cal_add_mod_dict:
cal_add_mod_dict[t] = (a % n + b % n) % n
@nitinbhojwani
nitinbhojwani / primes.py
Last active January 16, 2019 05:04
Prime Numbers till N
def get_prime_numbers(n):
prime = [True for i in range(n+1)]
res = []
p = 2
while (p * p <= n):
# If prime[p] is not changed, then it is a prime
if (prime[p] == True):
res.append(i)
# Update all multiples of p
for i in range(p * p, n+1, p):
@nitinbhojwani
nitinbhojwani / add_multiple_embedded_documents.py
Last active April 20, 2019 16:45
Mongoengine Add or Remove EmbeddedDocument in ListField of EmbeddedDocuments
# roles as Python list of dictionaries
roles = [
{'org': 'ABC', 'permissions':['RO', 'RW']},
{'org': 'DEF', 'permissions':['RO', 'RW']},
{'org': 'GHI', 'permissions':['RO', 'RW']},
{'org': 'JKL', 'permissions':['RO', 'RW']},
]
# get user object
user = User.objects(id=ObjectId('user_id')).first()
@nitinbhojwani
nitinbhojwani / threadpool_execution.py
Created April 8, 2020 12:24
Using threadpool executor in Python3
import concurrent.futures
def test_function(n):
return n ** 2
# initiate a thread pool executor
executor = concurrent.futures.ThreadPoolExecutor(max_workers=10)
# 1. submit jobs to the executor and track in a list
jobs = []
@nitinbhojwani
nitinbhojwani / processpool_executor.py
Created April 8, 2020 12:31
Using processpool executor in Python3
import concurrent.futures
def test_function(n):
return n ** 2
# initiate a process pool executor
# by default, number of workers = number of CPU cores
executor = concurrent.futures.ProcessPoolExecutor(max_workers=10)
# 1. submit jobs to the executor and track in a list
@nitinbhojwani
nitinbhojwani / TestScheduler.kt
Created July 5, 2020 08:02
Test Scheduler using Kotlin and SpringFramework - tested for Target JVM 1.8
package com.example.org.schedules
import com.example.org.services.ConstructorInjection
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
/**
* TestScheduler to schedule a method to run with fixedDelay