Skip to content

Instantly share code, notes, and snippets.

View chandra-prakash-meghwal's full-sized avatar

Chandra Prakash Meghwal chandra-prakash-meghwal

View GitHub Profile
@chandra-prakash-meghwal
chandra-prakash-meghwal / aws-cloudformation-s3-bucket-data-expiry.yml
Created November 21, 2024 07:03
aws cloudformation template for create s3 bucket with auto removal of data using expiration
AWSTemplateFormatVersion: '2010-09-09'
Description: Create an S3 bucket with a lifecycle rule to expire current versions of objects after 1 day
Resources:
MyS3Bucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: bucket-test-auto-removal
LifecycleConfiguration:
Rules:
- Id: ExpireObjectsAfterOneDay
@chandra-prakash-meghwal
chandra-prakash-meghwal / download_html_table_as_csv.js
Last active October 12, 2024 10:26
js code to download table data to csv
function downloadTableAsCSV(tableId, filename) {
const rows = document.querySelectorAll(`#${tableId} tr`);
const csv = [];
for (const row of rows) {
const cols = row.querySelectorAll('td, th');
const csvRow = Array.from(cols).map(col => {
const data = col.innerText.replace(/"/g, '""'); // Escape double quotes
return `"${data}"`; // Wrap in quotes
}).join(','); // Join columns with a comma
@chandra-prakash-meghwal
chandra-prakash-meghwal / microservice_uml_diagram
Created July 12, 2024 10:25
UML diagram for microservices
@startuml
title Microservices Architecture on AWS
node "Microservice 1: User Service" as user_service {
database "PostgreSQL Database" as user_db
}
node "Microservice 2: Merchant Service" as merchant_service {
database "PostgreSQL Database" as merchant_db
@chandra-prakash-meghwal
chandra-prakash-meghwal / split_csv_year_and_month_wise.py
Last active June 24, 2024 06:59
python script to split csv file month wise in separate folders year wise with selected columns in the output files
import os
import pandas as pd
from pytz import timezone
# Read the original CSV file
csv_file_path = input('enter csv path ')
df = pd.read_csv(csv_file_path)
# Extract month and year from 'created_at'
df['created_at'] = pd.to_datetime(df['created_at'])
@chandra-prakash-meghwal
chandra-prakash-meghwal / mysql_rows_generator.py
Created June 18, 2024 04:51
python mysql fetch rows as generator
import mysql.connector
def fetch_rows_as_generator(query, chunk_size=100):
connection = mysql.connector.connect(
host='localhost',
user='your_username',
password='your_password',
database='your_database'
)
@chandra-prakash-meghwal
chandra-prakash-meghwal / lambda_invoke_inside_lambda.py
Created June 11, 2024 13:26
Python AWS lambda function which invokes another lambda
import boto3
import json
class LambdaClient:
def __init__(self, function_name):
self.function_name = function_name
self.client = boto3.client('lambda')
def invoke_function(self, payload):
@chandra-prakash-meghwal
chandra-prakash-meghwal / request_throttler.py
Created June 11, 2024 13:22
Decorator that prevents a function from being called more than the request limit every time period.
import json
from datetime import datetime, timedelta
from functools import wraps
class throttle(object):
"""
Decorator that prevents a function from being called more than the request limit every
time period.
To create a function that cannot be called more than 10 ten time in a minute:
@chandra-prakash-meghwal
chandra-prakash-meghwal / async_and_sync.js
Last active June 7, 2024 11:48
Javascript asynchronous and synchronous function calls
async function fetchData1() {
// Simulate API 1 call (e.g., fetching data)
await new Promise((resolve) => setTimeout(resolve, 1000));
return 'Data from API 1';
}
async function fetchData2() {
// Simulate API 2 call (e.g., fetching data)
await new Promise((resolve) => setTimeout(resolve, 1500));
return 'Data from API 2';
@chandra-prakash-meghwal
chandra-prakash-meghwal / aws_rds.yml
Created June 4, 2024 13:18
AWS Cloudformation template to create RDS MySQL Database in specific VPC
AWSTemplateFormatVersion: '2010-09-09'
Description: Creates RDS MySQL Database in specific VPC
Parameters:
ResourceNamePrefix:
Type: String
Default: testing # You can change the default prefix as needed
RDSDBMasterUsername:
Type: String
Description: RDS db master username
@chandra-prakash-meghwal
chandra-prakash-meghwal / aws_elasticache_redis.yml
Created June 4, 2024 13:15
AWS Cloudformation template to create customizable ElastiCache Redis instance
AWSTemplateFormatVersion: '2010-09-09'
Description: Creates customizable ElastiCache Redis instance.
Parameters:
RedisCacheNodeType:
Type: String
Default: cache.t3.micro
Description: The compute and memory capacity of the nodes in the node group.
SecurityGroup: