Skip to content

Instantly share code, notes, and snippets.

View jairajsahgal's full-sized avatar

jairajsahgal jairajsahgal

View GitHub Profile
jairajsahgal /
Last active November 30, 2023 21:02
Handler Example
# ... (other logging configurations)
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'include_html': True,
# ... (loggers, filters, and formatters)
jairajsahgal /
Created October 2, 2023 19:24
time based stamp abstract model
from django.db import models
class TimeStampedModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
jairajsahgal /
Created September 30, 2023 18:58
This method is used to compress an image and return the image object.
def compress_image(image):
# Define your compression settings here
# For example, you can resize the image and adjust the quality
max_size = (800, 800)
quality = 80
# Resize the image while preserving aspect ratio
image.thumbnail(max_size, Image.ANTIALIAS)
# Convert the image to RGB mode (necessary for saving as JPEG)
jairajsahgal /
Created September 30, 2023 18:45
This gist contains a Django model for compressing images on save. The model can be used to save compressed images to a different directory, which can help to reduce the storage space required for your images and improve the performance of your website.
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from PIL import Image
from io import BytesIO
from django.core.files.uploadedfile import InMemoryUploadedFile
class CompressedImage(models.Model):
original_image = models.ImageField(upload_to='images/')
compressed_image = models.ImageField(upload_to='images/compressed/', blank=True)
jairajsahgal /
Created September 25, 2023 06:55
The query_debugger function is a Python decorator used for debugging and profiling database queries in a Django application.
def query_debugger(func):
def inner_func(*args, **kwargs):
start_queries = len(connection.queries)
start = time.perf_counter()
jairajsahgal /
Created November 9, 2021 19:55
Animation of sin wave in python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# References
def func(t, line):
jairajsahgal /
Created November 9, 2021 17:59
This python program gives you auto complete suggestions of words just like command line.
import readline
import requests
class MyCompleter(object): # Custom completer
def __init__(self, options):
self.options = sorted(options)
jairajsahgal /
Created September 30, 2021 19:56
This program reverses your video using opencv, and saves it with mp4 content.
# make sure you have opencv installed, "pip install opencv-python"
# make sure your video is .avi or .mp4 encoded, else change the encoder accordingly
import cv2
output_file = "output.mp4"
cap = cv2.VideoCapture("Video File Location")
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
check , vid =
counter = 0
jairajsahgal /
Created March 18, 2021 07:58
Password Generator in Bash
# Simple password generator
echo "This is a simple password generator"
echo "Please enter the length of the password"
echo "Please enter number of passwords to be generated"
echo "Enter website for which password is to be generated"
from random import *