Skip to content

Instantly share code, notes, and snippets.

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,
FROM pg_indexes
WHERE tablename = '$$table_name$$'
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 /
Created September 23, 2018 10:23
Observer Pattern Blog: Code Snippets
class Subject(object):
def __init__(self):
self.observer_list = []
def register_observer(obs):
def remove_observer(obs):
if obs in self.observer_list:
ketanbhatt /
Created September 23, 2018 10:21
Observer Pattern Blog: Code Snippets
class NewsGetterMachine(object):
def __init__(self): = None
def get_news():
def news_flash():
news = self.get_news()
ketanbhatt /
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 /
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 /
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'] # ^_^
ketanbhatt /
Created September 23, 2018 03:08
Django Haystack fix EdgeNGram Analyzer
class StandardAnalyzerElasticBackend(ElasticsearchSearchBackend):
'settings': {
"analysis": {
"analyzer": {
"ngram_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["haystack_ngram", "lowercase"]
ketanbhatt /
Created September 23, 2018 03:06
Django Haystack Mixin to make templates obsolete
class AutoPrepareTextIndexMixin(object):
Used with Indexed Classes to add common functions:
1. Prepares text to be documented using fields from the list document_fields
2. Gets fields to be `select_related` for efficient db querying while indexing
3. Defines "get_updated_field"
Also tries to check for these modifications and raises errors if things not implemented like they should be
def __init__(self):
ketanbhatt /
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:
auto_now = field.__getattribute__('auto_now')
except AttributeError:
auto_now = False