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 / oracle_connect.py
Created September 23, 2022 17:46
Connect to oracle database using python
import cx_Oracle
# Connect to the database
connection = cx_Oracle.connect("user", "password", "host:port/service_name")
# Create a cursor
cursor = connection.cursor()
try:
# Execute a statement
@iKunalChhabra
iKunalChhabra / docker_scan_password.py
Created September 26, 2022 12:06
Python script to fetch password variables from inside docker container
import docker
# connect to docker client
client = docker.from_env()
# execute command to get env_list variables
output = client.containers.run(image="ikunalchhabra/airflow-datascience:latest",
command="printenv",
remove=True)
@iKunalChhabra
iKunalChhabra / counter.py
Created September 27, 2022 15:36
Count the number of occurrences of characters in a string
from collections import Counter
string = "Kunal Chhabra"
search_char = "a"
counter = Counter(string.lower())
print(counter)
# output -> Counter({'a': 3, 'h': 2, 'k': 1, 'u': 1, 'n': 1, 'l': 1, ' ': 1, 'c': 1, 'b': 1, 'r': 1})
@iKunalChhabra
iKunalChhabra / word_frequency.py
Created October 1, 2022 16:02
Get top 5 words used in text data
# author : Kunal Chhabra
from collections import Counter
# text source : https://www.gutenberg.org/files/11/11-h/11-h.htm
data = '''
Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversations?”
So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.
There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to itself, “Oh dear! Oh dear! I shall be late!” (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed q
@iKunalChhabra
iKunalChhabra / postgresql_connect.py
Created October 2, 2022 16:29
Connect to postgreSQL in Python
import psycopg2
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
@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')
@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 / 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 / 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 / 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