Skip to content

Instantly share code, notes, and snippets.

View himat's full-sized avatar
💭
Making

Hima himat

💭
Making
View GitHub Profile
Go to your website of choice in Chrome. e.g.: calendar.google.com
At the top right, click the 3 dots menu
Go to the "More Tools" sub-menu
Click "Create Shortcut"
Give this soon-to-be app a title, and make sure to select "Open as window"
Press Create, and you'll now have an icon in your dock that you can use to open up this page directly and it'll always be in its own window!!
@himat
himat / map_option.rs
Created December 16, 2020 19:51
[Extract field inside of optional struct as an option] Use this when you want to extract a field from an optional struct and keep that field also as an option type #rust
struct User {
id: i32,
}
// fn do_something_with_user_id(Option<i32>);
// Bad, simple solution
fn something(user_option: Option<User>) {
let user_id_option = if let Some(user) = user_option { Some(user.id) } else { None };
do_something_with_user_id(user_id_option);
@himat
himat / update_react_state_ex.js
Created December 16, 2020 16:25
[Update element in react state array] #react
// https://stackoverflow.com/a/46761122/1807163
const [employees, setEmployees] = useState([]);
const onUpdate((newEmployee) => {
setEmployees((existingEmployees) => {
const index = employees.findIndex(emp => emp.id === newEmployee.id);
const newEmployees = [...existingEmployees]; // important to create a copy, otherwise you'll modify state outside of setState call
newEemployees[index] = employee;
return newEmployees;
});
@himat
himat / find.js
Last active November 11, 2020 04:57
[Find which page stylesheet contains a certain css text] #javascript
for (var sheetI=0; sheetI<document.styleSheets.length; sheetI+=1) {
try {
var foundRule = Array.from(document.styleSheets[sheetI].cssRules).find(rule => rule.cssText.includes(".intercom-sg4b1o"));
console.log("found? doc", sheetI, " - ", foundRule);
} catch (error) { console.log(error); }
}
@himat
himat / css_rules.js
Created October 15, 2020 15:16
[Get all CSS rules on a webpage] #javascript
Array.from(document.styleSheets).map(s => Array.from(s.cssRules)).flat().map(c => c.cssText).join("\n")
@himat
himat / info.md
Created April 20, 2020 02:29
[deploy gcloud containers] Create containers that restart automatically (for Hasura)

This sets up a Postgres DB and Hasura container as a long-running instance that restarts automatically on failure

  • Spin up your SQL DB: gcloud sql instances create "$db_instance" --database-version=POSTGRES_11 --zone="${project_zone}" --tier=db-g1-small --storage-type=SSD --maintenance-window-day=sunday --maintenance-window-hour=00
  • Then set up your Hasura instance: gcloud compute instances create-with-container "$hasura_instance" --container-image="hasura/graphql-engine:latest" --machine-type=e2-micro --container-env-file="./hasura.env" --container-env HASURA_GRAPHQL_ADMIN_SECRET="$hasura_admin_secret",HASURA_GRAPHQL_ACCESS_KEY="$hasura_admin_secret",HASURA_GRAPHQL_DATABASE_URL="postgres://hasurauser:${hasura_db_pw}@${sql_ip}:5432/postgres" --zone="${project_zone}" --container-restart-policy=always --tags="http-server" --address="${hasura_fixed_ext_ip}"
  • You will additionally need to patch the SQL DB to allow the console to change firewall rules with: `yes | gcloud sql instances patch "$db_insta
@himat
himat / things.md
Created April 18, 2020 23:51
[Useful git tips] Things for git #git

Aborting Git Commits And Rebases

When you are amending a commit or doing an interactive rebase of a series of commits, Vim will open and when you close the file, git will continue with the procedure. But if you want to cancel the commit or rebase?

Vim allows you to quit with an error code.

:cq

This means that irrespective of the content of the buffer, Vim will signal to Git with an error code to not process the commit or rebase, effectively aborting the action.

@himat
himat / links.md
Created March 13, 2020 18:38
[Rasterio useful things] Links to useful ways of doing things #rasterio #GIS #python
@himat
himat / nan_tripup.py
Created March 9, 2020 18:10
[Don't use `is` to check for NaN in Python] Always use `np.isnan(.)` #python
>>> import pandas as pd
>>> import numpy as np
>>>
>>> s = pd.Series([10, 11, 12])
>>>
>>> s[2] = np.nan
>>> s[2] is np.nan
False
# A NaN type can get coerced which is what happened above, and when this happens, a separate instance is created that thus has a different id(.) which maks the `is` check fail
@himat
himat / adder_closure.go
Created December 7, 2019 04:34
[Using closures to maintain state] Nice abstraction to add closures instead of having to maintain separate state variables within the main body #golang
import "fmt"
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}