Skip to content

Instantly share code, notes, and snippets.

@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:
"""
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

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

"""
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 = ""
@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,得到一个新的环境