Skip to content

Instantly share code, notes, and snippets.

View swateek's full-sized avatar
🎯
One Small Step at a Time

Swateek Jena swateek

🎯
One Small Step at a Time
View GitHub Profile
@swateek
swateek / PyMongo Snippets
Last active December 15, 2020 07:33
All Pymongo Snippets
# Everything Pymongo
@swateek
swateek / nested_dict_keys.py
Created December 14, 2020 14:19
Python Snippets
'''
https://stackoverflow.com/a/43491315/1844056
'''
def keys_exists(element, *keys):
'''
Check if *keys (nested) exists in `element` (dict).
'''
if not isinstance(element, dict):
raise AttributeError('keys_exists() expects dict as first argument.')
@swateek
swateek / shell-script-args.sh
Last active November 1, 2020 06:41
A sample shell script with parameters
#!/bin/bash
help()
{
echo "Invalid Arguments Supplied.."
echo "############## Use one from below ##############"
echo "'./run_db.sh --start' for starting MongoDB server"
echo "'./run_db.sh --stop' for stopping MongoDB server"
echo "'./run_db.sh --clean' for clean restart of MongoDB server"
}
@swateek
swateek / progress-bar.sh
Created October 29, 2020 05:34
A Progress Bar in Shell Script
#!/usr/bin/bash
echo -ne "[#] 10%\r"
sleep 1
echo -ne "[##] 20%\r"
sleep 1
echo -ne "[###] 30%\r"
sleep 1
echo -ne "[####] 40%\r"
sleep 1
import numpy as np
from numpy.linalg import norm
class Kmeans:
'''Implementing Kmeans algorithm.'''
def __init__(self, n_clusters, max_iter=100, random_state=123):
self.n_clusters = n_clusters
self.max_iter = max_iter
@swateek
swateek / upgrade_mongodb.py
Created November 22, 2019 12:28
Upgrading MongoDB across versions.
#!/usr/bin/python
import os
import sys
import tarfile
import subprocess
from datetime import datetime
from pymongo import MongoClient
class UpgradeMongo():
@swateek
swateek / convert_to_timezone.py
Last active July 31, 2019 08:44
Converting a Timezone's Local Time to UTC
#!/usr/bin/python
import moment
# New York's
ny_now_local = moment.utcnow().timezone("America/New_York")
ny_midnight_local = ny_now_local.clone().replace(hours=0, minutes=0, seconds=0, microseconds=0)
ny_prev_midnight_local = ny_midnight_local.clone().subtract(days=1)
ny_prev_lasthour_local = ny_prev_midnight_local.clone().replace(hours=23, minutes=59, seconds=59, microseconds=999999)
@swateek
swateek / cleanOldDockers.sh
Last active December 10, 2020 05:48
clean Docker Images with <none> and stopped containers
# Clean unused dockers in repo, which are named as <none>
docker rmi -f `docker images | grep none | awk '{ print $3 }'`
docker rm `docker ps -a | awk '{ print $1 }'`
@swateek
swateek / nginx.conf
Created July 6, 2018 08:00 — forked from anjia0532/nginx.conf
nginx proxy_pass add a static parameter
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
@swateek
swateek / difference.js
Created June 8, 2018 07:54 — forked from Yimiprod/difference.js
Deep diff between two object, using lodash
/**
* Deep diff between two object, using lodash
* @param {Object} object Object compared
* @param {Object} base Object to compare with
* @return {Object} Return a new object who represent the diff
*/
function difference(object, base) {
function changes(object, base) {
return _.transform(object, function(result, value, key) {
if (!_.isEqual(value, base[key])) {