Skip to content

Instantly share code, notes, and snippets.

@gaganpreet
gaganpreet / example.py
Created June 28, 2022 09:38
Generic sub-class resolved type hint
from typing import Generic, TypeVar, Union, get_type_hints
T = TypeVar("T", bound=Union[str, int])
class Base(Generic[T]):
value: T
def get_value(self) -> T:
@gaganpreet
gaganpreet / main.py
Last active March 23, 2022 13:15
WSGIMiddleware overhead
from a2wsgi import WSGIMiddleware
from fastapi import FastAPI
from fastapi.responses import ORJSONResponse, UJSONResponse
from flask import Flask
from sqlalchemy.pool import StaticPool
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

Install this add-on: https://github.com/SchumacherFM/wordpress-to-hugo-exporter

Install Disqus on the blog and set it up. I had issues with automatic syncing the comments from Wordpress to Disqus (there were some blog posts which ended up with 0 comments), so I used the manual import. More info here: https://help.disqus.com/en/articles/1717131-importing-comments-from-wordpress

Now that your comments are on Disqus, the only thing that needs to be migrated to Hugo is the actual blog content.

Start a new Hugo project using this guide: https://gohugo.io/getting-started/quick-start/

Copy all posts from hugo export to the new post directory. You should at this point have a working blog with all the text content.

Keybase proof

I hereby claim:

  • I am gaganpreet on github.
  • I am gaganpreet (https://keybase.io/gaganpreet) on keybase.
  • I have a public key ASBjmzHuWE2w0iwNGqtMJkvaH7LPtZ9kaTSkXYe96OJmQgo

To claim this, I am signing this object:

@gaganpreet
gaganpreet / pagination_without_count.py
Created October 23, 2017 12:30
Flask SQLAlchemy Pagination without count query
def optimised_pagination(query, per_page, page):
'''A more efficient pagination for SQLAlchemy
Fetch one item before offset (to know if there's a previous page)
Fetch one item after limit (to know if there's a next page)
The trade-off is that the total items are not available, but if you don't need them
there's no need for an extra COUNT query
'''
offset_start = (page - 1) * per_page
query_offset = max(offset_start - 1, 0)
@gaganpreet
gaganpreet / adobe_passwords.txt
Created November 11, 2013 14:00
Adobe passwords with frequency
123456 1911876
123456789 446148
password 345833
12345678 201571
qwerty 130828
1234567 124248
111111 113880
123123 82689
000000 76185
abc123 70794
@gaganpreet
gaganpreet / django-standalone.py
Created July 11, 2013 17:38
Use Django ORM in other Python projects
import os
import sys
cwd = os.path.dirname(__file__)
sys.path.insert(0, os.path.realpath(os.path.join(cwd, '/path/to/django/root/')))
os.environ['DJANGO_SETTINGS_MODULE'] = "mysite.settings"
from myapp.models import ModelA, ModelB
@gaganpreet
gaganpreet / pycon-dl.py
Last active December 15, 2015 16:18
A quick and dirty script to download videos from pyvideo.org
import json
import sys
import requests
import urllib
base_url = 'http://pyvideo.org'
class Video():
title = None
summary = None
@gaganpreet
gaganpreet / screenstream.sh
Created June 16, 2012 04:00
Twitch.tv stream with webcam overlay/pip
#!/bin/sh -xe
INFO=$(xwininfo -frame)
API_KEY="KEY"
WIN_GEO=$(echo $INFO | grep -oEe 'geometry [0-9]+x[0-9]+' | grep -oEe '[0-9]+x[0-9]+')
WIN_XY=$(echo $INFO | grep -oEe 'Corners:\s+\+[0-9]+\+[0-9]+' | grep -oEe '[0-9]+\+[0-9]+' | sed -e 's/\+/,/' )
FPS="15"
INRES='640x480'
@gaganpreet
gaganpreet / oh-my-zsh-setup.sh
Created November 25, 2011 16:30
Install oh-my-zsh
git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
chsh -s /bin/zsh