Skip to content

Instantly share code, notes, and snippets.

View nitinbhojwani's full-sized avatar

Nitin Bhojwani nitinbhojwani

View GitHub Profile
@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 / 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 / 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 / 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 December 1, 2017 17:59
Run Python script inside Docker container
FROM python:3
ADD script.py /
CMD [ "python", "-u", "./script.py" ]
@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 / do_telnet.py
Created July 26, 2017 10:49
Send message over TCP connection given host and port
def do_telnet(host, port, message):
import telnetlib
tn = telnetlib.Telnet(host, port)
ret = tn.write(message + '\n')
print "sent the message: %s" % message
tn.close()
def send_thru_socket(host, port, message):
TCP_IP = host
@nitinbhojwani
nitinbhojwani / nmonitor.sh
Created March 23, 2017 07:28
Monitor individual process metrics - nmonitor usage and shell script.
#!/bin/bash
if [ "$#" -ne 2 ]; then
echo "usage error: sh nmonitor.sh <pid> <time_to_monitor_in_seconds>";exit 1
fi
pid=$1
time=$2
#read -p "Please enter your process Id: " pid
@nitinbhojwani
nitinbhojwani / django_send_mail
Created May 4, 2016 18:33
Django - Send a Mail with Attachment File like CSV
# Import EmailMessage class - https://docs.djangoproject.com/en/1.9/topics/email/#django.core.mail.EmailMessage
from django.core.mail import EmailMessage
email = EmailMessage('... Subject ...', '... Body ...', 'from-email',
['to-email-1', 'to-email-2'], ['bcc-email-1', 'bcc-email-2'])
# now let's create a csv file dynamically
import csv, StringIO
attachment_csv_file = StringIO.StringIO()
writer = csv.writer(attachment_csv_file)
@nitinbhojwani
nitinbhojwani / nr-server-commands
Created May 3, 2016 13:18
commands to setup newrelic for server monitoring in ubuntu
wget -O- https://download.newrelic.com/548C16BF.gpg | sudo apt-key add -
# create new file
sudo vi /etc/apt/sources.list.d/newrelic.list
# and write this line in above opened file
deb http://apt.newrelic.com/debian/ newrelic non-free
sudo apt-get update