Skip to content

Instantly share code, notes, and snippets.

View iKunalChhabra's full-sized avatar
🚀
Working on something great !

Kunal Chhabra iKunalChhabra

🚀
Working on something great !
View GitHub Profile
@iKunalChhabra
iKunalChhabra / df_to_html_table.py
Created March 17, 2023 15:12
Create excel like table html from pandas dataframe to send in email reports
def df_to_html_table(df):
css = """
<style>
table {
border-collapse: collapse;
width: 50%;
margin: 20px auto;
font-family: Arial, sans-serif;
}
th {
@iKunalChhabra
iKunalChhabra / cron.js
Last active March 13, 2023 16:48
parse cron expression in javascript
const cronstrue = require("cronstrue"); // https://www.npmjs.com/package/cronstrue
const cronparser = require("cron-parser"); // https://www.npmjs.com/package/cron-parser
const timeDiff = require("js-time-diff"); // https://www.npmjs.com/package/js-time-diff
let cronExpression = "5 * * * SUN";
let currentDate = new Date();
try {
let nextDate = cronparser.parseExpression(cronExpression).next().toDate();
let cronMeaning = cronstrue.toString(cronExpression);
@iKunalChhabra
iKunalChhabra / convert_bytes.py
Created February 19, 2023 12:59
Convert bytes to human readable unit and format
def convert_bytes(num):
for x in ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']:
if num < 1024.0:
return f"{num:.2f} {x}"
num /= 1024.0
print(convert_bytes(1000_000_000_000))
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
class Hash {
public static void main(String[] args) throws NoSuchAlgorithmException {
String message = "Hello World";
Hash hash = new Hash();
System.out.println("MD5: " + hash.getMD5(message));
System.out.println("SHA-1: " + hash.getSHA1(message));
@iKunalChhabra
iKunalChhabra / data_store.py
Last active June 27, 2023 00:52
A Utility to store key-value, configs, secrets, properties with python dict like interface that uses sqlite3 as a backend.
import sqlite3
import os
import json
from cryptography import fernet
class KeyValStore:
"""
KeyValStore is a simple key-value store that uses sqlite3 as a backend.
It is not intended to be used as a production database, but rather as a
@iKunalChhabra
iKunalChhabra / pii.py
Last active December 22, 2022 13:43
simple pii identifier
import country_list # pip install country_list
import phonenumbers # pip install phonenumbers
import pycountry # pip install pycountry
import langdetect # pip install langdetect
import whois # pip install python-whois
import re
import datetime
langdetect.DetectorFactory.seed = 0
@iKunalChhabra
iKunalChhabra / hash.py
Last active December 21, 2022 15:28
snippet to compute hashes for various data types
import hashlib
import json
def get_hash(data, hash_type='md5', sort=True):
if not data:
raise ValueError('Data cannot be empty')
if hash_type not in hashlib.algorithms_available:
raise ValueError(f"Invalid hash type: {hash_type}\nAvailable hash types: {hashlib.algorithms_available}")
@iKunalChhabra
iKunalChhabra / kafka_connect.py
Last active February 18, 2023 16:43
Kafka consumer producer class in python
from confluent_kafka import Consumer, Producer, KafkaError, KafkaException
from confluent_kafka.schema_registry.avro import AvroSerializer, AvroDeserializer
from confluent_kafka.serialization import StringSerializer, SerializationContext, MessageField
from confluent_kafka.schema_registry import SchemaRegistryClient
class Kafka:
def __init__(self, bootstrap_server, topic, timeout=60.0):
self.__bootstrap_server = bootstrap_server
self.__topic = topic
@iKunalChhabra
iKunalChhabra / mongo_connect.py
Last active December 20, 2022 07:18
Example class to connect to mongo and do various operations
import pymongo
import pandas as pd
class Mongo:
def __init__(self, db_name, host, port):
self.__db_name = db_name
self.__host = host
self.__port = port
self.__client = pymongo.MongoClient(self.__host, self.__port)
@iKunalChhabra
iKunalChhabra / color_logger.py
Created October 5, 2022 12:28
Print colored logs
from colorama import Fore, Back, Style
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')