Skip to content

Instantly share code, notes, and snippets.

View jairajsahgal's full-sized avatar

jairajsahgal jairajsahgal

View GitHub Profile
@jairajsahgal
jairajsahgal / handler_example.py
Last active November 30, 2023 21:02
Handler Example
LOGGING = {
# ... (other logging configurations)
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'include_html': True,
},
},
# ... (loggers, filters, and formatters)
@jairajsahgal
jairajsahgal / compressing_image.py
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
jairajsahgal / TimeBasedModel.py
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
jairajsahgal / saving_compress_image_django.py
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
jairajsahgal / get_query_and_query_number.py
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):
@functools.wraps(func)
def inner_func(*args, **kwargs):
reset_queries()
start_queries = len(connection.queries)
start = time.perf_counter()
@jairajsahgal
jairajsahgal / animated_sin_wave.py
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
# https://towardsdatascience.com/animations-with-matplotlib-d96375c5442c
# https://riptutorial.com/matplotlib/example/23558/basic-animation-with-funcanimation
def func(t, line):
@jairajsahgal
jairajsahgal / word_auto_complete.py
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
jairajsahgal / reverse_video_save.py
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 = cap.read()
counter = 0
@jairajsahgal
jairajsahgal / password_generator.sh
Created March 18, 2021 07:58
Password Generator in Bash
#!/bin/bash
# Simple password generator
echo "This is a simple password generator"
echo "Please enter the length of the password"
read PASS_LENGTH
echo "Please enter number of passwords to be generated"
read NO_OF_PASS
echo "Enter website for which password is to be generated"
read COMPANY
from random import *
l=["Head","Tail"]
shuffle(l)
print(l[0])