Skip to content

Instantly share code, notes, and snippets.

View ngshiheng's full-sized avatar

Jerry Ng ngshiheng

View GitHub Profile
.DEFAULT_GOAL := help
.PHONY: help
help:
@echo "Welcome to $(NAME)!"
@echo "Use 'make <target>' where <target> is one of:"
@echo ""
@echo " all run build -> stop -> run"
@echo " build build docker image based on shell ENV_VAR"
@echo " stop stop docker container"
@echo " run run docker container"
@ngshiheng
ngshiheng / generate_csv.yml
Last active August 23, 2022 09:42
I Export PostgreSQL Queries to Google Sheets For Free. Here’s How https://jerrynsh.com/i-export-postgresql-queries-to-gsheets-for-free-heres-how/
name: Generate CSV
on:
workflow_dispatch:
schedule:
- cron: "30 23 * * *" # https://crontab.guru/#30_23_*_*_*
env:
PGDATABASE: ${{ secrets.PGDATABASE }}
PGHOST: ${{ secrets.PGHOST }}
PGPASSWORD: ${{ secrets.PGPASSWORD }}
PGPORT: ${{ secrets.PGPORT }}
image: renovate/renovate:32.6.12
variables:
LOG_LEVEL: debug
renovate:on-schedule:
tags:
- your-gitlab-runner-tag-if-any
only:
- schedules
// ...
app.collector.OnXML(restaurantXPath, func(e *colly.XMLElement) {
url := e.Request.AbsoluteURL(e.ChildAttr(restaurantDetailUrlXPath, "href"))
location := e.ChildText(restaurantLocationXPath)
longitude := e.ChildAttr(restaurantXPath, "data-lng")
latitude := e.ChildAttr(restaurantXPath, "data-lat")
e.Request.Ctx.Put("location", location)
// /server/src/datasources/paste.js
const { ApolloError } = require('apollo-server-cloudflare')
const moment = require('moment')
/*
Create a new paste in `PASTE_DB`.
Fetch a new `uuid` key from `KEY_DB`.
UUID is then removed from `KEY_DB` to avoid duplicates.
@ngshiheng
ngshiheng / createShortUrl.js
Last active January 13, 2022 14:07
I Built My Own TinyURL. Here’s How I Did it https://jerrynsh.com/i-built-my-own-tiny-url/
// handlers/createShortUrl.js
import { generateUniqueUrlKey } from '../utils/urlKey'
export const createShortUrl = async (request, event) => {
try {
const urlKey = await generateUniqueUrlKey()
const { host } = new URL(request.url)
const shortUrl = `https://${host}/${urlKey}`
@ngshiheng
ngshiheng / createTodoistTask.js
Last active January 13, 2022 14:08
How I Sync Daily LeetCoding Challenge to Todoist for Free https://jerrynsh.com/how-i-sync-daily-leetcoding-challenge-to-todoist/
const TODOIST_API_ENDPOINT = 'https://api.todoist.com/rest/v1'
// Passing in the `question` object from fetchDailyCodingChallenge function
const createTodoistTask = async question => {
const questionInfo = question.data.activeDailyCodingChallengeQuestion
const questionTitle = questionInfo.question.title
const questionDifficulty = questionInfo.question.difficulty
const questionLink = `https://leetcode.com${questionInfo.link}`
def add_book(book_id, storage=[]):
storage.append(book_id)
return storage
my_book_list = add_book(1)
print(my_book_list)
my_other_book_list = add_book(2)
print(my_other_book_list)
# List Comprehension
# ------------------
cProfile.run('sum([i * i for i in range(100_000_000)])')
# 5 function calls in 13.956 seconds
# Ordered by: standard name
# ncalls tottime percall cumtime percall filename:lineno(function)
# 1 8.442 8.442 8.442 8.442 <string>:1(<listcomp>)
# 1 0.841 0.841 13.956 13.956 <string>:1(<module>)
# 1 0.000 0.000 13.956 13.956 {built-in method builtins.exec}
@ngshiheng
ngshiheng / classes.py
Last active February 7, 2022 05:46
All You Need To Know About Data Classes in Python https://jerrynsh.com/all-you-need-to-know-about-data-classes-in-python/
class Car:
def __init__(self, name: str, brand: str, price: int) -> None:
self.name = name
self.brand = brand
self.price = price
car1 = Car('Model X', 'Tesla', 120_000)
car2 = Car('Model X', 'Tesla', 120_000)