Skip to content

Instantly share code, notes, and snippets.

@sunnytarawade
sunnytarawade / constants.py
Last active July 21, 2022 12:55
Constants used by functions
MAX_FILTER_QUERY_DEPTH = 3
ANALYSIS = 'analysis'
EXPERIMENT = 'experiment'
SPECIMEN = 'specimen'
ORGANISM = 'organism'
ARTICLE = 'article'
DATASET = 'dataset'
FILE = 'file'
PROTOCOL_ANALYSIS = 'protocol_analysis'
@sunnytarawade
sunnytarawade / resolve_single_document.py
Created July 17, 2022 09:22
Returns single document based on id
def resolve_single_document(index_name,id,primary_keys):
body = {
"query":{
"bool":{
# We append '.keyword' to the key name because otherwise elasticsearch
# returns nothing. Hence we need to either append '.keyword' to key name
# or convert all the values to lower case. This is because these fields
# are not analysed by elasticsearch.
"should":[{"term":{key + '.keyword':id}} for key in primary_keys]
@sunnytarawade
sunnytarawade / ArticleSchema.py
Created July 17, 2022 09:20
Schema of Article Index
class ArticleSchema(ObjectType):
article = Field(ArticleNode,id = ID(required=True), alternate_id = ID(required = False))
# all_article = relay.ConnectionField(ArticleConnection,filter=MyInputObjectType())
all_articles = relay.ConnectionField(ArticleConnection,filter=ArticleFilter_Argument())
# just an example of relay.connection field and batch loader
some_articles = relay.ConnectionField(ArticleConnection,ids = List(of_type=String, required=True))
def resolve_article(root,info,**args):
@sunnytarawade
sunnytarawade / resolve_all.py
Last active July 21, 2022 12:55
Gets all documents of specified index based on the filter conditions provided
def resolve_all(index_name,**kwargs):
if index_name in ['derived_from_specimen','derives_specimen_sample']:
index_name = 'specimen'
elif index_name == 'derived_from_organism':
index_name = 'organism'
filter_queries = kwargs['filter'] if 'filter' in kwargs else []
query = {}
@sunnytarawade
sunnytarawade / helpers.py
Created July 17, 2022 09:10
Helper functions which are used.
# '''
# This function takes in a string path and returns a value.
# Eg. e = {'a' : {'b' : {'c' : 'd'}}}
# deep_get(e,'a.b.c') => 'd'
# '''
def deep_get(dictionary, keys, default=None):
return reduce(lambda d, key: d.get(key, default) if isinstance(d, dict) else default, keys.split("."), dictionary)
@sunnytarawade
sunnytarawade / get_projected_data.py
Created July 17, 2022 09:05
Gets the Left and Right index data and performs a join between them based on the type of join
def get_projected_data(left_index,right_index,left_index_data,right_index_data,inner_join = True):
# this is going to be our joined resultant data
res = []
# There are two types of joins, which I have named Type 1 and Type 2.
# Let's take an example for understanding:
# Analysis : {
@sunnytarawade
sunnytarawade / resolve_with_join.py
Created July 17, 2022 09:02
The recursive function which handles basic data fetching, filtering, joins without filtering (Left Join) and joins with filtering (Inner Join)
def resolve_with_join(filter,left_index):
# check how deep is the query for join. That is if we have a join between more than
# 3 indices then the query is invalid and hence throw an exception
if not is_filter_query_depth_valid(filter):
raise Exception(QUERY_MAX_DEPTH_EXCEEDED)
# if filter object is empty return all the documents of the index
if not bool(filter):
@sunnytarawade
sunnytarawade / grokking_to_leetcode.md
Created February 22, 2022 08:47 — forked from tykurtz/grokking_to_leetcode.md
Grokking the coding interview equivalent leetcode problems

GROKKING NOTES

I liked the way Grokking the coding interview organized problems into learnable patterns. However, the course is expensive and the majority of the time the problems are copy-pasted from leetcode. As the explanations on leetcode are usually just as good, the course really boils down to being a glorified curated list of leetcode problems.

So below I made a list of leetcode problems that are as close to grokking problems as possible.

Pattern: Sliding Window