Skip to content

Instantly share code, notes, and snippets.

View navhits's full-sized avatar
💤
Code Dreaming

Naveen S navhits

💤
Code Dreaming
View GitHub Profile
@navhits
navhits / getMinutes.gs
Last active May 19, 2020 08:22
Calculate the minutes between two datetime in Google Sheets with Google AppScript
var dueBy = "A"; // Provide the appropriate column ID
var todayIs = "B" // Provide the appropriate column ID
var resultField = "C"; // Provide the appropriate column ID
var onTrack = "D" // Provide the appropriate column ID
// first sheet
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
// row range
var startRow = 2; // Mention the range start only
@navhits
navhits / HTML Redirect.html
Last active May 19, 2020 08:19
Setting up redirects without adding 301 or 302 rules. Primarily useful if you're hosting static content on Github, Firebase or other static resource hosting services.
<!doctype html>
<html>
<head>
<script>
// Adding a Javascript redirct
// The replace() will replace the current document with provided document
window.location.replace('//example.com');
</script>
<noscript>
<!--
@navhits
navhits / datetime_validation_with_pydantic_model.py
Last active March 15, 2021 10:51
A demonstration of datetime validation and parsing in Python using Pydantic library. The code should be run as-is. You can make changes to the pydantic model if you'd like to see how it works.
from pydantic import BaseModel
from datetime import datetime
import time
class Model(BaseModel):
a: datetime
b : datetime
c : datetime
d : datetime
@navhits
navhits / input_validation_with_pydantic_model.py
Last active March 15, 2021 10:58
A demonstration of getting values for a Pydantic model from std input, validating it back with same model and exporting it, in Python using Pydantic library. The code should be run as-is. You can make changes to the pydantic model if you'd like to see how it works.
# This snippet reads a Pydantic model and gets data via std input and validates them back with the same model. This validated data can be exported for further processing in your own code.
from pydantic import BaseModel, AnyUrl
from typing import Optional
# Create a Pydantic Model. All Pydantic models needs be validated as dictionary mappings. A model can also contain a sub model. Such feature can be used to validate a Dictionary object within a dictionary. Pydantic also provides ways to validate data without creating models. For more info refer the docs, https://pydantic-docs.helpmanual.io/
class MyModel(BaseModel):
name: str = None # Demonstrating the bahaviour of providing default value as None
title: Optional[str] # Demonstrating the behavior when using Optional without setting a default value (will auto default to None)
@navhits
navhits / function_arguments_validation_with_pydantic_valdiator.py
Created March 15, 2021 10:54
A demonstration of validating function arguments in Python using Pydantic library.
from pydantic import validate_arguments, ValidationError
# A function with type hints
def arg_not_validated(s: str, i: int) -> str:
return f'{type(s)}, {type(i)}'
# A function using the Pydantic validate_arugments decorator.
# Refer https://pydantic-docs.helpmanual.io/usage/validation_decorator/
@validate_arguments
def arg_validated(s: str, i: int) -> str:
@navhits
navhits / DriveQuery.py
Last active April 29, 2022 12:08
Query construction class based on Builder pattern for Google Drive API v3
import typing
import datetime
import warnings
from .enums import QueryFields, Operator, MimeType
class Query:
def __init__(self) -> None:
self.data = list()
@navhits
navhits / gitgemparse.py
Created December 15, 2021 10:48
Parse Git based gems from Gemfile
def parse(path):
with open(path) as gf:
file_stream = gf.read()
import re
dependencies = []
lines = re.findall(r"\s+?gem\s\s?(?:\'|\")[a-zA-Z0-9\_\-\.]+(?:\'|\"),?\s?(?:(?:\'|\")[0-9\.]+(?:\'|\"),\s)?git:.*", file_stream)
if lines:
temp = []
for line in lines:
@navhits
navhits / fastapi_app.py
Created January 7, 2024 16:59
A FastAPI example for auth using Authorization header
import os
import secrets
from fastapi import FastAPI, Security, HTTPException, status, Depends
from starlette.responses import JSONResponse
from fastapi.security.api_key import APIKeyHeader, APIKey
auth_header = APIKeyHeader(name="Authorization", auto_error=False)