Skip to content

Instantly share code, notes, and snippets.

View shanehh's full-sized avatar
🏠

SH shanehh

🏠
View GitHub Profile
@shanehh
shanehh / lil-interpreter.rkt
Created July 12, 2022 09:22
a tiny interpreter
#lang racket
; 来源:http://www.yinwang.org/blog-cn/2012/08/01/interpreter
;;; 以下三个定义 global-env, ext-env, lookup 是对环境(environment)的基本操作:
;; 空环境
(define global-env '())
;; 扩展:对环境 env 进行扩展,把 x 映射到 v,得到一个新的环境
"""
change `$` delimiter to `\(` and `\)` pair, at mathjax
"""
import itertools
delimiters = iter(itertools.cycle(["\(", "\)"]))
# from https://math.stackexchange.com/questions/3307627
original = r"""The point is that all rational numbers can be expressed in lowest terms. They don't have to be. Yes, $\frac 24$ is a perfectly good rational number, but the same number can be expressed as $\frac 12$. When we prove $\sqrt 2$ is irrational, we assume it is rational, then say to express that rational in lowest terms as $\frac ab$. The reason to do that is that we then show both $a$ and $b$ are even, so $\frac ab$ is not in lowest terms. If we had not assumed it was in lowest terms we would not have the contradiction we are seeking."""
new = ""

"Programs must be written for people to read, and only incidentally for machines to execute." — Harold Abelson

"""
Test if site allow to be crawled with respect to `robots.txt`
ref: https://stackoverflow.com/a/64495913/11487798
"""
import requests
from protego import Protego
from urllib.parse import urlparse
@shanehh
shanehh / RememberMissingKeysDict.py
Last active April 15, 2022 07:49
A dict, which remembers missing keys
from collections import UserDict
class RememberMissingKeysDict(UserDict):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.missing_keys = set()
def get(self, k, default_v=None):
if super().get(k) is None:
const MiddlewarePipeline = () => {
const _middlewares = []
return {
apply: middleware => _middlewares.push(middleware),
run: function run (middlewares) {
if (!middlewares) {
// set for first time call `run`
middlewares = _middlewares
@shanehh
shanehh / gregorian_weeknum.py
Last active December 30, 2021 03:40
get weeknum of gregorian
import datetime as dt
from collections import namedtuple
SUNDAY = "Sunday"
yearweek = namedtuple("yearweek", ["year", "week"])
def get_weekday(date: dt.date) -> str:
"""
# docker run -d -p 4444:4444 -p 7900:7900 --shm-size="2g" selenium/standalone-chrome:4.1.0-20211209
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote("http://127.0.0.1:4444/wd/hub", DesiredCapabilities.CHROME)
driver.get("http://www.google.com")
driver.save_screenshot("test.png")
driver.quit()
@shanehh
shanehh / my-monitor.py
Last active November 23, 2021 10:03
cronjob to take a screenshot for my main monitor.. record my digital life.
#!/usr/bin/python
import os
def save_path():
import datetime as dt
from pathlib import Path
# file-naming
# https://softwareengineering.stackexchange.com/questions/61683/standard-format-for-using-a-timestamp-as-part-of-a-filename
import pyperclip
import json
def object2json_and_copy_it_to_clipboard(obj):
"""
convert any object jsonable(if need), and dumps it
copy to clipboard
"""
if hasattr(obj, "__dict__"):