Skip to content

Instantly share code, notes, and snippets.

@ketanbhatt
ketanbhatt / update.py
Last active October 19, 2023 12:20
Update with Last modified time Django auto_now fields
def update_with_last_modified_time(qs, **kwargs):
# This function adds any auto_now field to the update call because QuerySet.update() doesn't do it :X
model_fields = qs.model._meta.get_fields()
fields_and_value_map = {}
for field in model_fields:
try:
auto_now = field.__getattribute__('auto_now')
except AttributeError:
auto_now = False
from django.contrib.admin import ModelAdmin
class MyTableAdmin(ModelAdmin):
...
paginator = LargeTablePaginator
...
@ketanbhatt
ketanbhatt / evaluate_select.sql
Last active October 18, 2019 09:37
Avoid Joins: Evaluate and Select Query
WITH user_ids AS
(SELECT id
FROM user
WHERE account_id IN
(SELECT generate_series(1,1000)))
SELECT purchase.id
FROM purchase
WHERE user_id IN
(SELECT id
FROM user_ids);
@ketanbhatt
ketanbhatt / get_idx_definitions.sql
Created August 16, 2019 09:13
Get a list of the indexes and their definitions for a given table, in Postgres
SELECT tablename,
indexname,
indexdef
FROM pg_indexes
WHERE tablename = '$$table_name$$'
@ketanbhatt
ketanbhatt / Retro.scala
Created February 3, 2019 19:08
Get scrum retro using Trello's API, in Scala
import spray.json._
import DefaultJsonProtocol._
object Main extends App {
class Card(val cardId: String, val name: String, val cardType: String, val effortHours: Double) {
def formatCard(idx: Int): String = {
idx + ". " + name + ": [" + effortHours + " hour(s)]"
}
@ketanbhatt
ketanbhatt / example_2.py
Created September 23, 2018 10:23
Observer Pattern Blog: Code Snippets
class Subject(object):
def __init__(self):
self.observer_list = []
def register_observer(obs):
self.observer_list.append(obs)
def remove_observer(obs):
if obs in self.observer_list:
self.observer_list.remove(obs)
@ketanbhatt
ketanbhatt / example_1.py
Created September 23, 2018 10:21
Observer Pattern Blog: Code Snippets
class NewsGetterMachine(object):
def __init__(self):
self.news = None
def get_news():
return self.news
def news_flash():
news = self.get_news()
@ketanbhatt
ketanbhatt / example_2.py
Created September 23, 2018 03:49
Strategy Pattern Blog: Code Snippets
# Flying Behavior
class FlyBehavior(object):
def fly():
raise NotImplementedError
class FlyWithWings(FlyBehavior):
def fly():
print "I am flying!"
class FlyNoWay(FlyBehavior):
@ketanbhatt
ketanbhatt / example_1.py
Created September 23, 2018 03:45
Strategy Pattern Blog: Code Snippets
class Duck(object):
def quack():
print "Quack! Quack!"
def swim():
print "Yaay I am swimming!"
def display():
raise NotImplementedError
@ketanbhatt
ketanbhatt / my_model_index.py
Created September 23, 2018 03:11
Django Haystack Index definition after using custom code
class MyModelIndex(AutoPrepareTextIndexMixin, CelerySearchIndex, indexes.Indexable):
model_pk = indexes.IntegerField(model_attr='pk') # This is required
text = indexes.EdgeNgramField(document=True) # This too
some_boolean = indexes.IntegerField(model_attr='some_boolean')
# Filters should map to the exact field name that the admin will access them by.
# Example: foreign keys are accessed by FKModel__id. This is for filters
rel_model = indexes.IntegerField(model_attr='rel_model__id')
document_fields = ['name', 'nickname', 'rel_model__title'] # ^_^