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 / postman_tests.js
Last active December 7, 2023 23:09
All POSTMAN functions that can be used to run tests
// pm.globals.clear(); // Be careful with this usage
// Save common tests in a global variable
postman.setGlobalVariable("commonTests", () => {
pm.test("Common Tests - Response Time", function(){
pm.expect(pm.response.responseTime).is.lessThan(500);
});
pm.test("Common Tests - Content Type", function(){
pm.expect(pm.response.headers.get('Content-Type')).equals('application/json');
@swateek
swateek / mysql_json.sql
Last active January 17, 2022 13:23
Working with JSON data structures in MySQL
-- https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
-- select rows where isPrivate field is ""
SELECT * FROM ecom_categories WHERE JSON_EXTRACT(`attributes` , '$.isPrivate') = "";
-- select rows where isPrivate field is not a part of JSON object
SELECT * FROM ecom_categories WHERE JSON_EXTRACT(`attributes` , '$.isPrivate') IS NULL;
-- add isPrivate field to attributes which are not present
UPDATE ecom_categories SET `attributes` = JSON_INSERT(`attributes`, '$.isPrivate', false);
@swateek
swateek / pre-commit
Last active September 9, 2022 04:56
PreCommit Hook - Convert Tab to Spaces for Golang Files
#!/bin/bash
# Print an introduction line in cyan
printf "\033[0;36mPre-commit hook - For Checking Tabs in Golang Files...\033[0m \n"
# Grab feed of staged files
files=$(git diff --name-only --cached -- "*.go")
numfiles=$( printf '%s' "$files" | grep -c '^' )
if [ $numfiles -eq 0 ]
@swateek
swateek / database_dump_restore.sh
Last active December 16, 2020 13:39
Everything MongoDB related
# using mongodump
mkdir swateek
mongodump --db="stats" --out="./swateek" --gzip
cd swateek
tar cvzf ../db_backup.tar.gz *
# using mongorestore
tar -xvzf db_backup.tar.gz
mongorestore --gzip --db="stats" --noIndexRestore stats/
@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():