Skip to content

Instantly share code, notes, and snippets.

@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
View get_idx_definitions.sql
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
View Retro.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
View example_2.py
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
View example_1.py
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
View example_2.py
# 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
View example_1.py
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
View my_model_index.py
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
ketanbhatt / standard_analyzer_elastic_backend.py
Created September 23, 2018 03:08
Django Haystack fix EdgeNGram Analyzer
View standard_analyzer_elastic_backend.py
class StandardAnalyzerElasticBackend(ElasticsearchSearchBackend):
SET_ANALYZE_STANDARD_FOR_SEARCH = getattr(settings, 'SET_ANALYZE_STANDARD_FOR_HAYSTACK_SEARCH', False)
DEFAULT_SETTINGS = {
'settings': {
"analysis": {
"analyzer": {
"ngram_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["haystack_ngram", "lowercase"]
@ketanbhatt
ketanbhatt / auto_prepare_text_index_mixin.py
Created September 23, 2018 03:06
Django Haystack Mixin to make templates obsolete
View auto_prepare_text_index_mixin.py
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
ketanbhatt / update.py
Last active October 19, 2023 12:20
Update with Last modified time Django auto_now fields
View update.py
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