Skip to content

Instantly share code, notes, and snippets.

View deepanshumehtaa's full-sized avatar
💭
If Good things are not coming to you, Snatch them™

Deepanshu mehta deepanshumehtaa

💭
If Good things are not coming to you, Snatch them™
View GitHub Profile
@deepanshumehtaa
deepanshumehtaa / useReducer.jsx
Last active November 5, 2023 13:35
useReducer
import { useReducer } from "react";
// where we need to manage multiple states
// and one solution for multiple states reduce to one i.e. `reducer`
function reducerFunc(state, action){
if(action.type === "INC"){
return state + 1
} else if (action.type === "DEC"){
return state - 1
@deepanshumehtaa
deepanshumehtaa / pydantic.py
Last active November 21, 2023 21:14
Pydantic Hack
from pydantic import BaseModel
from typing import Generic
Generics ............................................................
T = TypeVar("T")
class SuccessResponseModel(BaseModel, Generic[T]):
"""
now we don't need to define multiple Success model for like `Items`, `User`, etc
just use this like:
@deepanshumehtaa
deepanshumehtaa / test.py
Last active December 21, 2023 11:59
pytest
class SomeDataClass:
url_img1 = (
"https://test-m-invoice.s3.amazonaws.com/ttl-public/images/valid+image.jpg"
)
url_img2 = (
"http://s3.pdd-1623763075688.jpeg"
)
@deepanshumehtaa
deepanshumehtaa / logger.py
Created February 13, 2024 10:11
Python Logger
import logging
class ColoredFormatter(logging.Formatter):
RESET_CODE = '\033[0m' # Reset color
COLOR_CODES = {
'DEBUG': '\033[94m', # Blue
'INFO': '\033[92m', # Green
'WARNING': '\033[93m', # Yellow
'ERROR': '\033[91m', # Red
@deepanshumehtaa
deepanshumehtaa / Bulk Insert Update.py
Created February 22, 2024 14:04
Bulk Insert Update
Bulk Insert:
def bulk_insert(self, sql, sql_parameters, batch_size=1000):
for i in range(0, len(sql_parameters), batch_size):
batch = sql_parameters[i:i+batch_size]
cursor = self.get_connection_cursor()
cursor.executemany(sql,batch)
self.connection.commit()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome Page</title>
<style>
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
@deepanshumehtaa
deepanshumehtaa / django advanced queryset.py
Last active March 7, 2024 10:05
django advanced queryset
m2m
class Post(models.Model):
slug = models.SlugField(max_length=200, unique=True)
likes = models.ManyToManyField(User, related_name='blogpost_like', blank=True)
post = get_object_or_404(Post, slug=slug)
if post.likes.filter(id=request.user.id).exists():
post.likes.remove(request.user)
@deepanshumehtaa
deepanshumehtaa / PySpark.py
Last active March 19, 2024 19:30
PySpark
lect 0: https://colab.research.google.com/drive/1crwg2yOosVEQATTlMfPe-FdiQKZF-gFN?usp=sharing
lec 1: https://drive.google.com/file/d/1_H5jhUut-DPjT5U0vmgCkg30DKZCa_-G/view?usp=sharing
lect 2: https://colab.research.google.com/drive/1DxeRBxRaqENX-HnxqSj-fexuRxBaTp9R?usp=sharing
lect 3: https://colab.research.google.com/drive/1fzTxGc0ttQeUV4D4VQf0zYDSOb6gDkoH?usp=sharing
databrick NB: https://databricks-prod-cloudfront.cloud.databricks.com/public/4027ec902e239c93eaaa8714f173bcfc/6073444084857434/2270297475493747/7614095282524650/latest.html
Adaptable:
1. Responsive (Adaptable):
- web application is designed to adapt and respond to different screen sizes, resolutions, and orientations.
- CSS media queries.
/* Responsive styles */
@media screen and (max-width: 768px) {
/* Adjust styles for screens up to 768px wide */
.container {
padding: 10px;
}
@deepanshumehtaa
deepanshumehtaa / Singleton Aiohttp.py
Last active March 10, 2024 07:20
Singleton Aiohttp
import asyncio
from collections.abc import Coroutine
from socket import AF_INET
# AF_INET specifies the Internet address family for IPv4.
from typing import List, Optional, Any, Dict
import aiohttp
from fastapi import FastAPI
from fastapi.logger import logger as fastAPI_logger # convenient name