Skip to content

Instantly share code, notes, and snippets.

@anchitarnav
anchitarnav / gcp_api_call.py
Last active June 11, 2020 14:51
Doing an Google Cloud Platform / GCP / GCLOUD API Call
API_TO_HIT = "https://apikeys.googleapis.com/v2alpha1/projects/your-project-id/keys"
from google.auth.transport.requests import AuthorizedSession
from google.auth import default
# Assuming GOOGLE_APPLICATION_CREDENTIALS env variable is set
credentials, default_project_id = default(scopes=['https://www.googleapis.com/auth/cloud-platform'])
session = AuthorizedSession(credentials)
res = session.get(API_TO_HIT)
@anchitarnav
anchitarnav / gcp_api_call_explicit_creds.py
Created June 11, 2020 14:38
GCP API call using a credential file for a service account
# Doing an API call using some service account credentials
API_TO_HIT = "https://apikeys.googleapis.com/v2alpha1/projects/your-project-name/keys"
from google.auth.transport.requests import AuthorizedSession
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file(
filename='/path/to/your_service_account_creds.json',
scopes=['https://www.googleapis.com/auth/cloud-platform']
@anchitarnav
anchitarnav / gcloud-datastore-CRUD.py
Created June 23, 2020 11:21
GCP/ GCLOUD create, read, delete, update for datastore (fiorstore in datastore mode)
from google.cloud import ndb
client = ndb.Client()
# client = ndb.Client(project='your-project-name', namespace='your-namespace')
class Person(ndb.Model):
name = ndb.StringProperty()
age = ndb.IntegerProperty()
new = ndb.IntegerProperty()
@anchitarnav
anchitarnav / json_schema_validation.py
Created July 28, 2020 15:09
JSON Schema Validation
import json
from jsonschema import Draft7Validator
schema_file = '/path/to/schema_file/schema.json'
json_data = dict() # as ingested
# Loading schema file from data file folder
with open(schema_file) as fp:
json_schema = json.load(fp)
@anchitarnav
anchitarnav / python_graphs_vis_js.py
Created August 11, 2020 04:55
Create Excellent vis.js graphs using python
# https://github.com/WestHealth/pyvis
# pip install pyvis
from pyvis.network import Network
g = Network(directed=True)
# If you want images as nodes
# g.add_node(0, label='Attacker', image='https://storage.googleapis.com/aarnav-test-sample/test01/hacker.png', shape='image')
# g.add_node(1, label='VM 01', shape='image', image='https://storage.googleapis.com/aarnav-test-sample/test01/monitor.png')
@anchitarnav
anchitarnav / create_setup_short_lived.sh
Last active December 12, 2020 22:51
Creating and using Short Lived Credentials in GCP
#!/bin/bash
# Sets env variable GOOGLE_CLOUD_PROJECT if not present
[[ -z "${GOOGLE_CLOUD_PROJECT}" ]] && read -p "Enter Project ID : " GOOGLE_CLOUD_PROJECT
gcloud config set project $GOOGLE_CLOUD_PROJECT
# Creating the service accounts
gcloud iam service-accounts create sa-2-high
gcloud iam service-accounts create sa-1-low
@anchitarnav
anchitarnav / gcp_api_call_impersonated_creds.py
Created November 8, 2020 08:40
Using Impersonated Credentials to make API calls
import os
import sys
import json
# pip install requests google-auth
try:
from google.oauth2 import service_account
from google.auth import impersonated_credentials
from google.auth.transport.requests import AuthorizedSession
@anchitarnav
anchitarnav / array_prefix_sum.py
Created April 3, 2021 10:13
Implementation for Array Prefix Problem mentioned at https://www.geeksforgeeks.org/prefix-sum-2d-array/
# https://www.geeksforgeeks.org/prefix-sum-2d-array/
from copy import deepcopy
def get_from_array_if_present(array: list, x: int, y: int, default=None) -> int:
# Ensure that -ve indexes are never accessed
if x < 0 or y < 0:
return default
try:
@anchitarnav
anchitarnav / tree_traversals_iterative.py
Last active June 27, 2021 08:09
Tree Traversals iterative
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Stack(list):
def push(self, node):
self.append(node)
@anchitarnav
anchitarnav / DisJointSets.py
Created July 3, 2021 12:15
Disjoint Sets Array Implementation
# Disjoint sets are used to find cycles in a graph
from typing import Dict, Tuple, Any
class DisJointSet:
def __init__(self):
self.sets: Dict[Any, Tuple[bool, int]] = dict()
# tuple => (is_parent(bool), parent/weight(Any/int))
# -> Denotes a node is parent itself
def get_parent(self, node):