Skip to content

Instantly share code, notes, and snippets.

View tshrv's full-sized avatar

Tushar Srivastava tshrv

View GitHub Profile
@tshrv
tshrv / tictactoe.py
Created March 11, 2022 13:05
Low-level design for a tictactoe game using mediator design pattern
# Tic Tac Toe
from typing import Optional, Tuple
import uuid
import random
Move = Tuple[int, int]
class Stats:
@tshrv
tshrv / unittest_design_philosophy.md
Created September 26, 2021 18:46
Design philosophy for writing unit tests
  1. Test should not leave the function context. ** Most important **
  2. Test should verify the calls to methods / functions with side effects
  3. Test should have descriptive names ( alterative to this is BDD )
  4. Function and methods should only get the inputs that are essential to test functionality.
  5. Test should be per function / method not per module / class. ( You can couple tests based on functionality )
  6. Test should focus on branch coverage than LOC.
@tshrv
tshrv / dry-slugify-models.py
Created February 21, 2021 17:51
Using mixins, we can automate how the slug field in a model is populated based on some other field in the model.
class SlugifyMixin:
slug_source_field = None
slug_target_field = None
def __init__(self, *args, **kwargs):
assert self.slug_source_field is not None, 'slug_source_field is required'
assert self.slug_target_field is not None, 'slug_target_field is required'
super().__init__(*args, **kwargs)
def save(self, *args, **kwargs):
@tshrv
tshrv / image-metadata-filter.py
Created February 20, 2021 17:23
Camera model (metadata) based image filtering using PIL library
import PIL.Image
import os
def getCameraModel(image_name):
# 272 is key for camera model
try:
camera_model = PIL.Image.open(image_name)._getexif()[272]
except:
camera_model = None